ASP.NET Core Api网关Ocelot的利用初探

Ocelot面向利用.NET运行微型处事/面向处事的体系布局的人员,这些体系布局需要在系统中具有统一的进口点。出格是我想与IdentityServer参考和承载令牌轻松集成。Ocelot是按特定顺序分列的一堆中间件。Ocelot将HttpRequest工具操纵到由其设置指定的状态,直到达到请求构建器中间件,在该中间件中它建设一个HttpRequestMessage工具,该工具用于向下游处事发出请求。发出请求的中间件是Ocelot管道中的最后一件事。它不会挪用下一其中间件。有一块中间件可将HttpResponseMessage映射到HttpResponse工具,然后将其返回给客户端。根基上,它具有很多其他成果。

代码实现
1、新建api客户端1

ASP.NET Core Api网关Ocelot的操作初探

2、新建api 网关test

ASP.NET Core Api网关Ocelot的操作初探

3、nuget安装Ocelot

ASP.NET Core Api网关Ocelot的操作初探

4、Program文件添加ConfigureAppConfiguration

public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration(conf => { conf.AddJsonFile("ocelot.json", false, true); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }

5、Startup文件设置

services.AddOcelot(Configuration);

app.UseOcelot().Wait();

6、网关项目下添加文件ocelot.json

{ "ReRoutes": [ { "DownstreamPathTemplate": "/api/WeatherForecast/GetList", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5000 } ], "UpstreamPathTemplate": "/GetList", "UpstreamHttpMethod": [ "Get" ] }, { "DownstreamPathTemplate": "/{everything}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5000 } ], "UpstreamPathTemplate": "/{everything}", "UpstreamHttpMethod": [ "Post" ] }, { "DownstreamPathTemplate": "/api/WeatherForecast/GetModel?id={s1}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5000 } ], "UpstreamPathTemplate": "/GetModel?id={s1}", "UpstreamHttpMethod": [ "Get" ] } ] }

7、2个项目运行,测试

ASP.NET Core Api网关Ocelot的操作初探

代码地点

https://gitee.com/conanOpenSource_admin/Example/commit/b3b5a6b15a060b46c5ecd2ea31f0d36791cda18c

以上就是ASP.NET Core Api网关Ocelot的利用初探的具体内容,更多关于ASP.NET Core Api网关Ocelot的资料请存眷剧本之家其它相关文章!

您大概感乐趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsjfww.html