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限流规则设置