From 6057b3f11ea2ab270d336d55b77fa0b739e509fc Mon Sep 17 00:00:00 2001 From: hello Date: Sat, 20 Apr 2024 21:18:59 +0800 Subject: [PATCH] =?UTF-8?q?=E8=81=9A=E5=90=88=20API=20=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notes/helloshop/open-api-aggregate.md | 58 +++++++++++++++++++++++++++ notes/helloshop/yarp-reverse-proxy.md | 2 +- 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 notes/helloshop/open-api-aggregate.md diff --git a/notes/helloshop/open-api-aggregate.md b/notes/helloshop/open-api-aggregate.md new file mode 100644 index 0000000..9387445 --- /dev/null +++ b/notes/helloshop/open-api-aggregate.md @@ -0,0 +1,58 @@ +# 聚合 OpenApi 文档 + +每个微服务都有自己的 OpenApi 文档,但是在实际开发中,我们更希望能够将所有微服务的 OpenApi 文档聚合到一起,以便于查看和调试,微服务都是基于 Aspire 框架开发的,所以我们可以使用 Aspire 框架提供的服务发现功能来自动聚合所有微服务的 OpenApi 文档。 + +## 使用 Aspire 服务发现自动配置 OpenApi 文档 + +```csharp +public class OpenApiConfigureOptions() : IConfigureOptions +``` + +```csharp +builder.Services.AddTransient, 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(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; +} +``` diff --git a/notes/helloshop/yarp-reverse-proxy.md b/notes/helloshop/yarp-reverse-proxy.md index 723b5ac..c8f3fab 100644 --- a/notes/helloshop/yarp-reverse-proxy.md +++ b/notes/helloshop/yarp-reverse-proxy.md @@ -2,7 +2,7 @@ ## 构建和设计 API 网关 -API 网关是一个服务器,它是客户端和后端服务之间的中介。它接收来自客户端的请求,然后将这些请求转发到后端服务。API 网关还可以执行其他任务,例如身份验证、监视、负载平衡、缓存、请求分析和日志记录。流程图如下: +API 网关是一个服务器,它是客户端和后端服务之间的中介。它接收来自客户端的请求,然后将这些请求转发到后端服务。API 网关还可以执行其他任务,例如身份验证、监视、负载平衡、缓存、请求分析和日志记录。 ## BFF 模式聚合多个服务