diff --git a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/AbpProAspNetCoreConsts.cs b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/AbpProAspNetCoreConsts.cs index afaa93ab..cfde401b 100644 --- a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/AbpProAspNetCoreConsts.cs +++ b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/AbpProAspNetCoreConsts.cs @@ -13,9 +13,9 @@ public class AbpProAspNetCoreConsts public const string DefaultCookieName = "Lion.AbpPro.Http.Api"; /// - /// 网关配置节名称 + /// Consul网关配置节名称 /// - public const string Gateway = "Gateway"; + public const string Consul = "Consul"; /// /// 跨域配置节名称 diff --git a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/AbpProAspNetCoreModule.cs b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/AbpProAspNetCoreModule.cs index 03603a2f..b0e78a10 100644 --- a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/AbpProAspNetCoreModule.cs +++ b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/AbpProAspNetCoreModule.cs @@ -6,7 +6,7 @@ public class AbpProAspNetCoreModule : AbpModule { public override void PreConfigureServices(ServiceConfigurationContext context) { - context.Services.Configure(context.Configuration.GetSection(AbpProAspNetCoreConsts.Gateway)); + context.Services.Configure(context.Configuration.GetSection(AbpProAspNetCoreConsts.Consul)); context.Services.Configure(context.Configuration.GetSection(AbpProAspNetCoreConsts.Cors)); context.Services.Configure(context.Configuration.GetSection(AbpProAspNetCoreConsts.MiniProfiler)); context.Services.Configure(context.Configuration.GetSection(AbpProAspNetCoreConsts.MultiTenancy)); diff --git a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/Options/AbpProConsulOptions.cs b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/Options/AbpProConsulOptions.cs new file mode 100644 index 00000000..7a132a2c --- /dev/null +++ b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/Options/AbpProConsulOptions.cs @@ -0,0 +1,49 @@ +namespace Lion.AbpPro.AspNetCore.Options; + +public class AbpProConsulOptions +{ + /// + /// 是否启用 + /// + public bool Enabled { get; set; } + + /// + /// consul服务地址 + /// + public string ServiceUrl { get; set; } + + /// + /// 服务名称 + /// + public string ClientName { get; set; } + + /// + /// 服务地址,不带端口 + /// + public string ClientAddress { get; set; } + + /// + /// 服务端口 + /// + public int ClientPort { get; set; } + + /// + /// 健康检查地址 + /// + public string HealthUrl { get; set; } + + /// + /// 服务停止多久后注销,单位秒 + /// + public int DeregisterCriticalServiceAfter { get; set; } + + /// + /// 健康检查时间间隔,或者称为心跳 间隔,单位秒 + /// + public int Interval { get; set; } + + /// + /// 超时时间,单位秒 + /// + public int Timeout { get; set; } +} \ No newline at end of file diff --git a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/Options/AbpProGatewayOptions.cs b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/Options/AbpProGatewayOptions.cs deleted file mode 100644 index 784485ca..00000000 --- a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/Options/AbpProGatewayOptions.cs +++ /dev/null @@ -1,24 +0,0 @@ -namespace Lion.AbpPro.AspNetCore.Options; - -public class AbpProGatewayOptions -{ - /// - /// 是否启用 - /// - public bool Enabled { get; set; } - - /// - /// consul服务地址 - /// - public string ConsulServiceUrl { get; set; } - - /// - /// consul服务名称 - /// - public string ServiceName { get; set; } - - /// - /// 健康检查地址 - /// - public string HealthUrl { get; set; } -} \ No newline at end of file diff --git a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Microsoft/AspNetCore/Builder/ApplicationBuilderExtensions.cs b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Microsoft/AspNetCore/Builder/ApplicationBuilderExtensions.cs index 2a626b2f..4f128411 100644 --- a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Microsoft/AspNetCore/Builder/ApplicationBuilderExtensions.cs +++ b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Microsoft/AspNetCore/Builder/ApplicationBuilderExtensions.cs @@ -7,42 +7,38 @@ using System.Threading.Tasks; using Lion.AbpPro.AspNetCore.Options; using Microsoft.AspNetCore.Localization; using Swashbuckle.AspNetCore.SwaggerUI; +using Volo.Abp.Guids; namespace Microsoft.AspNetCore.Builder; public static class ApplicationBuilderExtensions { - - /// /// consul服务 /// public static IApplicationBuilder UseAbpProConsul(this IApplicationBuilder app) { // 获取 AbpProGatewayOptions 配置 - var consulOptions = app.ApplicationServices.GetRequiredService>().Value; + var consulOptions = app.ApplicationServices.GetRequiredService>().Value; if (!consulOptions.Enabled) return app; var appLifetime = app.ApplicationServices.GetService(); using var scope = app.ApplicationServices.CreateScope(); - var clock = scope.ServiceProvider.GetService(); - var appUrl = new Uri(consulOptions.ConsulServiceUrl, UriKind.Absolute); - - var client = scope.ServiceProvider.GetService(); - + var client = scope.ServiceProvider.GetRequiredService(); + var guidGenerator = scope.ServiceProvider.GetRequiredService(); var consulServiceRegistration = new AgentServiceRegistration { - Name = consulOptions.ServiceName, - ID = $"{consulOptions.ServiceName}:{clock.Now:yyyyMMddHHmmssfff}", - Address = appUrl.Host, - Port = appUrl.Port, + Name = consulOptions.ClientName, + ID = guidGenerator.Create().ToString(), + Address = consulOptions.ClientAddress, + Port = consulOptions.ClientPort, Check = new AgentServiceCheck { - DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(5), //服务停止多久后注销 - Interval = TimeSpan.FromSeconds(3), //健康检查时间间隔,或者称为心跳 间隔 + DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(consulOptions.DeregisterCriticalServiceAfter), //服务停止多久后注销 + Interval = TimeSpan.FromSeconds(consulOptions.Interval), //健康检查时间间隔,或者称为心跳 间隔 HTTP = consulOptions.HealthUrl, //健康检查地址 - Timeout = TimeSpan.FromSeconds(15) //超时时间 + Timeout = TimeSpan.FromSeconds(consulOptions.Timeout) //超时时间 } }; @@ -57,7 +53,7 @@ public static class ApplicationBuilderExtensions public static IApplicationBuilder UseAbpProOcelot(this IApplicationBuilder app) { // 获取 AbpProGatewayOptions 配置 - var consulOptions = app.ApplicationServices.GetRequiredService>().Value; + var consulOptions = app.ApplicationServices.GetRequiredService>().Value; if (!consulOptions.Enabled) return app; app.UseOcelot().Wait(); diff --git a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Microsoft/Extensions/DependencyInjection/ServiceCollectionExtensions.cs b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Microsoft/Extensions/DependencyInjection/ServiceCollectionExtensions.cs index 36d3103e..4fe290a9 100644 --- a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Microsoft/Extensions/DependencyInjection/ServiceCollectionExtensions.cs +++ b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Microsoft/Extensions/DependencyInjection/ServiceCollectionExtensions.cs @@ -30,11 +30,11 @@ public static class ServiceCollectionExtensions public static IServiceCollection AddAbpProConsul(this IServiceCollection service) { - var consulOptions = service.BuildServiceProvider().GetRequiredService>().Value; + var consulOptions = service.BuildServiceProvider().GetRequiredService>().Value; if (!consulOptions.Enabled) return service; - service.AddSingleton(p => new ConsulClient(config => { config.Address = new Uri(consulOptions.ConsulServiceUrl); })); + service.AddSingleton(p => new ConsulClient(config => { config.Address = new Uri(consulOptions.ServiceUrl); })); return service; } diff --git a/aspnet-core/frameworks/src/Lion.AbpPro.Starter/Lion/AbpPro/Starter/PreheatAbpProStarterContributor.cs b/aspnet-core/frameworks/src/Lion.AbpPro.Starter/Lion/AbpPro/Starter/PreheatAbpProStarterContributor.cs index 897dd06e..fe215aa3 100644 --- a/aspnet-core/frameworks/src/Lion.AbpPro.Starter/Lion/AbpPro/Starter/PreheatAbpProStarterContributor.cs +++ b/aspnet-core/frameworks/src/Lion.AbpPro.Starter/Lion/AbpPro/Starter/PreheatAbpProStarterContributor.cs @@ -26,6 +26,7 @@ public class PreheatAbpProStarterContributor : IAbpProStarterContributor, ITrans try { _logger.LogInformation($"开始预热:{_options.RequestUrl}"); + await Task.Delay(TimeSpan.FromSeconds(30)); await _httpClientFactory.CreateClient().GetAsync(_options.RequestUrl); _logger.LogInformation($"预热成功"); } diff --git a/aspnet-core/gateways/Lion.AbpPro.WebGateway/AbpProWebGatewayModule.cs b/aspnet-core/gateways/Lion.AbpPro.WebGateway/AbpProWebGatewayModule.cs index 254c8d09..8311391c 100644 --- a/aspnet-core/gateways/Lion.AbpPro.WebGateway/AbpProWebGatewayModule.cs +++ b/aspnet-core/gateways/Lion.AbpPro.WebGateway/AbpProWebGatewayModule.cs @@ -1,55 +1,25 @@ using Lion.AbpPro.AspNetCore; -namespace Lion.AbpPro.WebGateway +namespace Lion.AbpPro.WebGateway; + +[DependsOn( + typeof(AbpAspNetCoreMvcModule), + typeof(AbpProAspNetCoreModule))] +public class AbpProWebGatewayModule : AbpModule { - [DependsOn( - typeof(AbpProAspNetCoreModule))] - public class AbpProWebGatewayModule : AbpModule + public override void ConfigureServices(ServiceConfigurationContext context) { - private const string DefaultCorsPolicyName = "Default"; - - public override void ConfigureServices(ServiceConfigurationContext context) - { - ConfigureCors(context); - - } - - public override void OnApplicationInitialization(ApplicationInitializationContext context) - { - var app = context.GetApplicationBuilder(); - app.UseCorrelationId(); - app.UseCors(DefaultCorsPolicyName); - app.UseRouting(); - app.UseConfiguredEndpoints(endpoints => { endpoints.MapHealthChecks("/health"); }); - app.UseWebSockets(); - app.UseOcelot().Wait(); - } + context.Services.AddAbpProHealthChecks(); + context.Services.AddAbpProOcelot(); + } - /// - /// 配置跨域 - /// - private void ConfigureCors(ServiceConfigurationContext context) - { - var configuration = context.Services.GetConfiguration(); - context.Services.AddCors(options => - { - options.AddPolicy(DefaultCorsPolicyName, builder => - { - builder - .WithOrigins( - configuration["App:CorsOrigins"] - .Split(",", StringSplitOptions.RemoveEmptyEntries) - .Select(o => o.RemovePostFix("/")) - .ToArray() - ) - .WithAbpExposedHeaders() - .SetIsOriginAllowedToAllowWildcardSubdomains() - .AllowAnyHeader() - .AllowAnyMethod() - .AllowCredentials(); - }); - }); - } - + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + var app = context.GetApplicationBuilder(); + app.UseCorrelationId(); + app.UseRouting(); + app.UseConfiguredEndpoints(endpoints => { endpoints.MapHealthChecks("/health"); }); + app.UseWebSockets(); + app.UseOcelot().Wait(); } } \ No newline at end of file diff --git a/aspnet-core/gateways/Lion.AbpPro.WebGateway/GlobalUsings.cs b/aspnet-core/gateways/Lion.AbpPro.WebGateway/GlobalUsings.cs index dc4fb4e4..96ec3898 100644 --- a/aspnet-core/gateways/Lion.AbpPro.WebGateway/GlobalUsings.cs +++ b/aspnet-core/gateways/Lion.AbpPro.WebGateway/GlobalUsings.cs @@ -15,4 +15,5 @@ global using Microsoft.Extensions.Hosting; global using Microsoft.Extensions.Logging; global using Ocelot.Middleware; global using Volo.Abp; +global using Volo.Abp.AspNetCore.Mvc; global using Volo.Abp.Modularity; \ No newline at end of file diff --git a/aspnet-core/gateways/Lion.AbpPro.WebGateway/Lion.AbpPro.WebGateway.csproj b/aspnet-core/gateways/Lion.AbpPro.WebGateway/Lion.AbpPro.WebGateway.csproj index aa8a69ca..b4e1981a 100644 --- a/aspnet-core/gateways/Lion.AbpPro.WebGateway/Lion.AbpPro.WebGateway.csproj +++ b/aspnet-core/gateways/Lion.AbpPro.WebGateway/Lion.AbpPro.WebGateway.csproj @@ -3,7 +3,6 @@ net9.0 - diff --git a/aspnet-core/gateways/Lion.AbpPro.WebGateway/Program.cs b/aspnet-core/gateways/Lion.AbpPro.WebGateway/Program.cs index 3c1a2404..99b1c2a3 100644 --- a/aspnet-core/gateways/Lion.AbpPro.WebGateway/Program.cs +++ b/aspnet-core/gateways/Lion.AbpPro.WebGateway/Program.cs @@ -1,16 +1,14 @@ -namespace Lion.AbpPro.WebGateway +namespace Lion.AbpPro.WebGateway; + +public class Program { - public class Program + public static async Task Main(string[] args) { - public static async Task Main(string[] args) - { - var builder = WebApplication.CreateBuilder(args); - builder.Host - .UseAutofac(); - await builder.AddApplicationAsync(); - var app = builder.Build(); - await app.InitializeApplicationAsync(); - await app.RunAsync(); - } + var builder = WebApplication.CreateBuilder(args); + builder.Host .UseAutofac(); + await builder.AddApplicationAsync(); + var app = builder.Build(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); } } \ No newline at end of file diff --git a/aspnet-core/gateways/Lion.AbpPro.WebGateway/Properties/launchSettings.json b/aspnet-core/gateways/Lion.AbpPro.WebGateway/Properties/launchSettings.json index 158fe54b..47b3d773 100644 --- a/aspnet-core/gateways/Lion.AbpPro.WebGateway/Properties/launchSettings.json +++ b/aspnet-core/gateways/Lion.AbpPro.WebGateway/Properties/launchSettings.json @@ -7,7 +7,7 @@ "launchUrl": "swagger", "applicationUrl": "http://localhost:44314", "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" + "ASPNETCORE_ENVIRONMENT": "Consul" } } } diff --git a/aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.Consul.json b/aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.Consul.json index 57991d1e..030733b4 100644 --- a/aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.Consul.json +++ b/aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.Consul.json @@ -7,17 +7,13 @@ } }, "AllowedHosts": "*", - "App": { - "SelfUrl": "http://120.24.194.14:8014", - "CorsOrigins": "http://localhost:4200,http://120.24.194.14:8012,http://120.24.194.14:8011,http://120.24.194.14:8013" - }, "GlobalConfiguration": { }, "Routes": [ { "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "http", - "ServiceName": "AbpPro-Service", + "ServiceName": "Lion.AbpPro.Api", "LoadBalancerOptions": { "Type": "LeastConnection" }, @@ -32,7 +28,7 @@ { "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "ws", - "ServiceName": "AbpPro-Service", + "ServiceName": "Lion.AbpPro.Api", "LoadBalancerOptions": { "Type": "LeastConnection" }, diff --git a/aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.Production.json b/aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.Production.json index 1c1d6e95..030733b4 100644 --- a/aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.Production.json +++ b/aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.Production.json @@ -7,22 +7,16 @@ } }, "AllowedHosts": "*", - "App": { - "SelfUrl": "http://localhost:44314", - "CorsOrigins": "http://*:*,https://*:*" - }, "GlobalConfiguration": { }, "Routes": [ { "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "http", - "DownstreamHostAndPorts": [ - { - "Host": "localhost", - "Port": 8080 - } - ], + "ServiceName": "Lion.AbpPro.Api", + "LoadBalancerOptions": { + "Type": "LeastConnection" + }, "UpstreamPathTemplate": "/gateway/{url}", "UpstreamHttpMethod": [ "Get", @@ -34,12 +28,10 @@ { "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "ws", - "DownstreamHostAndPorts": [ - { - "Host": "localhost", - "Port": 8080 - } - ], + "ServiceName": "Lion.AbpPro.Api", + "LoadBalancerOptions": { + "Type": "LeastConnection" + }, "UpstreamPathTemplate": "/ws/{url}", "UpstreamHttpMethod": [ "Get", @@ -49,4 +41,4 @@ ] } ] -} \ No newline at end of file +} diff --git a/aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.json b/aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.json index 11ae9e2d..030733b4 100644 --- a/aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.json +++ b/aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.json @@ -7,22 +7,16 @@ } }, "AllowedHosts": "*", - "App": { - "SelfUrl": "http://localhost:44314", - "CorsOrigins": "http://localhost:4200" - }, "GlobalConfiguration": { }, "Routes": [ { "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "http", - "DownstreamHostAndPorts": [ - { - "Host": "localhost", - "Port": 44315 - } - ], + "ServiceName": "Lion.AbpPro.Api", + "LoadBalancerOptions": { + "Type": "LeastConnection" + }, "UpstreamPathTemplate": "/gateway/{url}", "UpstreamHttpMethod": [ "Get", @@ -34,12 +28,10 @@ { "DownstreamPathTemplate": "/{url}", "DownstreamScheme": "ws", - "DownstreamHostAndPorts": [ - { - "Host": "localhost", - "Port": 44315 - } - ], + "ServiceName": "Lion.AbpPro.Api", + "LoadBalancerOptions": { + "Type": "LeastConnection" + }, "UpstreamPathTemplate": "/ws/{url}", "UpstreamHttpMethod": [ "Get", diff --git a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs index 3c99663a..c92bd3c9 100644 --- a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs +++ b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs @@ -39,6 +39,7 @@ public partial class AbpProHttpApiHostModule : AbpModule .AddAbpProTenantResolvers() .AddAbpProLocalization() .AddAbpProExceptions() + .AddAbpProConsul() .AddAbpProSwagger("AbpPro"); } diff --git a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json index 7ffad74a..e1222c38 100644 --- a/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json +++ b/aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json @@ -89,8 +89,16 @@ "Enabled": true, "CorsOrigins": "http://localhost:4200,http://localhost:4201" }, - "Gateway": { - "Enabled": false + "Consul": { + "Enabled": false, + "ServiceUrl": "http://localhost:8500", + "ClientName": "Lion.AbpPro.Api", + "ClientAddress": "localhost", + "ClientPort": 44315, + "HealthUrl": "http://localhost:44315/health", + "DeregisterCriticalServiceAfter": 30, + "Interval": 30, + "Timeout": 30 }, "MultiTenancy": { "Enabled": true