当前位置:   article > 正文

.NET6之MiniAPI(二十一):限流

net6 allowedhosts

限流,可以网络的基础设施进行配置实现,也可以在网关的地方进行限流,但服务本身的限流也不可或缺,因为当多副本时,一个副本故障,流量对于其他副本来说会提高,如果超过其承受请求量的范围,会引起服务连续崩塌,所以单服务本身最好也是要实现限流的。

在asp.net core项目中,可以引入AspNetCoreRateLimit进行限流处理。

可以通过如下方式引入NuGet

Install-Package AspNetCoreRateLimit

Client限流

  1. using AspNetCoreRateLimit;
  2. var builder = WebApplication.CreateBuilder(args);
  3. // 注入内存缓存服务
  4. builder.Services.AddMemoryCache();
  5. //加载ClientRateLimiting配置文件
  6. builder.Services.Configure<ClientRateLimitOptions>(builder.Configuration.GetSection("ClientRateLimiting"));
  7. //加载ClientRateLimitPolicies配置文件
  8. builder.Services.Configure<ClientRateLimitPolicies>(builder.Configuration.GetSection("ClientRateLimitPolicies"));
  9. // 注入限流内存缓存服务
  10. builder.Services.AddInMemoryRateLimiting();
  11. // 注入限流配置文件服务
  12. builder.Services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
  13. var app = builder.Build();
  14. //启用ClientRateLimitPolicies
  15. var clientPolicyStore = app.Services.GetRequiredService<IClientPolicyStore>();
  16. await clientPolicyStore.SeedAsync();
  17. //使用Client限流中间件
  18. app.UseClientRateLimiting();
  19. app.MapGet("/test00", () =>
  20. {
  21. return "get test00 ok";
  22. });
  23. app.MapGet("/test01", () =>
  24. {
  25. return "get test01 ok";
  26. });
  27. app.MapGet("/test02", () =>
  28. {
  29. return "get test02 ok";
  30. });
  31. app.MapPost("/test02", () =>
  32. {
  33. return "post test02 ok";
  34. });
  35. app.Run();

appsetings.json

  1. {
  2. "Logging": {
  3. "LogLevel": {
  4. "Default": "Information",
  5. "Microsoft.AspNetCore": "Warning"
  6. }
  7. },
  8. "AllowedHosts": "*",
  9. "ClientRateLimiting": {
  10. "EnableEndpointRateLimiting": false,
  11. "StackBlockedRequests": false,
  12. "ClientIdHeader": "X-ClientId",
  13. "HttpStatusCode": 429,
  14. "EndpointWhitelist": [ "get:/test00", "*:/test01" ],
  15. "ClientWhitelist": [ "dev-id-1", "dev-id-2" ],
  16. "GeneralRules": [
  17. {
  18. "Endpoint": "*",
  19. "Period": "5s",
  20. "Limit": 2
  21. },
  22. {
  23. "Endpoint": "*",
  24. "Period": "10s",
  25. "Limit": 3
  26.       }      
  27. ]
  28.   },
  29.    "ClientRateLimitPolicies": {
  30. "ClientRules": [
  31. {
  32. "ClientId": "client-id-1",
  33. "Rules": [
  34. {
  35. "Endpoint": "*",
  36. "Period": "5s",
  37. "Limit": 1
  38. },
  39. {
  40. "Endpoint": "*",
  41. "Period": "15m",
  42. "Limit": 200
  43. }
  44. ]
  45. },
  46. {
  47. "ClientId": "client-id-2",
  48. "Rules": [
  49. {
  50. "Endpoint": "*",
  51. "Period": "1s",
  52. "Limit": 5
  53. },
  54. {
  55. "Endpoint": "*",
  56. "Period": "15m",
  57. "Limit": 150
  58. },
  59. {
  60. "Endpoint": "*",
  61. "Period": "12h",
  62. "Limit": 500
  63. }
  64. ]
  65. }
  66. ]
  67. }
  68. }

配置说明:

EnableEndpointRateLimiting为false全部请求总次数超过阈值,即限流,EnableEndpointRateLimiting为true时,各个请求单次超过阈值即限流

