在上篇.Net微服务实践(二):Ocelot介绍和快速开始中咱们介绍了Ocelot,建立了一个Ocelot Hello World程序,接下来,咱们会介绍Oclot的主要特性路由和另一个特性请求聚合。这些特性都是经过配置来实现的。html
{ "ReRoutes": [], "GlobalConfiguration": {} }
Ocelot的配置文件包含两个节点: ReRoutes和GlobalConfiguration前端
Ocelot的完整配置项以下nginx
{ "DownstreamPathTemplate": "/", "UpstreamPathTemplate": "/", "UpstreamHttpMethod": [ "Get" ], "DownstreamHttpMethod": "", "DownstreamHttpVersion": "", "AddHeadersToRequest": {}, "AddClaimsToRequest": {}, "RouteClaimsRequirement": {}, "AddQueriesToRequest": {}, "RequestIdKey": "", "FileCacheOptions": { "TtlSeconds": 0, "Region": "" }, "ReRouteIsCaseSensitive": false, "ServiceName": "", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 51876, } ], "QoSOptions": { "ExceptionsAllowedBeforeBreaking": 0, "DurationOfBreak": 0, "TimeoutValue": 0 }, "LoadBalancer": "", "RateLimitOptions": { "ClientWhitelist": [], "EnableRateLimiting": false, "Period": "", "PeriodTimespan": 0, "Limit": 0 }, "AuthenticationOptions": { "AuthenticationProviderKey": "", "AllowedScopes": [] }, "HttpHandlerOptions": { "AllowAutoRedirect": true, "UseCookieContainer": true, "UseTracing": true, "MaxConnectionsPerServer": 100 }, "DangerousAcceptAnyServerCertificateValidator": false }
完整配置项中的每一项具体含义和做用接下来会一一介绍,大的配置项的主要含义以下:git
在上一篇的hello world程序中使用的就是基本配置github
{ "ReRoutes": [ { "DownstreamPathTemplate": "/api/orders", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5001 } ], "UpstreamPathTemplate": "/api/orders", "UpstreamHttpMethod": [ "Get" ] } ], "GlobalConfiguration": { "BaseUrl": "http://localhost:5000" } }
在基本配置的示例中:要实现的功能就是将 http://localhost:5000/api/orders GET 请求路由到 http://localhost:5001/api/orders GETjson
在Ocelot中,能够以{something}的形式将变量的占位符添加到模板中。占位符变量须要同时出如今DownstreamPathTemplate和UpstreamPathTemplate属性中。请求时Ocelot将尝试请求时进行替换api
{ "DownstreamPathTemplate": "/api/{everything}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5002 } ], "UpstreamPathTemplate": "/api/{everything}", "UpstreamHttpMethod": [ "Get" ] }
示例说明:全部http://localhost:5000/api/XXXXXX的请求都会路由到http://localhost:5002/api/XXXXXX
例如http://localhost:5000/api/products 路由到 http://localhost:5002/api/products
例如http://localhost:5000/api/products/1 路由到 http://localhost:5002/api/products/1缓存
验证服务器
修改配置,运行示例程序, 访问http://localhost:5000/api/products,返回了产品数据数据结构
注意:在添加Ocelot.json文件时 .AddJsonFile("Ocelot.json",false,true), 第三个参数是指定文件发生变化时,是否从新加载,示例程序中是true. 因此咱们只要修改运行目录下的配置文件,不用从新运行示例程序。
既然占位符能够作通用匹配,天然而然就有一种配置能够匹配全部请求
{ "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5002 } ], "UpstreamPathTemplate": "/{url}", "UpstreamHttpMethod": [ "Get" ] }
示例说明: 转发全部的请求到http://localhost:5002
验证
修改配置,运行示例程序, 访问http://localhost:5000/api/products,返回了产品数据
若是一个上游请求有多个路由配置都能匹配,到底该使用哪一个路由呢? 路由能够配置优先级(Priority), 0最小,路由会使用优先级高的(说明:若是多个匹配路由优先级同样,则按顺序使用第一个)
[ApiController] public class CategoryController : ControllerBase { // GET: api/Product [Route("api/categories")] [HttpGet] public IEnumerable<string> Get() { return new string[] { "电子产品", "医护用品" }; } }
{ "DownstreamPathTemplate": "/api/products", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5002 } ], "UpstreamPathTemplate": "/api/products", "UpstreamHttpMethod": [ "Get" ], "Priority": 0 }, { "DownstreamPathTemplate": "/api/categories", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5002 } ], "UpstreamPathTemplate": "/api/{everything}", "UpstreamHttpMethod": [ "Get" ], "Priority": 1 }
若是这时访问http://localhost:5000/api/products, 你们猜一下,是返回产品数据仍是类别数据?
验证
修改配置,运行示例程序, 访问http://localhost:5000/api/products,返回了类别数据, 由于类别路由的优先级是1, 优先级更高
[Route("api/orders/{id}")] [HttpGet] public string Get(int id) { string order = string.Empty; switch(id) { case 1: order = "刘明的订单"; break; case 2: order = "王天的订单"; break; default: order = "没有找到订单"; break; } return order; }
{ "DownstreamPathTemplate": "/api/orders/{id}", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5001 } ], "UpstreamPathTemplate": "/api/orders?id={id}", "UpstreamHttpMethod": [ "Get" ] }
咱们指望的结果是,当访问http://localhost:5000/api/orders?id=1 (下游服务实际没这个接口)时 路由到http://localhost:5001/api/orders/1返回订单明细
验证
修改配置,运行示例程序, 访问http://localhost:5000/api/orders?id=1,返回了订单明细数据
有一种场景,前端一个页面,调用了多个API,要同时开多个链接几回调用才能所有所须要的数据,为了减小没必要要的请求和开销,Ocelot也支持请求聚合
{ "DownstreamPathTemplate": "/api/orders", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5001 } ], "UpstreamPathTemplate": "/api/orders", "UpstreamHttpMethod": [ "Get" ], "Key": "Orders" }, { "DownstreamPathTemplate": "/api/products", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5002 } ], "UpstreamPathTemplate": "/api/products", "UpstreamHttpMethod": [ "Get" ], "Priority": 0, "Key": "Products" }
你们注意一下,这和以前的配置有什么区别? 区别就是再每个路由配置下多了一个 Key, Key的值能够任意定义(但建议仍是按业务含义定义)
"Aggregates": [ { "ReRouteKeys": [ "Orders", "Products" ], "UpstreamPathTemplate": "/api/aggregates" } ]
注意Aggregates配置是和在ReRoutes配置平级的
{ "ReRoutes": [], "Aggregates": [], "GlobalConfiguration": {} }
示例说明: 当访问http://localhost:5000/api/aggregates, 会同时返回订单数据和产品数据
运行示例进行验证
既然是多个请求聚合,那么问题来了:
// GET: api/Product [Route("api/orders")] [HttpGet] public IEnumerable<string> Get() { throw new Exception("获取全部订单出错"); }
再次运行示例,访问http://localhost:5000/api/aggregates,Response是200, 可是body中Products节点是正常的产品数据,Orders节点里面的数据是异常信息
若是默认的聚合返回的结果数据结构不是咱们想要的,想要修改怎么办?答案是使用自定义聚合
public class FakeDefinedAggregator : IDefinedAggregator { public FakeDefinedAggregator(FakeDepdendency dep) { } public async Task<DownstreamResponse> Aggregate(List<DownstreamContext> responses) { var one = await responses[0].DownstreamResponse.Content.ReadAsStringAsync(); var two = await responses[1].DownstreamResponse.Content.ReadAsStringAsync(); var merge = $"{one}, {two}"; var headers = responses.SelectMany(x => x.DownstreamResponse.Headers).ToList(); return new DownstreamResponse(new StringContent(merge), HttpStatusCode.OK, headers, "some reason"); } }
services.AddOcelot() .AddSingletonDefinedAggregator<FakeDefinedAggregator>();
"Aggregates": [ { "ReRouteKeys": [ "Orders", "Products" ], "UpstreamPathTemplate": "/api/aggregates", "Aggregator": "FakeDefinedAggregator" } ],
与以前的配置相比,多了以下的配置,就是指定自定义聚合器的
"Aggregator": "FakeDefinedAggregator"
验证
修改配置,运行示例程序, 访问http://localhost:5000/api/aggregate, 验证返回结果
本篇咱们介绍了Ocelot配置,只要特性路由,以及请求聚合。接下里咱们会介绍Ocelot的其余特性:限流熔断、负载均衡 .Net微服务实践(四)[网关]:Ocelot限流熔断、缓存以及负载均衡