服务器可以自己的网站吗,小学学校网站建设计划,北京电商开发公司,做网站免费搭建本文介绍Ocelot中的QoS(Quality of Service)#xff0c;其使用了Polly对超时等请求下游失败等情况进行熔断。1、添加Nuget包添加 Ocelot.Provider.Polly 到OcelotGetway项目中2、修改 Startup.ConfigureServices 如下来添加Polly#xff1a;services .AddOcelot(new Confi… 本文介绍Ocelot中的QoS(Quality of Service)其使用了Polly对超时等请求下游失败等情况进行熔断。1、添加Nuget包添加 Ocelot.Provider.Polly 到OcelotGetway项目中2、修改 Startup.ConfigureServices 如下来添加Pollyservices .AddOcelot(new ConfigurationBuilder() .AddJsonFile(configuration.json) .Build()) .AddConsul() .AddPolly() .AddCacheManager(x x.WithDictionaryHandle()) .AddAdministration(/administration, secret);3、在WebApiA中添加一个SlowController并添加如下代码using System.Threading.Tasks;using Microsoft.AspNetCore.Mvc;namespace WebApiA.Controllers{public class SlowController : Controller { [Produces(application/json)] [Route(api/[controller]/[action])]public async Taskstring GetName() { await Task.Delay(6000);return Jonathan; } }}其中 GetName 延时6秒返回。4、在configuration.json的 ReRoutes 节点添加一个新的路由来访问刚才添加的api方法{DownstreamPathTemplate: /api/Slow/GetName,DownstreamScheme: http,DownstreamHostAndPorts: [ {Host: localhost,Port: 5001 } ],UpstreamPathTemplate: /GetName,UpstreamHttpMethod: [ Get ],QoSOptions: {ExceptionsAllowedBeforeBreaking:3,DurationOfBreak:60000,TimeoutValue: 1000 }}其中通过 QoSOptions 对该路由添加QoS对其中的3个属性解释如下ExceptionsAllowedBeforeBreaking发生几次请求异常比如超时后进行熔断该值必须大于0DurationOfBreak熔断时间单位毫秒TimeoutValue下游请求超时时间单位毫秒默认90秒用一句话描述上述配置对http://localhost:5001/api/Slow/GetName请求超过1s将会超时发生三次超时后保持60s熔断。运行WebApiA与OcelotGetway项目然后请求http://localhost:5000/GetName多次可以看到在前3次请求Time在1000ms之后返回503在第四次以后发生熔断请求后立即Time在100ms左右返回503。官方文档中说可以只配置 TimeoutValue 而不配置其它两个来达到修改超时时间的功能如下QoSOptions: {TimeoutValue:5000}该配置存在bug因为如上配置 ExceptionsAllowedBeforeBreaking 将会为0将会触发Polly配置异常我已经向Ocelot提交了一个Pull Request来修复该问题并且已经被合并到主分支中预计在下一个版本中该问题将不会存在。如果你现在想修改超时时间但是又不想使用熔断可以配置如下QoSOptions: {ExceptionsAllowedBeforeBreaking:10000,DurationOfBreak: 1,TimeoutValue: 1000}在发生很多次异常才会进行熔断并且立即从熔断中恢复。相关文章.Netcore 2.0 Ocelot Api网关教程番外篇- Ocelot v13.x升级.Netcore 2.0 Ocelot Api网关教程6- 配置管理.Netcore 2.0 Ocelot Api网关教程7- 限流.Netcore 2.0 Ocelot Api网关教程8- 缓存【.NET Core项目实战-统一认证平台】第十六章 网关篇-Ocelot集成RPC服务ocelot 自定义认证和授权eShopOnContainers 知多少[9]Ocelot gateways使用Ocelot、IdentityServer4、Spring Cloud Eureka搭建微服务网关一原文地址https://www.jianshu.com/p/c7f5f9515962.NET社区新闻深度好文欢迎访问公众号文章汇总 http://www.csharpkit.com