3.8 集成Swagger
...小于 1 分钟
简介
网关
集成Swagger
之后可以显示所有微服务的接口文档,统一文档入口。
Program代码
首先,需要你的微服务集成Swagger
之后生成swagger.json
文件,网关通过读取各微服务的swagger.json
文件来生成Swagger
文档。
using Microsoft.AspNetCore.Builder;
using Wing;
using Wing.ServiceProvider;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddSwaggerGen();
builder.Host.AddWing(builder => builder.AddConsul());
builder.Services.AddWing()
.AddPersistence()
.AddGateWay(null, null, app =>
{
var services = App.DiscoveryService.Get(HealthStatus.Healthy).GetAwaiter().GetResult();
app.UseSwagger()
.UseSwaggerUI(c =>
{
services.ForEach(s => c.SwaggerEndpoint($"{s.ServiceAddress}/swagger/v1/swagger.json", s.Name));
});
});
var app = builder.Build();
app.Run();