修改 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">
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
在 Routes
路由配置中增加了 FileCacheOptions
。TtlSeconds
代表缓存的过期时间,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">
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
在 Routes
路由配置中增加了 RateLimitOptions
。ClientWhitelist
代表客户端 白名单,在白名单中的客户端可以不受限流的影响;EnableRateLimiting
代表是否限流;Period
代表限流的单位时间,例如1s,5m,1h,1d等;PeriodTimespan
代表客户端达到请求上限多少秒后可以重试;Limit
代表客户端在定义的时间内可以发出的最大请求数。
在 GlobalConfiguration
配置中也增加了 RateLimitOptions
。
DisableRateLimitHeaders
代表是否禁用 X-Rate-Limit 和 Retry-After 标头(请求达到上限时 response header
中的限制数和多少秒后能重试);
QuotaExceededMessage:代表请求达到上限时返回给客户端的消息;
HttpStatusCode:代表请求达到上限时返回给客户端的HTTP状态代码。ClientIdHeader
可以允许自定义用于标识客户端的标头。默认情况下为 “ClientId”
。
最重要的就是 Period,PeriodTimespan,Limit 这几个配置。
重新编译启动看一下效果:

2.3 超时/熔断
超时很好理解,就是网关请求服务时可容忍的最长响应时间。熔断的意思就是当请求某个服务的异常次数达到一定量时,那么网关在一定时间内就不再对这个服务发起请求了,直接熔断。
Ocelot
中启用 超时/熔断 需要 NuGet 安装一下 Ocelot.Provider.Polly
:

修改 Startup.cs
中的 ConfigureServices()
方法:
services.AddOcelot()
.AddConsul()
.AddCacheManager(x =>
{
x.WithDictionaryHandle();
})
.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"}">>
评论记录:
回复评论: