聚合 API 文档
This commit is contained in:
parent
0ce7e8ee70
commit
6057b3f11e
58
notes/helloshop/open-api-aggregate.md
Normal file
58
notes/helloshop/open-api-aggregate.md
Normal file
@ -0,0 +1,58 @@
|
||||
# 聚合 OpenApi 文档
|
||||
|
||||
每个微服务都有自己的 OpenApi 文档,但是在实际开发中,我们更希望能够将所有微服务的 OpenApi 文档聚合到一起,以便于查看和调试,微服务都是基于 Aspire 框架开发的,所以我们可以使用 Aspire 框架提供的服务发现功能来自动聚合所有微服务的 OpenApi 文档。
|
||||
|
||||
## 使用 Aspire 服务发现自动配置 OpenApi 文档
|
||||
|
||||
```csharp
|
||||
public class OpenApiConfigureOptions() : IConfigureOptions<SwaggerUIOptions>
|
||||
```
|
||||
|
||||
```csharp
|
||||
builder.Services.AddTransient<IConfigureOptions<SwaggerUIOptions>, OpenApiConfigureOptions>();
|
||||
```
|
||||
|
||||
## 定制 OpenApi 文档样式
|
||||
|
||||
在 Resource/OpenApi 文件夹下创建 `Custom.css` 文件。
|
||||
|
||||
```css
|
||||
.swagger-ui .topbar-wrapper img {
|
||||
content: url('https://test.com/logo.svg');
|
||||
}
|
||||
|
||||
.swagger-ui .topbar-wrapper .link::after {
|
||||
margin-left: 0.5rem;
|
||||
content: "HelloWorld";
|
||||
}
|
||||
```
|
||||
|
||||
## 关联 CSS 文件到 OpenApi 文档
|
||||
|
||||
```csharp
|
||||
public static class OpenApiExtensions
|
||||
{
|
||||
public static IServiceCollection AddOpenApi(this IServiceCollection)
|
||||
{
|
||||
services.Configure<SwaggerUIOptions>(options =>
|
||||
{
|
||||
options.DocumentTitle = Assembly.GetExecutingAssembly().GetName().Name;
|
||||
options.InjectStylesheet("/ServiceDefaults/Resources/OpenApi/Custom.css");
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```csharp
|
||||
public static IApplicationBuilder UseOpenApi(this IApplicationBuilder app)
|
||||
{
|
||||
// Configure the HTTP request pipeline.
|
||||
app.UseSwagger(apiConfigureOptions)
|
||||
app.Map("/ServiceDefaults", appBuilder => appBuilder.UseStaticFiles(new StaticFileOptions
|
||||
{
|
||||
FileProvider = new EmbeddedFileProvider(Assembly.GetExecutingAssembly())
|
||||
}))
|
||||
|
||||
return app;
|
||||
}
|
||||
```
|
@ -2,7 +2,7 @@
|
||||
|
||||
## 构建和设计 API 网关
|
||||
|
||||
API 网关是一个服务器,它是客户端和后端服务之间的中介。它接收来自客户端的请求,然后将这些请求转发到后端服务。API 网关还可以执行其他任务,例如身份验证、监视、负载平衡、缓存、请求分析和日志记录。流程图如下:
|
||||
API 网关是一个服务器,它是客户端和后端服务之间的中介。它接收来自客户端的请求,然后将这些请求转发到后端服务。API 网关还可以执行其他任务,例如身份验证、监视、负载平衡、缓存、请求分析和日志记录。
|
||||
|
||||
## BFF 模式聚合多个服务
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user