Browse Source

feat: 调整ocelot+consul

pull/148/head 9.3.4.2
zzzwangjun@gmail.com 6 months ago
parent
commit
9b56b3bdde
  1. 4
      aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/AbpProAspNetCoreConsts.cs
  2. 2
      aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/AbpProAspNetCoreModule.cs
  3. 49
      aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/Options/AbpProConsulOptions.cs
  4. 24
      aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/Options/AbpProGatewayOptions.cs
  5. 28
      aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Microsoft/AspNetCore/Builder/ApplicationBuilderExtensions.cs
  6. 4
      aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Microsoft/Extensions/DependencyInjection/ServiceCollectionExtensions.cs
  7. 1
      aspnet-core/frameworks/src/Lion.AbpPro.Starter/Lion/AbpPro/Starter/PreheatAbpProStarterContributor.cs
  8. 40
      aspnet-core/gateways/Lion.AbpPro.WebGateway/AbpProWebGatewayModule.cs
  9. 1
      aspnet-core/gateways/Lion.AbpPro.WebGateway/GlobalUsings.cs
  10. 1
      aspnet-core/gateways/Lion.AbpPro.WebGateway/Lion.AbpPro.WebGateway.csproj
  11. 8
      aspnet-core/gateways/Lion.AbpPro.WebGateway/Program.cs
  12. 2
      aspnet-core/gateways/Lion.AbpPro.WebGateway/Properties/launchSettings.json
  13. 8
      aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.Consul.json
  14. 24
      aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.Production.json
  15. 24
      aspnet-core/gateways/Lion.AbpPro.WebGateway/appsettings.json
  16. 1
      aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/AbpProHttpApiHostModule.cs
  17. 12
      aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.json

4
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";
/// <summary>
/// 网关配置节名称
/// Consul网关配置节名称
/// </summary>
public const string Gateway = "Gateway";
public const string Consul = "Consul";
/// <summary>
/// 跨域配置节名称

2
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<AbpProGatewayOptions>(context.Configuration.GetSection(AbpProAspNetCoreConsts.Gateway));
context.Services.Configure<AbpProConsulOptions>(context.Configuration.GetSection(AbpProAspNetCoreConsts.Consul));
context.Services.Configure<AbpProCorsOptions>(context.Configuration.GetSection(AbpProAspNetCoreConsts.Cors));
context.Services.Configure<AbpProMiniProfilerOptions>(context.Configuration.GetSection(AbpProAspNetCoreConsts.MiniProfiler));
context.Services.Configure<AbpProMultiTenancyOptions>(context.Configuration.GetSection(AbpProAspNetCoreConsts.MultiTenancy));

49
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
{
/// <summary>
/// 是否启用
/// </summary>
public bool Enabled { get; set; }
/// <summary>
/// consul服务地址
/// </summary>
public string ServiceUrl { get; set; }
/// <summary>
/// 服务名称
/// </summary>
public string ClientName { get; set; }
/// <summary>
/// 服务地址,不带端口
/// </summary>
public string ClientAddress { get; set; }
/// <summary>
/// 服务端口
/// </summary>
public int ClientPort { get; set; }
/// <summary>
/// 健康检查地址
/// </summary>
public string HealthUrl { get; set; }
/// <summary>
/// 服务停止多久后注销,单位秒
/// </summary>
public int DeregisterCriticalServiceAfter { get; set; }
/// <summary>
/// 健康检查时间间隔,或者称为心跳 间隔,单位秒
/// </summary>
public int Interval { get; set; }
/// <summary>
/// 超时时间,单位秒
/// </summary>
public int Timeout { get; set; }
}

24
aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/Options/AbpProGatewayOptions.cs

@ -1,24 +0,0 @@
namespace Lion.AbpPro.AspNetCore.Options;
public class AbpProGatewayOptions
{
/// <summary>
/// 是否启用
/// </summary>
public bool Enabled { get; set; }
/// <summary>
/// consul服务地址
/// </summary>
public string ConsulServiceUrl { get; set; }
/// <summary>
/// consul服务名称
/// </summary>
public string ServiceName { get; set; }
/// <summary>
/// 健康检查地址
/// </summary>
public string HealthUrl { get; set; }
}

28
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
{
/// <summary>
/// consul服务
/// </summary>
public static IApplicationBuilder UseAbpProConsul(this IApplicationBuilder app)
{
// 获取 AbpProGatewayOptions 配置
var consulOptions = app.ApplicationServices.GetRequiredService<IOptions<AbpProGatewayOptions>>().Value;
var consulOptions = app.ApplicationServices.GetRequiredService<IOptions<AbpProConsulOptions>>().Value;
if (!consulOptions.Enabled)
return app;
var appLifetime = app.ApplicationServices.GetService<IHostApplicationLifetime>();
using var scope = app.ApplicationServices.CreateScope();
var clock = scope.ServiceProvider.GetService<IClock>();
var appUrl = new Uri(consulOptions.ConsulServiceUrl, UriKind.Absolute);
var client = scope.ServiceProvider.GetService<IConsulClient>();
var client = scope.ServiceProvider.GetRequiredService<IConsulClient>();
var guidGenerator = scope.ServiceProvider.GetRequiredService<IGuidGenerator>();
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<IOptions<AbpProGatewayOptions>>().Value;
var consulOptions = app.ApplicationServices.GetRequiredService<IOptions<AbpProConsulOptions>>().Value;
if (!consulOptions.Enabled)
return app;
app.UseOcelot().Wait();

4
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<IOptions<AbpProGatewayOptions>>().Value;
var consulOptions = service.BuildServiceProvider().GetRequiredService<IOptions<AbpProConsulOptions>>().Value;
if (!consulOptions.Enabled)
return service;
service.AddSingleton<IConsulClient>(p => new ConsulClient(config => { config.Address = new Uri(consulOptions.ConsulServiceUrl); }));
service.AddSingleton<IConsulClient>(p => new ConsulClient(config => { config.Address = new Uri(consulOptions.ServiceUrl); }));
return service;
}

1
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($"预热成功");
}

40
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
{
private const string DefaultCorsPolicyName = "Default";
public override void ConfigureServices(ServiceConfigurationContext context)
{
ConfigureCors(context);
context.Services.AddAbpProHealthChecks();
context.Services.AddAbpProOcelot();
}
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();
}
/// <summary>
/// 配置跨域
/// </summary>
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();
});
});
}
}
}

1
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;

1
aspnet-core/gateways/Lion.AbpPro.WebGateway/Lion.AbpPro.WebGateway.csproj

@ -3,7 +3,6 @@
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\frameworks\src\Lion.AbpPro.AspNetCore\Lion.AbpPro.AspNetCore.csproj" />
</ItemGroup>

8
aspnet-core/gateways/Lion.AbpPro.WebGateway/Program.cs

@ -1,16 +1,14 @@
namespace Lion.AbpPro.WebGateway
{
namespace Lion.AbpPro.WebGateway;
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Host
.UseAutofac();
builder.Host .UseAutofac();
await builder.AddApplicationAsync<AbpProWebGatewayModule>();
var app = builder.Build();
await app.InitializeApplicationAsync();
await app.RunAsync();
}
}
}

2
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"
}
}
}

8
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"
},

24
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",

24
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",

1
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");
}

12
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

Loading…
Cancel
Save