feat 网关继承微服务Swagger
This commit is contained in:
@ -0,0 +1,27 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace KonSoft.Shared.Hosting.Gateways
|
||||
{
|
||||
public static class AbpHostingHostBuilderExtensions
|
||||
{
|
||||
public const string AppYarpJsonPath = "yarp.json";
|
||||
|
||||
public static IHostBuilder AddYarpJson(
|
||||
this IHostBuilder hostBuilder,
|
||||
bool optional = true,
|
||||
bool reloadOnChange = true,
|
||||
string path = AppYarpJsonPath)
|
||||
{
|
||||
return hostBuilder.ConfigureAppConfiguration((_, builder) =>
|
||||
{
|
||||
builder.AddJsonFile(
|
||||
path: AppYarpJsonPath,
|
||||
optional: optional,
|
||||
reloadOnChange: reloadOnChange
|
||||
)
|
||||
.AddEnvironmentVariables();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery.Dns" Version="9.5.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery.Yarp" Version="9.5.2" />
|
||||
<PackageReference Include="Yarp.ReverseProxy" Version="2.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using KonSoft.Shared.Hosting.AspNetCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Volo.Abp.Modularity;
|
||||
|
||||
namespace KonSoft.Shared.Hosting.Gateways;
|
||||
@ -8,4 +9,14 @@ namespace KonSoft.Shared.Hosting.Gateways;
|
||||
)]
|
||||
public class KonSoftSharedHostingGatewaysModule : AbpModule
|
||||
{
|
||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||
{
|
||||
var configuration = context.Services.GetConfiguration();
|
||||
|
||||
context.Services.AddHttpForwarderWithServiceDiscovery();
|
||||
|
||||
context.Services.AddReverseProxy()
|
||||
.LoadFromConfig(configuration.GetSection("ReverseProxy"))
|
||||
.AddServiceDiscoveryDestinationResolver();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,61 @@
|
||||
using KonSoft.Shared.Hosting.AspNetCore;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Volo.Abp;
|
||||
using Yarp.ReverseProxy.Configuration;
|
||||
|
||||
namespace KonSoft.Shared.Hosting.Gateways
|
||||
{
|
||||
public static class YarpSwaggerUIBuilderExtensions
|
||||
{
|
||||
public static IApplicationBuilder UseSwaggerUIWithYarp(this IApplicationBuilder app,
|
||||
ApplicationInitializationContext context)
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseAbpSwaggerWithCustomScriptUI(options =>
|
||||
{
|
||||
var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
|
||||
var logger = context.ServiceProvider.GetRequiredService<ILogger<ApplicationInitializationContext>>();
|
||||
var proxyConfigProvider = context.ServiceProvider.GetRequiredService<IProxyConfigProvider>();
|
||||
var yarpConfig = proxyConfigProvider.GetConfig();
|
||||
|
||||
var routedClusters = yarpConfig.Clusters
|
||||
.SelectMany(t => t.Destinations,
|
||||
(clusterId, destination) => new { clusterId.ClusterId, destination.Value });
|
||||
|
||||
var groupedClusters = routedClusters
|
||||
.GroupBy(q => q.Value.Address)
|
||||
.Select(t => t.First())
|
||||
.Distinct()
|
||||
.ToList();
|
||||
|
||||
foreach (var clusterGroup in groupedClusters)
|
||||
{
|
||||
var routeConfig = yarpConfig.Routes.FirstOrDefault(q =>
|
||||
q.ClusterId == clusterGroup.ClusterId);
|
||||
if (routeConfig == null)
|
||||
{
|
||||
logger.LogWarning($"Swagger UI: Couldn't find route configuration for {clusterGroup.ClusterId}...");
|
||||
continue;
|
||||
}
|
||||
|
||||
var baseUrl = clusterGroup.Value.Address;
|
||||
|
||||
if (Convert.ToBoolean(configuration["App:IsOnK8s"])) // If the application is running on K8s, the swagger.json should be reached from public dns.
|
||||
{
|
||||
baseUrl = clusterGroup.Value.Metadata?["PublicAddress"];
|
||||
}
|
||||
|
||||
options.SwaggerEndpoint($"{baseUrl}/swagger/v1/swagger.json", $"{routeConfig.RouteId} API");
|
||||
options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
|
||||
}
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user