C# 从一个位置调用多个ASP.Net核心微服务
我是ASP.Net核心微服务的新手,我想知道我是否创建了多个ASP.Net核心WebApi项目,如C# 从一个位置调用多个ASP.Net核心微服务,c#,asp.net,asp.net-web-api,asp.net-core,microservices,C#,Asp.net,Asp.net Web Api,Asp.net Core,Microservices,我是ASP.Net核心微服务的新手,我想知道我是否创建了多个ASP.Net核心WebApi项目,如 http://localhost:5555/CustomerService http://localhost:6666/StoreService http://localhost:7777/EmailService &我是否可以只调用一个HTTP URL并将调用路由到特定的URL,然后将结果返回给客户端,而不是将所有URL调用到web应用程序中 差不多 不知道如何使用asp.net核心创建a
http://localhost:5555/CustomerService
http://localhost:6666/StoreService
http://localhost:7777/EmailService
&我是否可以只调用一个HTTP URL并将调用路由到特定的URL,然后将结果返回给客户端,而不是将所有URL调用到web应用程序中
差不多
不知道如何使用asp.net核心创建api网关,如有任何帮助,将不胜感激
感谢您可以使用它,它是一个.NET API网关,作为nuget包添加到您的项目中。然后配置进入网关的呼叫如何路由到下游服务。这主要是通过JSON配置文件完成的,它映射上游和下游路径
它支持,这是您需要的功能。此配置的一个(未测试)示例为:
{
"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
是一个客户聚合器,可用于生成最终结果。或者可以从配置中删除此属性,该配置只需将所有返回值合并为一个有效负载。api网关只是另一个api应用程序,它具有类似于/customer
或/store
的方法,它所做的只是将请求转发到微服务上。其想法是,它是关闭/扩展的中心位置,微服务本身可以升级或重新路由。Service registry是另一种模式,您可以查看。atI建议您观看Microsoft如何在vs2017中使用docker容器实现这一点:在视频中0:50:00(在视频中播放50分钟)门面模式的种类:)我如何在组变量中替换来自azure发布管道的服务url(如主机、端口)的值,意味着产品环境中的服务名称和端口将不同,因此我需要替换值,ocelot配置只是一个类似appsettings.json的配置源,因此您可以像对待这些配置一样对待它。e、 g.拥有ocelot.{environment}.json
,或者在ci/cd管道中使用变量替换。这正是我要问的,我如何才能为ocelot.json文件执行变量替换。bex文件具有相同的参数,host,port host,portI抱歉,我不明白你的意思。根据您使用的ci/cd应用程序,变量替换的处理方式略有不同(例如,azure devops将不同于jenkins等)。