加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

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

发布时间:2020-11-22 20:06:35 所属栏目:Asp教程 来源:网络整理
导读:副标题#e# 前一个章节我们已经介绍过Ocelot,相信大家也了解到,Ocelot的主要功能是接收客户端等传入的HTTP请求,并将其转发到下游服务。Ocelot当前仅以另一个http请求的形式支持此功能(将来可能是任何传输机制)。 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时候,会发现浏览器上输出如下信息:


(编辑:PHP编程网 - 黄冈站长网)

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

推荐文章
    热点阅读