Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

c# - ASP.NET Core Api-Gateway中间件(ASP.NET Core Api-Gateway middleware)

I am new to API gateways and have a question of understanding.

(我是API网关的新手,有一个了解的问题。)

I try too put a series of (micro)services behind an endpoint.

(我也尝试在端点后面放置一系列(微)服务。)

For this purpose, I have set up an ASP.NET Core Application and added the package ThreeMammals Ocelot .

(为此,我建立了一个ASP.NET Core应用程序并添加了软件包ThreeMammals Ocelot 。)

With the help of documentation I have configured the Up- and Downstreams.

(借助文档,我已经配置了上游和下游。)

So far, so good.

(到目前为止,一切都很好。)

草图

The client makes a request to http://mygateway:4242/s1/ {api} and, for example, get a JSON or XML response from Service1, as expected.

(客户端向http:// mygateway:4242 / s1 / {api}发出请求,例如,按预期从Service1获取JSON或XML响应。)

Same behavior for http://mygateway:4242/s2/ {api} with also the expected result!

(http:// mygateway:4242 / s2 / {api}的行为相同,并且预期结果相同!)

My understanding problem is with Service3.

(我的理解问题是Service3。)

When I send a request to http://mygateway/s3/ , I get the index.html as response.

(当我向http:// mygateway / s3 /发送请求时,得到index.html作为响应。)

The index.html itself requires the CSS-File 'xyz.css' via link-tag and forces the client to load the file.

(index.html本身需要通过链接标签的CSS文件'xyz.css',并强制客户端加载文件。)

<head>
  <link rel="stylesheet" type="text/css" href="xyz.css">
</head>

The request URL the client send to "mygateway" in this case is http://mygateway:4242/xyz.css and not http://mygateway:4242/ s3 /xyz.css and so the respone is a 404 not found, since the "mygateway" knows nothing about a "xyz.css"

(在这种情况下,客户端发送到“ mygateway”的请求URL是http:// mygateway:4242 / xyz.css,而不是http:// mygateway:4242 / s3 / xyz.css,因此,未响应是404,因为“ mygateway”对“ xyz.css”一无所知)

How can I fix this routing(?) issue?

(如何解决此路由(?)问题?)

Is it possible to solve this problem with ocelot middleware?

(使用ocelot中间件可以解决此问题吗?)

Or do I need something else for the service (Service3) with the SinglePageApplication (SPA)?

(还是我需要带有SinglePageApplication(SPA)的服务(Service3)的其他产品?)

Maybe is it simply not possible or wrong to place the SPA behind the gateway?

(将SPA放置在网关后面可能根本就是不可能或错误吗?)

I hope you can give me some tips to get access to a SPA or MVC website behind a gateway.

(希望您能给我一些提示,以访问网关后面的SPA或MVC网站。)

Thanks iBot

(谢谢iBot)


UPATE: Enclosed the code of index.html.

(UPATE:包含index.html的代码。)

I think that's straight forward.

(我认为这很简单。)

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Title</title>
    <base href="/" />

    <link rel="stylesheet" type="text/css" href="dist/xyz.css">

</head>
<body>
    <div id="appContainer"></div>
    <script src="dist/xyz.js" asp-append-version="true"></script>
</body>
</html>
  ask by mMilk translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your architecture design is wrong!

(您的架构设计是错误的!)

First, let's find out what this the API Gateway.

(首先,让我们找出这是什么API网关。)

An API Gateway is programming that sits in front of an application programming interface ( API ) and acts as a single point of entry for a defined group of microservices.

(API网关是一种编程,它位于应用程序编程接口( API )的前面,并充当已定义微服务组的单个入口点。)

A major benefit of using API gateways is that they allow developers to encapsulate the internal structure of an application in multiple ways, depending upon use case.

(使用API??网关的主要好处是,它们允许开发人员根据用例以多种方式封装应用程序的内部结构。)

This is because, in addition to accommodating direct requests, gateways can be used to invoke multiple back-end services and aggregate the results.

(这是因为,除了容纳直接请求之外,网关还可以用于调用多个后端服务并汇总结果。)

Ok, the name " API Gateway" shows us that it is mostly intended for API services!

(好的,名称“ API Gateway”向我们表明它主要用于API服务!)

SPA or MVC applications are not back-end services.

(SPA或MVC应用程序不是后端服务。)

You should not put your front-end applications behind the api gateway.

(您不应该将前端应用程序放在api网关后面。)

In general, your architecture should look like this:

(通常,您的架构应如下所示:) 在此处输入图片说明

An API gateway is the single entry point for all clients.

(API网关是所有客户端的单个入口点。)

SPA is client of your services and should call it through API Gateway.

(SPA是您的服务的客户端,应通过API网关调用它。)

If your application has multiple client apps, that can be a primary pivot when identifying the multiple API Gateways types, so that you can have a different facade for the needs of each client app.

(如果您的应用程序具有多个客户端应用程序,则在确定多种API网关类型时,这可能是主要的枢纽,因此您可以为每个客户端应用程序的需求使用不同的外观。)

This case is a pattern named “Backend for Frontend” (BFF) where each API Gateway can provide a different API tailored for each client app type.

(这种情况是一种名为“后端的后端”(BFF)的模式 ,其中每个API网关可以提供针对每种客户端应用程序类型量身定制的不同API。)

What if you don't want to build a proper architecture?

(如果您不想建立合适的架构怎么办?)

  1. You can configure redirect.

    (您可以配置重定向。)

    It is something like to specify a default service of API gateway.

    (就像指定API网关的默认服务。)

    Then all clients that go to http://mygateway:4242/ will redirected to http://mygateway:4242/s3/

    (然后,所有转到http://mygateway:4242/客户端将重定向到http://mygateway:4242/s3/)

  2. Ocelot allows Middleware Injection.

    (Ocelot允许中间件注入。)

    So, you can inject your custom middleware where you will check which request and where to redirect it.

    (因此,您可以将自定义中间件注入到您将检查哪个请求以及将其重定向到何处的中间件。)

  3. Use CDN to store all css and other content.

    (使用CDN来存储所有CSS和其他内容。)

  4. Inline css into html files.

    (将CSS内联到html文件中。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...