【新增】新增并发限制中间件【Microsoft.AspNetCore.ConcurrencyLimiter】,限制并发处理,请结合限流进行合理配置。

This commit is contained in:
大灰灰
2022-11-11 02:58:16 +08:00
parent 2313bf6ac9
commit 67496fddfc
7 changed files with 45 additions and 12 deletions

View File

@@ -24,7 +24,6 @@ namespace CoreCms.Net.Configuration
public static readonly string AppConfigAppInterFaceUrl = AppSettingsHelper.GetContent("AppConfig", "AppInterFaceUrl");
#endregion
#region ================================================================================
/// <summary>
/// 获取数据库连接字符串
@@ -36,7 +35,6 @@ namespace CoreCms.Net.Configuration
public static readonly string DbDbType = AppSettingsHelper.GetContent("ConnectionStrings", "DbType");
#endregion
#region redis================================================================================
/// <summary>
@@ -55,7 +53,6 @@ namespace CoreCms.Net.Configuration
#endregion
#region AOP================================================================================
/// <summary>
/// 事务切面开关
@@ -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中间件================================================================================
/// <summary>
/// 是否记录ip信息
@@ -108,8 +103,14 @@ namespace CoreCms.Net.Configuration
/// 是否开启记录到数据库模式
/// </summary>
public static readonly bool MiddlewareRecordAccessLogsEnabledDbMode = AppSettingsHelper.GetContent("Middleware", "RecordAccessLogs", "EnabledDbMode").ObjToBool();
/// <summary>
/// 并发限制(最大并发请求数)
/// </summary>
public static readonly int MiddlewareConcurrencyLimiterMaxConcurrentRequests = AppSettingsHelper.GetContent("Middleware", "ConcurrencyLimiter", "MaxConcurrentRequests").ObjToInt(100);
/// <summary>
/// 并发限制(最大请求数)
/// </summary>
public static readonly int MiddlewareConcurrencyLimiterRequestQueueLimit = AppSettingsHelper.GetContent("Middleware", "ConcurrencyLimiter", "RequestQueueLimit").ObjToInt(100);
#endregion
#region ================================================================================
@@ -145,7 +146,6 @@ namespace CoreCms.Net.Configuration
#endregion
#region ================================================================================
/// <summary>

View File

@@ -49,6 +49,7 @@
<PackageReference Include="Essensoft.Paylink.Alipay" Version="4.0.14" />
<PackageReference Include="Essensoft.Paylink.WeChatPay" Version="4.0.14" />
<PackageReference Include="IdentityModel" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.ConcurrencyLimiter" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />

View File

@@ -84,6 +84,15 @@ builder.Services.AddHttpContextSetup();
//服务配置中加入AutoFac控制器替换规则。
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
//并发限制-使用队列策略模式
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 =>

View File

@@ -59,6 +59,12 @@
//开启Ip限流
"IpRateLimit": {
"Enabled": false
},
"ConcurrencyLimiter": {
//最大并发请求数
"MaxConcurrentRequests": 100,
//最大请求数
"RequestQueueLimit": 100
}
},
//ip限流规则设置

View File

@@ -57,6 +57,7 @@
<PackageReference Include="Hangfire.Core" Version="1.7.31" />
<PackageReference Include="Hangfire.Dashboard.BasicAuthorization" Version="1.0.2" />
<PackageReference Include="Hangfire.Redis.StackExchange" Version="1.8.6" />
<PackageReference Include="Microsoft.AspNetCore.ConcurrencyLimiter" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.1" />
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />

View File

@@ -95,6 +95,15 @@ builder.Services.AddHttpContextSetup();
//服务配置中加入AutoFac控制器替换规则。
builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
//并发限制-使用队列策略模式
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
//强制显示中文

View File

@@ -59,6 +59,12 @@
//开启Ip限流
"IpRateLimit": {
"Enabled": false
},
"ConcurrencyLimiter": {
//最大并发请求数
"MaxConcurrentRequests": 100,
//最大请求数
"RequestQueueLimit": 100
}
},
//ip限流规则设置