.Net微服务实践(三):Ocelot配置路由和请求聚合

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

.Net微服务实践(三):Ocelot配置路由和请求聚合

lcyhjx   2020-04-07 我要评论
[TOC] 在上篇[.Net微服务实践(二):Ocelot介绍和快速开始](https://www.cnblogs.com/lcyhjx/p/12649936.html)中我们介绍了Ocelot,创建了一个Ocelot Hello World程序,接下来,我们会介绍Oclot的主要特性**路由**和另外一个特性**请求聚合**。这些特性都是通过配置来实现的。 # 配置 ``` { "ReRoutes": [], "GlobalConfiguration": {} } ``` Ocelot的配置文件包含两个节点: ReRoutes和GlobalConfiguration - ReRoutes - 告诉Ocelot如何处理上游的请求 - GlobalConfiguration - 全局配置,此节点的配置允许覆盖ReRoutes里面的配置,你可以在这里进行通用的一些配置信息 Ocelot的完整配置项如下 ``` { "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 } ``` 完整配置项中的每一项具体含义和作用接下来会一一介绍,大的配置项的主要含义如下: - Downstream - 下游服务配置 - UpStream - 上游服务配置 - Aggregates - 服务聚合配置 - ServiceName, LoadBalancer, UseServiceDiscovery - 配置服务发现 - AuthenticationOptions - 配置服务认证 - RouteClaimsRequirement - 配置Claims鉴权 - RateLimitOptions - 限流配置 - FileCacheOptions - 缓存配置 - QosOptions - 服务质量与熔断 - DownstreamHeaderTransform - 头信息转发 # 路由 ## 基本配置 在上一篇的hello world程序中使用的就是基本配置 ``` { "ReRoutes": [ { "DownstreamPathTemplate": "/api/orders", "DownstreamScheme": "http", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 5001 } ], "UpstreamPathTemplate": "/api/orders", "UpstreamHttpMethod": [ "Get" ] } ], "GlobalConfiguration": { "BaseUrl": "http://localhost:5000" } } ``` - BaseUrl - Ocelot的服务运行地址,要特别注意一下BaseUrl是我们外部暴露的Url,比如我们的Ocelot运行在http://localhost:5000,但是前面有一个 nginx绑定了域名http://api.demo.com,那这里我们的BaseUrl就是 http://api.demo.com - UpstreamPathTemplate、UpstreamHttpMethod - 配置上游服务器请求URL - DownstreamPathTemplate、DownstreamScheme、DownstreamHostAndPorts - 配置下游服务器请求URL 在基本配置的示例中:要实现的功能就是将 http://localhost:5000/api/orders GET 请求路由到 http://localhost:5001/api/orders GET ## 占位符 在Ocelot中,可以以{something}的形式将变量的占位符添加到模板中。占位符变量需要同时出现在DownstreamPathTemplate和UpstreamPathTemplate属性中。请求时Ocelot将尝试请求时进行替换 ``` { "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最小,路由会使用优先级高的(说明:如果多个匹配路由优先级一样,则按顺序使用第一个) - 在product-api中添加一个category api ``` [ApiController] public class CategoryController : ControllerBase { // GET: api/Product [Route("api/categories")] [HttpGet] public IEnumerable

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们