StackBlockedRequests为false时,如果前5s请求了2个成功,1个失败,那第6s后还可以请求一个成功,如果这StackBlockedRequests为true时,那第6s的请求是不能成功的,也就是带不带拒绝的请求

  1. {
  2. "Endpoint": "*",
  3. "Period": "5s",
  4. "Limit": 2
  5. },
  6. {
  7. "Endpoint": "*",
  8. "Period": "10s",
  9. "Limit": 3
  10. },

ClientIdHeader指定可以通过Header键为X-ClientId处理限流黑白名单

ClientWhitelist值为dev-id-1,dev-id-2,指Header里X-ClientId只要是其中的值 ,就放行

EndpointWhitelist不参与限流的终节点

HttpStatusCode限流后返回的状态码

GeneralRules通用限流规则

ClientRateLimitPolicies 配置是专门为了对不同的X-ClientId进行不同的限流配置,这样可以有区别的来分配流程,而不是一概而论,可以说是ClientId的灰名单,有限流的访问

不但可以通过ClientID限流,还可以是客户请示Ip来限流,配置雷同。

IP限流

  1. using AspNetCoreRateLimit;
  2. var builder = WebApplication.CreateBuilder(args);
  3. // 注入内存缓存服务
  4. builder.Services.AddMemoryCache();
  5. //加载IPRateLimiting配置文件
  6. builder.Services.Configure<IpRateLimitOptions>(builder.Configuration.GetSection("IpRateLimiting"));
  7. //加载IPRateLimitPolicies配置文件
  8. builder.Services.Configure<IpRateLimitPolicies>(builder.Configuration.GetSection("IpRateLimitPolicies"));
  9. // 注入限流内存缓存服务
  10. builder.Services.AddInMemoryRateLimiting();
  11. // 注入限流配置文件服务
  12. builder.Services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
  13. var app = builder.Build();
  14. //启用IPRateLimitPolicies
  15. var ipPolicyStore = app.Services.GetRequiredService<IIpPolicyStore>();
  16. await ipPolicyStore.SeedAsync();
  17. //使用Ip限流中间件
  18. app.UseIpRateLimiting();
  19. app.MapGet("/test00", () =>
  20. {
  21. return "get test00 ok";
  22. });
  23. app.MapGet("/test01", () =>
  24. {
  25. return "get test01 ok";
  26. });
  27. app.MapGet("/test02", () =>
  28. {
  29. return "get test02 ok";
  30. });
  31. app.MapPost("/test02", () =>
  32. {
  33. return "post test02 ok";
  34. });
  35. app.MapGet("/test03", () =>
  36. {
  37. return "get test01 ok";
  38. });
  39. app.Run();

appsettings.json

  1. {
  2. "Logging": {
  3. "LogLevel": {
  4. "Default": "Information",
  5. "Microsoft.AspNetCore": "Warning"
  6. }
  7. },
  8. "AllowedHosts": "*",
  9. "IpRateLimiting": {
  10. "EnableEndpointRateLimiting": false,
  11. "StackBlockedRequests": false,
  12. "RealIpHeader": "X-Real-IP",
  13. "IpWhitelist": [ "127.0.0.1"],
  14. "ClientIdHeader": "X-ClientId",
  15. "HttpStatusCode": 429,
  16. "EndpointWhitelist": [ "get:/test00", "*:/test01" ],
  17. "ClientWhitelist": [ "dev-id-1", "dev-id-2" ],
  18. "GeneralRules": [
  19. {
  20. "Endpoint": "*",
  21. "Period": "5s",
  22. "Limit": 2
  23. },
  24. {
  25. "Endpoint": "*",
  26. "Period": "10s",
  27. "Limit": 3
  28. }
  29. ]
  30. },
  31. "IpRateLimitPolicies": {
  32. "IpRules": [
  33. {
  34. "Ip": "127.0.0.2",
  35. "Rules": [
  36. {
  37. "Endpoint": "*",
  38. "Period": "4s",
  39. "Limit": 1
  40. },
  41. {
  42. "Endpoint": "*",
  43. "Period": "15m",
  44. "Limit": 200
  45. }
  46. ]
  47. }
  48. ]
  49. }
  50. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/788446
推荐阅读
相关标签
  

闽ICP备14008679号