From 67496fddfc16105ee4088721836b6ce7d3076a0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=81=B0=E7=81=B0?= Date: Fri, 11 Nov 2022 02:58:16 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=B9=B6=E5=8F=91=E9=99=90=E5=88=B6=E4=B8=AD=E9=97=B4?= =?UTF-8?q?=E4=BB=B6=E3=80=90Microsoft.AspNetCore.ConcurrencyLimiter?= =?UTF-8?q?=E3=80=91=EF=BC=8C=E9=99=90=E5=88=B6=E5=B9=B6=E5=8F=91=E5=A4=84?= =?UTF-8?q?=E7=90=86=EF=BC=8C=E8=AF=B7=E7=BB=93=E5=90=88=E9=99=90=E6=B5=81?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=90=88=E7=90=86=E9=85=8D=E7=BD=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AppSettingsConstVars.cs | 16 ++++++++-------- .../CoreCms.Net.Web.Admin.csproj | 1 + CoreCms.Net.Web.Admin/Program.cs | 14 ++++++++++++-- CoreCms.Net.Web.Admin/appsettings.json | 6 ++++++ .../CoreCms.Net.Web.WebApi.csproj | 1 + CoreCms.Net.Web.WebApi/Program.cs | 13 +++++++++++-- CoreCms.Net.Web.WebApi/appsettings.json | 6 ++++++ 7 files changed, 45 insertions(+), 12 deletions(-) diff --git a/CoreCms.Net.Configuration/AppSettingsConstVars.cs b/CoreCms.Net.Configuration/AppSettingsConstVars.cs index 13c5771a..826c70a8 100644 --- a/CoreCms.Net.Configuration/AppSettingsConstVars.cs +++ b/CoreCms.Net.Configuration/AppSettingsConstVars.cs @@ -24,7 +24,6 @@ namespace CoreCms.Net.Configuration public static readonly string AppConfigAppInterFaceUrl = AppSettingsHelper.GetContent("AppConfig", "AppInterFaceUrl"); #endregion - #region 数据库================================================================================ /// /// 获取数据库连接字符串 @@ -36,7 +35,6 @@ namespace CoreCms.Net.Configuration public static readonly string DbDbType = AppSettingsHelper.GetContent("ConnectionStrings", "DbType"); #endregion - #region redis================================================================================ /// @@ -55,7 +53,6 @@ namespace CoreCms.Net.Configuration #endregion - #region AOP================================================================================ /// /// 事务切面开关 @@ -71,14 +68,12 @@ namespace CoreCms.Net.Configuration public static readonly string JwtConfigAudience = AppSettingsHelper.GetContent("JwtConfig", "Audience"); #endregion - #region Cors跨域设置================================================================================ public static readonly string CorsPolicyName = AppSettingsHelper.GetContent("Cors", "PolicyName"); public static readonly bool CorsEnableAllIPs = AppSettingsHelper.GetContent("Cors", "EnableAllIPs").ObjToBool(); public static readonly string CorsIPs = AppSettingsHelper.GetContent("Cors", "IPs"); #endregion - #region Middleware中间件================================================================================ /// /// 是否记录ip信息 @@ -108,8 +103,14 @@ namespace CoreCms.Net.Configuration /// 是否开启记录到数据库模式 /// public static readonly bool MiddlewareRecordAccessLogsEnabledDbMode = AppSettingsHelper.GetContent("Middleware", "RecordAccessLogs", "EnabledDbMode").ObjToBool(); - - + /// + /// 并发限制(最大并发请求数) + /// + public static readonly int MiddlewareConcurrencyLimiterMaxConcurrentRequests = AppSettingsHelper.GetContent("Middleware", "ConcurrencyLimiter", "MaxConcurrentRequests").ObjToInt(100); + /// + /// 并发限制(最大请求数) + /// + public static readonly int MiddlewareConcurrencyLimiterRequestQueueLimit = AppSettingsHelper.GetContent("Middleware", "ConcurrencyLimiter", "RequestQueueLimit").ObjToInt(100); #endregion #region 支付================================================================================ @@ -145,7 +146,6 @@ namespace CoreCms.Net.Configuration #endregion - #region 微信配置================================================================================ /// diff --git a/CoreCms.Net.Web.Admin/CoreCms.Net.Web.Admin.csproj b/CoreCms.Net.Web.Admin/CoreCms.Net.Web.Admin.csproj index a6a67786..daa41312 100644 --- a/CoreCms.Net.Web.Admin/CoreCms.Net.Web.Admin.csproj +++ b/CoreCms.Net.Web.Admin/CoreCms.Net.Web.Admin.csproj @@ -49,6 +49,7 @@ + diff --git a/CoreCms.Net.Web.Admin/Program.cs b/CoreCms.Net.Web.Admin/Program.cs index 2021abf3..ffa9c00d 100644 --- a/CoreCms.Net.Web.Admin/Program.cs +++ b/CoreCms.Net.Web.Admin/Program.cs @@ -84,6 +84,15 @@ builder.Services.AddHttpContextSetup(); //服务配置中加入AutoFac控制器替换规则。 builder.Services.Replace(ServiceDescriptor.Transient()); +//并发限制-使用队列策略模式 +builder.Services.AddQueuePolicy(options => +{ + //最大并发请求数,超过之后,进行排队 + options.MaxConcurrentRequests = AppSettingsConstVars.MiddlewareConcurrencyLimiterMaxConcurrentRequests; + //最大请求数,超过之后,返回503 + options.RequestQueueLimit = AppSettingsConstVars.MiddlewareConcurrencyLimiterRequestQueueLimit; +}); + //注册mvc,注册razor引擎视图 builder.Services.AddMvc(options => { @@ -151,6 +160,7 @@ builder.Host.UseNLog(); var app = builder.Build(); + #region 解决Ubuntu Nginx 代理不能获取IP问题=================================================================== app.UseForwardedHeaders(new ForwardedHeadersOptions { @@ -159,7 +169,8 @@ app.UseForwardedHeaders(new ForwardedHeadersOptions #endregion #region 中间件注册=================================================================== - +// 启用并发限制数中间件 +app.UseConcurrencyLimiter(); // 开启Ip限流 app.UseIpLimitMiddle(); // 记录请求与返回数据 (注意开启权限,不然本地无法写入) @@ -168,7 +179,6 @@ app.UseRequestResponseLog(); app.UseRecordAccessLogsMildd(GlobalEnumVars.CoreShopSystemCategory.Admin.ToString()); // 记录ip请求 (注意开启权限,不然本地无法写入) app.UseIpLogMildd(); - #endregion app.UseSwagger().UseSwaggerUI(c => diff --git a/CoreCms.Net.Web.Admin/appsettings.json b/CoreCms.Net.Web.Admin/appsettings.json index 46964560..1e495d66 100644 --- a/CoreCms.Net.Web.Admin/appsettings.json +++ b/CoreCms.Net.Web.Admin/appsettings.json @@ -59,6 +59,12 @@ //开启Ip限流 "IpRateLimit": { "Enabled": false + }, + "ConcurrencyLimiter": { + //最大并发请求数 + "MaxConcurrentRequests": 100, + //最大请求数 + "RequestQueueLimit": 100 } }, //ip限流规则设置 diff --git a/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.csproj b/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.csproj index 39a8e7bf..2de72fc5 100644 --- a/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.csproj +++ b/CoreCms.Net.Web.WebApi/CoreCms.Net.Web.WebApi.csproj @@ -57,6 +57,7 @@ + diff --git a/CoreCms.Net.Web.WebApi/Program.cs b/CoreCms.Net.Web.WebApi/Program.cs index ed27b477..4aa56886 100644 --- a/CoreCms.Net.Web.WebApi/Program.cs +++ b/CoreCms.Net.Web.WebApi/Program.cs @@ -95,6 +95,15 @@ builder.Services.AddHttpContextSetup(); //服务配置中加入AutoFac控制器替换规则。 builder.Services.Replace(ServiceDescriptor.Transient()); +//并发限制-使用队列策略模式 +builder.Services.AddQueuePolicy(options => +{ + //最大并发请求数,超过之后,进行排队 + options.MaxConcurrentRequests = AppSettingsConstVars.MiddlewareConcurrencyLimiterMaxConcurrentRequests; + //最大请求数,超过之后,返回503 + options.RequestQueueLimit = AppSettingsConstVars.MiddlewareConcurrencyLimiterRequestQueueLimit; +}); + //注册mvc,注册razor引擎视图 builder.Services.AddMvc(options => { @@ -170,7 +179,8 @@ app.UseForwardedHeaders(new ForwardedHeadersOptions #endregion #region 中间件注册=================================================================== - +// 启用并发限制数中间件 +app.UseConcurrencyLimiter(); // 开启Ip限流 app.UseIpLimitMiddle(); // 记录请求与返回数据 (注意开启权限,不然本地无法写入) @@ -179,7 +189,6 @@ app.UseRequestResponseLog(); app.UseRecordAccessLogsMildd(GlobalEnumVars.CoreShopSystemCategory.Api.ToString()); // 记录ip请求 (注意开启权限,不然本地无法写入) app.UseIpLogMildd(); - #endregion //强制显示中文 diff --git a/CoreCms.Net.Web.WebApi/appsettings.json b/CoreCms.Net.Web.WebApi/appsettings.json index 46964560..1e495d66 100644 --- a/CoreCms.Net.Web.WebApi/appsettings.json +++ b/CoreCms.Net.Web.WebApi/appsettings.json @@ -59,6 +59,12 @@ //开启Ip限流 "IpRateLimit": { "Enabled": false + }, + "ConcurrencyLimiter": { + //最大并发请求数 + "MaxConcurrentRequests": 100, + //最大请求数 + "RequestQueueLimit": 100 } }, //ip限流规则设置