zoukankan      html  css  js  c++  java
  • .Netcore 2.0 Ocelot Api网关教程(9)- QoS

    本文介绍Ocelot中的QoS(Quality of Service),其使用了Polly对超时等请求下游失败等情况进行熔断。

    1、添加Nuget包

    添加 Ocelot.Provider.Polly 到OcelotGetway项目中

     
    nuget.png

    2、修改 Startup.ConfigureServices 如下来添加Polly:

    services
        .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 Task<string> 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:发生几次请求异常(比如超时)后进行熔断,该值必须大于0
    • DurationOfBreak:熔断时间(单位:毫秒)
    • TimeoutValue:下游请求超时时间(单位:毫秒,默认90秒)
      用一句话描述上述配置:对http://localhost:5001/api/Slow/GetName请求超过1s将会超时,发生三次超时后保持60s熔断。
      运行WebApiA与OcelotGetway项目,然后请求http://localhost:5000/GetName多次:
       
      QoS.GetName.gif

      可以看到在前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
    }
    

    在发生很多次异常才会进行熔断,并且立即从熔断中恢复。
    源码下载

    完,下一篇介绍请求头传递

     
     
    3人点赞
     
    Ocelot
     
     

    作者:Weidaicheng
    链接:https://www.jianshu.com/p/c7f5f9515962
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 相关阅读:
    房子不是盖来住的
    房子不是盖来住的
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/lhxsoft/p/11724858.html
Copyright © 2011-2022 走看看