Ocelot(五)- 流量限制、服务质量 (2)

然后,在OcelotConsole项目中添加代码如下:

public static async Task Main(string[] args) { using (var client = new HttpClient()) { for (var i = 0; i < 100; i++) { Console.WriteLine(DateTime.Now); var result = await client.GetAsync("http://localhost:4727/ocelot/ratelimit/5"); Console.WriteLine($"{result.StatusCode}, {result.Content.ReadAsStringAsync().Result}"); System.Threading.Thread.Sleep(1000); } Console.Read(); } }

每隔1s就发出一次请求,运行,在命令窗口得到以下输出:

2019/6/3 13:33:31 OK, This is from localhost:8001, path: /api/ocelot/5 2019/6/3 13:33:32 OK, This is from localhost:8001, path: /api/ocelot/5 2019/6/3 13:33:33 OK, This is from localhost:8001, path: /api/ocelot/5 2019/6/3 13:33:34 OK, This is from localhost:8001, path: /api/ocelot/5 2019/6/3 13:33:35 OK, This is from localhost:8001, path: /api/ocelot/5 2019/6/3 13:33:36 TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m. 2019/6/3 13:33:37 TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m. 2019/6/3 13:33:38 TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m. 2019/6/3 13:33:39 TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m. 2019/6/3 13:33:40 TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m. 2019/6/3 13:33:41 OK, This is from localhost:8001, path: /api/ocelot/5 2019/6/3 13:33:43 OK, This is from localhost:8001, path: /api/ocelot/5 2019/6/3 13:33:44 OK, This is from localhost:8001, path: /api/ocelot/5 2019/6/3 13:33:45 OK, This is from localhost:8001, path: /api/ocelot/5 2019/6/3 13:33:46 OK, This is from localhost:8001, path: /api/ocelot/5 2019/6/3 13:33:47 TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m. 2019/6/3 13:33:48 TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m. 2019/6/3 13:33:49 TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m. 2019/6/3 13:33:50 TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m. 2019/6/3 13:33:51 TooManyRequests, API calls quota exceeded! maximum admitted 5 per 1m.

然后,我又通过修改参数,得出如下结果:

PeriodTimespan=10, Limit=5:5个成功,5个失败

PeriodTimespan=10, Limit=6:6个成功,4个失败

PeriodTimespan=20, Limit=5:5个成功,15个失败

PeriodTimespan=30, Limit=5:5个成功,25个失败

似乎都是与我前面得到的结论相同,与官方文档不一致。
我在GitHub上提了一个issue:https://github.com/ThreeMammals/Ocelot/issues/910,期待能得到答复。

流量限制的全局配置

"RateLimitOptions": { "DisableRateLimitHeaders": true, "QuotaExceededMessage": "Customize Tips!", "HttpStatusCode": 999, "ClientIdHeader": "Test" }

DisableRateLimitHeaders:当设为true时,请求头中就不会输出流量限制的相关信息,默认值:"false"

QuotaExceededMessage:当请求数量超出流量限制时,输出的信息,默认值:"API calls quota exceeded! maximum admitted {Limit} per {Period}."

HttpStatusCode:当请求数量超出流量限制时,输出的状态码,默认值:"429"

ClientIdHeader:标识为白名单中的客户端的请求头key,默认值:"ClientId"

Ocelot_049_ratelimit_request1noheader

Ocelot_050_ratelimit_request6status

Ocelot_051_ratelimit_whitelist

案例七 服务质量

Ocelot支持服务质量与熔断,意味着当下游服务不可用时,Ocelot会进行自动熔断,不再将请求转发给该下游服务。来看看配置

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

ExceptionsAllowedBeforeBreaking:允许异常次数,当Ocelot转发给该下游服务连续出现异常次数达到该数字时,Ocelot会进行自动熔断,一段时间内不再向该下游服务转发请求

DurationOfBreak:熔断时间,单位为ms(毫秒),持续多长时间不向该下游服务转发请求

TimeoutValue:服务质量配置项,超时时间,单位为ms(毫秒),当Ocelot向下游服务转发请求多长时间后,自动认为该请求超时

ExceptionsAllowedBeforeBreaking 必须跟 DurationOfBreak一起使用,TimeoutValue可以单独使用。

首先需要安装Polly支持程序,通过Nuget搜索Ocelot.Provider.Polly或者通过以下命令安装

Install-Package Ocelot.Provider.Polly

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpxdyg.html