修改 ocelot.json 配置文件:

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/products",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/products",
      "UpstreamHttpMethod": [ "Get" ],
      "ServiceName": "ProductService",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      },
      "FileCacheOptions": {
        "TtlSeconds": 5,
        "Region": "regionname"
      }
    },
    {
      "DownstreamPathTemplate": "/orders",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/orders",
      "UpstreamHttpMethod": [ "Get" ],
      "ServiceName": "OrderService",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      },
      "FileCacheOptions": {
        "TtlSeconds": 5,
        "Region": "regionname"
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:9070",
    "ServiceDiscoveryProvider": {
      "Scheme": "http",
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul"
    }
  }
}

 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
class="hide-preCode-box">

Routes 路由配置中增加了 FileCacheOptionsTtlSeconds 代表缓存的过期时间,Region 代表缓冲区名称,这个我们目前用不到。

好了,代码修改完需要编译重启一下网关项目,然后打开客户端网站测试一下:

在这里插入图片描述
可以看到,5秒之内的请求都是同样的缓存数据。Ocelot 也支持自定义缓存。

2.2 限流

限流就是限制客户端一定时间内的请求次数。

继续修改配置:

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/products",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/products",
      "UpstreamHttpMethod": [ "Get" ],
      "ServiceName": "ProductService",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      },
      "FileCacheOptions": {
        "TtlSeconds": 5,
        "Region": "regionname"
      },
      "RateLimitOptions": {
        "ClientWhitelist": [ "SuperClient" ],
        "EnableRateLimiting": true,
        "Period": "5s",
        "PeriodTimespan": 2,
        "Limit": 1
      }
    },
    {
      "DownstreamPathTemplate": "/orders",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/orders",
      "UpstreamHttpMethod": [ "Get" ],
      "ServiceName": "OrderService",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      },
      "FileCacheOptions": {
        "TtlSeconds": 5,
        "Region": "regionname"
      },
      "RateLimitOptions": {
        "ClientWhitelist": [ "SuperClient" ],
        "EnableRateLimiting": true,
        "Period": "5s",
        "PeriodTimespan": 2,
        "Limit": 2
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:9070",
    "ServiceDiscoveryProvider": {
      "Scheme": "http",
      "Host": "localhost",
      "Port": 8500,
      "Type": "Consul"
    },
    "RateLimitOptions": {
      "DisableRateLimitHeaders": false,
      "QuotaExceededMessage": "too many requests...",
      "HttpStatusCode": 999,
      "ClientIdHeader": "Test"
    }
  }
}

 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}"> class="hide-preCode-box">

Routes 路由配置中增加了 RateLimitOptionsClientWhitelist 代表客户端 白名单,在白名单中的客户端可以不受限流的影响;EnableRateLimiting 代表是否限流;Period代表限流的单位时间,例如1s,5m,1h,1d等;PeriodTimespan代表客户端达到请求上限多少秒后可以重试;Limit 代表客户端在定义的时间内可以发出的最大请求数。

GlobalConfiguration 配置中也增加了 RateLimitOptions

DisableRateLimitHeaders 代表是否禁用 X-Rate-LimitRetry-After 标头(请求达到上限时 response header 中的限制数和多少秒后能重试);

QuotaExceededMessage:代表请求达到上限时返回给客户端的消息;

HttpStatusCode:代表请求达到上限时返回给客户端的HTTP状态代码。ClientIdHeader可以允许自定义用于标识客户端的标头。默认情况下为 “ClientId”

最重要的就是 Period,PeriodTimespan,Limit 这几个配置。

重新编译启动看一下效果:

在这里插入图片描述

2.3 超时/熔断

超时很好理解,就是网关请求服务时可容忍的最长响应时间。熔断的意思就是当请求某个服务的异常次数达到一定量时,那么网关在一定时间内就不再对这个服务发起请求了,直接熔断。

Ocelot 中启用 超时/熔断 需要 NuGet 安装一下 Ocelot.Provider.Polly

在这里插入图片描述
修改 Startup.cs 中的 ConfigureServices() 方法:

//添加ocelot服务
services.AddOcelot()
    //添加consul支持
    .AddConsul()
    //添加缓存
    .AddCacheManager(x =>
    {
        x.WithDictionaryHandle();
    })
    //添加Polly
    .AddPolly();

 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

同样的在 ocelot.json 路由配置中增加 QoSOptions

"QoSOptions": {
        "ExceptionsAllowedBeforeBreaking": 3,
        "DurationOfBreak": 10000,
        "TimeoutValue": 5000
      }

 class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">

ExceptionsAllowedBeforeBreaking 代表发生错误的次数,DurationOfBreak代表熔断时间,TimeoutValue代表超时时间。

以上的配置意思就是当服务发生3次错误时,那么就熔断10秒,期间客户端的请求直接返回错误,10秒之后恢复。

这个不太好模拟,就不演示了,应该也挺好理解的。

三、总结

关于服务治理的学问还有很多,不继续了。。。就到此为止吧。

想要更深入了解 Ocelot 的,请看官网:https://ocelot.readthedocs.io/en/latest/
或者看源码:https://github.com/ThreeMammals/Ocelot

下一篇准备说一下:事件总线。


在这里插入图片描述

data-report-view="{"mod":"1585297308_001","spm":"1001.2101.3001.6548","dest":"http://iyenn.com/rec/1636891.html","extend1":"pc","ab":"new"}">>
注:本文转载自blog.csdn.net的Microi风闲的文章"https://lisaisai.blog.csdn.net/article/details/145244257"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接

评论记录:

未查询到任何数据!