Tec*_*ion 5 c# asp.net asp.net-web-api microservices asp.net-core
我是 ASP.Net Core 微服务的新手,我想知道我是否创建了多个 asp.net core WebApi 的项目,例如
http://localhost:5555/CustomerService
http://localhost:6666/StoreService
http://localhost:7777/EmailService
Run Code Online (Sandbox Code Playgroud)
& 而不是将所有 URL 调用到 Web 应用程序中,我可以只调用一个 HTTP URL 并将调用路由到特定 url 并将结果返回给客户端吗?
类似于https://www.nginx.com/blog/building-microservices-using-an-api-gateway/
不确定如何使用 asp.net 核心创建 api 网关,任何帮助将不胜感激。
谢谢
You could use Ocelot, which is a .NET API Gateway that is added to your project as a nuget package. You then configure how calls into the gateway are routed to downstream services. This is primarily done through JSON configuration files, which map Upstream and Downstream paths.
It supports aggregrates, which is the functionality that you need. An (untested) example of this configuration is:
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/CustomerService",
"UpstreamPathTemplate": "/Customer",
"UpstreamHttpMethod": [
"Get"
],
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5555
}
],
"Key": "Customer"
},
{
"DownstreamPathTemplate": "/StoreService",
"UpstreamPathTemplate": "/Store",
"UpstreamHttpMethod": [
"Get"
],
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 6666
}
],
"Key": "Store"
},
{
"DownstreamPathTemplate": "/EmailService",
"UpstreamPathTemplate": "/Email",
"UpstreamHttpMethod": [
"Get"
],
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 7777
}
],
"Key": "Email"
}
],
"Aggregates": [
{
"ReRouteKeys": [
"Customer",
"Store",
"Email"
],
"UpstreamPathTemplate": "/",
"Aggregator": "FakeDefinedAggregator"
}
]
}
Run Code Online (Sandbox Code Playgroud)
This would expose 4 routes in your application:
/customer which would call localhost:5555/CustomerService/store which would call localhost:6666/StoreService/email which would call localhost:7777/EmailService/ which would call localhost:5555/CustomerService, localhost:6666/StoreService and localhost:7777/EmailService and aggregrate all results together.FakeDefinedAggregator is a customer aggregrator that you can use to generate the final result. Or this property can be ommited from the configuration, which would simply combine all returned values into 1 payload.
| 归档时间: |
|
| 查看次数: |
1728 次 |
| 最近记录: |