我是ASP.Net Core微服务的新手,我想知道我是否创建了多个asp.net核心WebApi项目,如
http://localhost:5555/CustomerService
http://localhost:6666/StoreService
http://localhost:7777/EmailService
&安培;我可以只调用一个HTTP URL并将调用路由到特定的URL并将结果返回给客户端,而不是将所有URL调用到Web应用程序中吗?
类似于https://www.nginx.com/blog/building-microservices-using-an-api-gateway/
不确定如何使用asp.net核心创建api网关,任何帮助都将不胜感激。
由于
答案 0 :(得分:0)
您可以使用Ocelot,它是一个.NET API网关,已作为nuget包添加到您的项目中。然后,您可以配置如何将进入网关的呼叫路由到下游服务。这主要是通过JSON配置文件完成的,该文件映射了上游和下游路径。
它支持aggregrates,这是您需要的功能。此配置的一个(未经测试的)示例是:
{
"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"
}
]
}
这将在您的应用程序中公开4条路由:
/customer
,它将调用localhost:5555 / CustomerService /store
,它将调用localhost:6666 / StoreService /email
,它将调用localhost:7777 / EmailService /
,它将调用localhost:5555 / CustomerService,localhost:6666 / StoreService和localhost:7777 / EmailService并将所有结果汇总在一起。 FakeDefinedAggregator
是客户汇总器,可用于生成最终结果。或者可以从配置中忽略此属性,只需将所有返回的值合并为1个有效载荷即可。