加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

ASP.NET Core3.1 Ocelot路由的实现

发布时间:2020-11-22 16:49:49 所属栏目:编程 来源:网络整理
导读:这篇文章首要先容了ASP.NET Core3.1 Ocelot路由的实现,文中通过示例代码先容的很是具体,对各人的进修可能事变具有必然的参考进修代价,必要的伴侣们下面跟着小
副问题[/!--empirenews.page--]

前一个章节我们已经先容过Ocelot,信托各人也相识到,Ocelot的首要成果是吸取客户端等传入的HTTP哀求,并将其转发到下流处事。Ocelot当前仅以另一个http哀求的情势支持此成果(未来也许是任何传输机制)。
Ocelot将一个哀求路由到另一个哀求。为了让Ocelot正常事变,您必要在设置中配置一个Route。下面我们就Ocelot基本项目构建简朴先容下路由成果。

2.Ocelot基本项目构建(APIGatewayBasicDemo)

此刻我们按照GitHub孝顺者开源项目来进修Ocelot,按照下载下来Ocelot基本项目布局来看,我们能看到有一个网关项目(APIGateway),一个客户API项目(CustomersAPIServices),一个产物API项目(ProductsAPIServices)。如下图所示:

2.1Ocelot网关设置

APIGateway网关项目根目次下面有一个configuration.json设置文件,内容如下:

{ //Routes:处理赏罚上游哀求的工具(客户端),每个数组{}就是设置:上游地点和对应下流地点 "Routes": [ { //以Downstream开头的,是要转发到下流处事器的地点(CustomersAPIServices),与nginx转发相同 //下面全部Downstream开头的,构成一个转发url,转发地点是:9001/api/customers "DownstreamPathTemplate": "/api/customers", "DownstreamScheme": "http", // "DownstreamHost": "localhost", // "DownstreamPort": 9001, //转发到下流处事器的主机和端口。 "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 9001 } ], //Upstream开头是指上游处事器(客户端)会见解点,通过http get方法会见。 //也就是说客户端会见:9000/customers 现实是转发到了:9001/api/customers的处事 "UpstreamPathTemplate": "/customers", "UpstreamHttpMethod": [ "Get" ] }, { "DownstreamPathTemplate": "/api/customers/{id}", "DownstreamScheme": "http", // "DownstreamHost": "localhost", // "DownstreamPort": 9001, "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 9001 } ], "UpstreamPathTemplate": "/customers/{id}", "UpstreamHttpMethod": [ "Get" ] }, { "DownstreamPathTemplate": "/api/products", "DownstreamScheme": "http", // "DownstreamPort": 9002, // "DownstreamHost": "localhost", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 9002 } ], "UpstreamPathTemplate": "/products", "UpstreamHttpMethod": [ "Get" ] } ], //全局设置,应承包围Routes特定配置 "GlobalConfiguration": { "RequestIdKey": "OcRequestId", "AdministrationPath": "/administration" } }

下面我们就文件中这些属性举办表明:
DownstreamPathTemplate:下流路由处事地点。
DownstreamScheme:下流处事地点会见协议范例http可能https。
DownstreamHostAndPorts:是一个数据荟萃,用于界说您但愿将哀求转发到的任何下流处事的主机和端口。凡是,它仅包括一个条目,可是偶然您也许但愿对下流处事举办负载平衡哀求,Ocelot应承您添加多个条目,然后选择一个负载平衡器。
UpstreamPathTemplate:上游处事地点,即下流处事真实会见解点。
UpstreamHttpMethod:上游处事HTTP哀求方法,譬喻Get、Post。
GlobalConfiguration:顾名思义就是全局设置,此节点的设置应承包围Routes内里的设置,你可以在这里举办通用的一些设置信息。
在Ocelot中,您可以以{something}的情势将变量的占位符添加到模板中。占位符变量必要同时存在于DownstreamPathTemplate和UpstreamPathTemplate属性中。当配置为Ocelot时,Ocelot将实举动每个哀求Ocelot历程将UpstreamPathTemplate占位符中的值替代为DownstreamPathTemplate。譬喻上述/customers/{id}。

2.2网关项目中添加Ocelot支持

此刻我们在网关项目中添加Ocelot支持,代码如下:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) //.UseStartup<Startup>() //配置网关url .UseUrls("http://*:9000") .ConfigureAppConfiguration((hostingContext, config) => { //添加Ocelot设置文件 config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) .AddJsonFile("configuration.json") .AddEnvironmentVariables(); }) .ConfigureServices(s => { //添加Ocelot处事 s.AddOcelot(); }) .Configure(a => { //行使Ocelot中间件 a.UseOcelot().Wait(); });

Ocelot的设置如上代码根基完成了,下面我们看看一个基本Ocelot路由正常事变流程。

2.3CustomersAPIServices和ProductsAPIServices项目

CustomersAPIServices项目标CustomersController有如下两个要领:

[Route("api/[controller]")] public class CustomersController : Controller { [HttpGet] public IEnumerable<string> Get() { return new string[] { "Catcher Wong", "James Li" }; } [HttpGet("{id}")] public string Get(int id) { return $"Catcher Wong - {id}"; } }

ProductsAPIServices项目标ProductsController有如下一个要领:

[Route("api/[controller]")] public class ProductsController : Controller { [HttpGet] public IEnumerable<string> Get() { return new string[] { "Surface Book 2", "Mac Book Pro" }; } }

2.4运行测试

上面这三个下流路由地点按照configuration.json设置文件都别离设置了上游分发地点,我们把这三个项目按照设置信息别离在IIS上陈设起来,虽然你们也可以行使dotnet run呼吁别离启动这个三个项目。APIGateway、CustomersAPIServices、ProductsAPIServices项目绑定主机地点别离是:9000、:9001、:9002。
当我们在赏识器上打开:9000/customers时辰,会发明赏识器上输出如下信息:


(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读