您好,登錄后才能下訂單哦!
使用Asp.NET怎么實(shí)現(xiàn)一個(gè)限流控制功能?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
AspNetCoreRateLimit 是一個(gè)ASP.NET Core速率限制的解決方案,旨在控制客戶端根據(jù)IP地址或客戶端ID向Web API或MVC應(yīng)用發(fā)出的請求的速率。AspNetCoreRateLimit包含一個(gè) IpRateLimitMiddleware 和 ClientRateLimitMiddleware ,每個(gè)中間件可以根據(jù)不同的場景配置限制允許IP或客戶端,自定義這些限制策略,也可以將限制策略應(yīng)用在每個(gè)API URL或具體的HTTP Method上。
由上面介紹可知AspNetCoreRateLimit支持了兩種方式:基于 客戶端IP( IpRateLimitMiddleware) 和客戶端ID( ClientRateLimitMiddleware )速率限制 接下來就分別說明使用方式
添加Nuget包引用:
Install-Package AspNetCoreRateLimit
1、修改Startup.cs中方法:
public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; }// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //需要從加載配置文件appsettings.json services.AddOptions(); //需要存儲(chǔ)速率限制計(jì)算器和ip規(guī)則 services.AddMemoryCache(); //從appsettings.json中加載常規(guī)配置,IpRateLimiting與配置文件中節(jié)點(diǎn)對應(yīng) services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting")); //從appsettings.json中加載Ip規(guī)則 services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies")); //注入計(jì)數(shù)器和規(guī)則存儲(chǔ) services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>(); services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>(); services.AddControllers(); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); //配置(解析器、計(jì)數(shù)器密鑰生成器) services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>(); //Other Code } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //Other Code app.UseRouting(); app.UseAuthorization(); //啟用客戶端IP限制速率 app.UseIpRateLimiting(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } }
2、在appsettings.json中添加通用配置項(xiàng)節(jié)點(diǎn):(IpRateLimiting節(jié)點(diǎn)與Startup中取的節(jié)點(diǎn)對應(yīng))
"IpRateLimiting": { //false,則全局將應(yīng)用限制,并且僅應(yīng)用具有作為端點(diǎn)的規(guī)則*。例如,如果您設(shè)置每秒5次調(diào)用的限制,則對任何端點(diǎn)的任何HTTP調(diào)用都將計(jì)入該限制 //true, 則限制將應(yīng)用于每個(gè)端點(diǎn),如{HTTP_Verb}{PATH}。例如,如果您為*:/api/values客戶端設(shè)置每秒5個(gè)呼叫的限制, "EnableEndpointRateLimiting": false, //false,拒絕的API調(diào)用不會(huì)添加到調(diào)用次數(shù)計(jì)數(shù)器上;如 客戶端每秒發(fā)出3個(gè)請求并且您設(shè)置了每秒一個(gè)調(diào)用的限制,則每分鐘或每天計(jì)數(shù)器等其他限制將僅記錄第一個(gè)調(diào)用,即成功的API調(diào)用。如果您希望被拒絕的API調(diào)用計(jì)入其他時(shí)間的顯示(分鐘,小時(shí)等) //,則必須設(shè)置StackBlockedRequests為true。 "StackBlockedRequests": false, //Kestrel 服務(wù)器背后是一個(gè)反向代理,如果你的代理服務(wù)器使用不同的頁眉然后提取客戶端IP X-Real-IP使用此選項(xiàng)來設(shè)置 "RealIpHeader": "X-Real-IP", //取白名單的客戶端ID。如果此標(biāo)頭中存在客戶端ID并且與ClientWhitelist中指定的值匹配,則不應(yīng)用速率限制。 "ClientIdHeader": "X-ClientId", //限制狀態(tài)碼 "HttpStatusCode": 429, ////IP白名單:支持Ip v4和v6 //"IpWhitelist": [ "127.0.0.1", "::1/10", "192.168.0.0/24" ], ////端點(diǎn)白名單 //"EndpointWhitelist": [ "get:/api/license", "*:/api/status" ], ////客戶端白名單 //"ClientWhitelist": [ "dev-id-1", "dev-id-2" ], //通用規(guī)則 "GeneralRules": [ { //端點(diǎn)路徑 "Endpoint": "*", //時(shí)間段,格式:{數(shù)字}{單位};可使用單位:s, m, h, d "Period": "1s", //限制 "Limit": 2 }, //15分鐘只能調(diào)用100次 {"Endpoint": "*","Period": "15m","Limit": 100}, //12H只能調(diào)用1000 {"Endpoint": "*","Period": "12h","Limit": 1000}, //7天只能調(diào)用10000次 {"Endpoint": "*","Period": "7d","Limit": 10000} ] }
配置節(jié)點(diǎn)已添加相應(yīng)注釋信息。
規(guī)則設(shè)置格式:
端點(diǎn)格式: {HTTP_Verb}:{PATH}
,您可以使用asterix符號來定位任何HTTP謂詞。
期間格式: {INT}{PERIOD_TYPE}
,您可以使用以下期間類型之一: s, m, h, d
。
限制格式: {LONG}
3、特點(diǎn)Ip限制規(guī)則設(shè)置,在appsettings.json中添加 IP規(guī)則配置節(jié)點(diǎn)
"IpRateLimitPolicies": { //ip規(guī)則 "IpRules": [ { //IP "Ip": "84.247.85.224", //規(guī)則內(nèi)容 "Rules": [ //1s請求10次 {"Endpoint": "*","Period": "1s","Limit": 10}, //15分鐘請求200次 {"Endpoint": "*","Period": "15m","Limit": 200} ] }, { //ip支持設(shè)置多個(gè) "Ip": "192.168.3.22/25", "Rules": [ //1秒請求5次 {"Endpoint": "*","Period": "1s","Limit": 5}, //15分鐘請求150次 {"Endpoint": "*","Period": "15m","Limit": 150}, //12小時(shí)請求500次 {"Endpoint": "*","Period": "12h","Limit": 500} ] } ] }
1、修改Startup文件:
public void ConfigureServices(IServiceCollection services) { //需要從加載配置文件appsettings.json services.AddOptions(); //需要存儲(chǔ)速率限制計(jì)算器和ip規(guī)則 services.AddMemoryCache(); //從appsettings.json中加載常規(guī)配置 services.Configure<ClientRateLimitOptions>(Configuration.GetSection("IPRateLimiting")); //從appsettings.json中加載客戶端規(guī)則 services.Configure<ClientRateLimitPolicies>(Configuration.GetSection("ClientRateLimitPolicies")); //注入計(jì)數(shù)器和規(guī)則存儲(chǔ) services.AddSingleton<IClientPolicyStore, MemoryCacheClientPolicyStore>(); services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>(); services.AddControllers(); // https://github.com/aspnet/Hosting/issues/793 // the IHttpContextAccessor service is not registered by default. //注入計(jì)數(shù)器和規(guī)則存儲(chǔ) services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); //配置(解析器、計(jì)數(shù)器密鑰生成器) services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { //啟用客戶端限制 app.UseClientRateLimiting(); app.UseMvc(); }
2、通用配置采用IP限制相同配置,添加客戶端限制配置:
//客戶端限制設(shè)置 "ClientRateLimitPolicies": { "ClientRules": [ { //客戶端id "ClientId": "client-id-1", "Rules": [ {"Endpoint": "*","Period": "1s","Limit": 10}, {"Endpoint": "*","Period": "15m","Limit": 200} ] }, { "ClientId": "client-id-2", "Rules": [ {"Endpoint": "*","Period": "1s","Limit": 5}, {"Endpoint": "*","Period": "15m","Limit": 150}, {"Endpoint": "*","Period": "12h","Limit": 500} ] } ] }
3、調(diào)用結(jié)果:
設(shè)置規(guī)則:1s只能調(diào)用一次:首次調(diào)用
調(diào)用第二次:自定義返回內(nèi)容
添加 IpRateLimitController控制器:
/// <summary> /// IP限制控制器 /// </summary> [Route("api/[controller]")] [ApiController] public class IpRateLimitController : ControllerBase { private readonly IpRateLimitOptions _options; private readonly IIpPolicyStore _ipPolicyStore; /// <summary> /// /// </summary> /// <param name="optionsAccessor"></param> /// <param name="ipPolicyStore"></param> public IpRateLimitController(IOptions<IpRateLimitOptions> optionsAccessor, IIpPolicyStore ipPolicyStore) { _options = optionsAccessor.Value; _ipPolicyStore = ipPolicyStore; } /// <summary> /// 獲取限制規(guī)則 /// </summary> /// <returns></returns> [HttpGet] public async Task<IpRateLimitPolicies> Get() { return await _ipPolicyStore.GetAsync(_options.IpPolicyPrefix); } /// <summary> /// /// </summary> [HttpPost] public async Task Post(IpRateLimitPolicy ipRate) { var pol = await _ipPolicyStore.GetAsync(_options.IpPolicyPrefix); if (ipRate != null) { pol.IpRules.Add(ipRate); await _ipPolicyStore.SetAsync(_options.IpPolicyPrefix, pol); } } }
分布式部署時(shí),需要將速率限制計(jì)算器和ip規(guī)則存儲(chǔ)到分布式緩存中如Redis
修改注入對象
// inject counter and rules distributed cache stores services.AddSingleton<IClientPolicyStore, DistributedCacheClientPolicyStore>(); services.AddSingleton<IRateLimitCounterStore,DistributedCacheRateLimitCounterStore>();
添加Nuget包 Microsoft.Extensions.Caching.StackExchangeRedis
在Startup中設(shè)置Redis連接
services.AddStackExchangeRedisCache(options => { options.ConfigurationOptions = new ConfigurationOptions { //silently retry in the background if the Redis connection is temporarily down AbortOnConnectFail = false }; options.Configuration = "localhost:6379"; options.InstanceName = "AspNetRateLimit"; });
限制時(shí)自定義相應(yīng)結(jié)果:
//請求返回 "QuotaExceededResponse": { "Content": "{{\"code\":429,\"msg\":\"Visit too frequently, please try again later\",\"data\":null}}", "ContentType": "application/json;utf-8", "StatusCode": 429 },
調(diào)用時(shí)返回結(jié)果:
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。