日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

.net core在网关中统一配置Swagger

發布時間:2023/12/4 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .net core在网关中统一配置Swagger 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近在做微服務的時候,由于我們是采用前后端分離來開發的,提供給前端的直接是Swagger,如果Swagger分布在各個API中,前端查看Swagger的時候非常不便,因此,我們試著將Swagger集中放到網關中。

這里我用兩個API項目(一個BasicDataApi,一個UsersApi)和一個網關項目(ApiGateway)做示例,下面直接上代碼。

首先在BasicDataApi中配置Swagger:

public void ConfigureServices(IServiceCollection services)

? ? ? ? {

? ? ? ? ? ? services.AddMvc();

? ? ? ? ? ? services.AddSwaggerGen(options =>

? ? ? ? ? ? {

? ? ? ? ? ? ? ? options.SwaggerDoc("BasicDataApi", new Info { Title = "基礎數據服務", Version = "v1" });

? ? ? ? ? ? ? ? var basePath = PlatformServices.Default.Application.ApplicationBasePath;

? ? ? ? ? ? ? ? var xmlPath = Path.Combine(basePath, "Qka.BasicDataApi.xml");

? ? ? ? ? ? ? ? options.IncludeXmlComments(xmlPath);

? ? ? ? ? ? });

? ? ? ? }

?

? ? ? ? public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

? ? ? ? {

? ? ? ? ? ? app.UseMvc()

? ? ? ? ? ? ? ? ?.UseSwagger(c =>

? ? ? ? ? ? {

? ? ? ? ? ? ? ? c.RouteTemplate = "{documentName}/swagger.json";

? ? ? ? ? ? })

? ? ? ? ? ? ? ? .UseSwaggerUI(options =>

? ? ? ? ? ? {

? ? ? ? ? ? ? ? options.SwaggerEndpoint("/BasicDataApi/swagger.json", "BasicDataApi");

? ? ? ? ? ? });

? ? ? ? }

在UsersApi中一樣的配置:

public void ConfigureServices(IServiceCollection services)

? ? ? ? {? ? ? ? ?

? ? ? ? ? ? services.AddSwaggerGen(options =>

? ? ? ? ? ? {

? ? ? ? ? ? ? ? options.SwaggerDoc("UsersApi", new Info { Title = "用戶API接口", Version = "v1" });

? ? ? ? ? ? ? ? var basePath = PlatformServices.Default.Application.ApplicationBasePath;

? ? ? ? ? ? ? ? var xmlPath = Path.Combine(basePath, "Qka.UsersApi.xml");

? ? ? ? ? ? ? ? options.IncludeXmlComments(xmlPath);

? ? ? ? ? ? });

?

? ? ? ? ? ? services.AddMvc();

? ? ? ? }

?

? ? ? ? public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

? ? ? ? {

? ? ? ? ? ? ?

? ? ? ? ? ? app.UseMvc()

? ? ? ? ? ? ? ? .UseSwagger(c =>

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? c.RouteTemplate = "{documentName}/swagger.json";

? ? ? ? ? ? ? ? })

? ? ? ? ? ? ? ? .UseSwaggerUI(options =>

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? options.SwaggerEndpoint("/UsersApi/swagger.json", "UsersApi");

? ? ? ? ? ? ? ? });

? ? ? ? }? ? ? ??

最后在網關項目中修改Ocelot配置,獲取兩個項目的swagger.json不要授權:

"ReRoutes": [

? ? {

? ? ? "DownstreamPathTemplate": "/UsersApi/swagger.json",

? ? ? "DownstreamScheme": "http",

? ? ? "ServiceName": "userapi",

? ? ? "LoadBalancer": "RoundRobin",

? ? ? "UseServiceDiscovery": true,

? ? ? "UpstreamPathTemplate": "/UsersApi/swagger.json",

? ? ? "UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ]

? ? },

? ? {

? ? ? "DownstreamPathTemplate": "/BasicDataApi/swagger.json",

? ? ? "DownstreamScheme": "http",

? ? ? "ServiceName": "basedataapi",

? ? ? "LoadBalancer": "RoundRobin",

? ? ? "UseServiceDiscovery": true,

? ? ? "UpstreamPathTemplate": "/BasicDataApi/swagger.json",

? ? ? "UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ]

? ? },

? ? {

? ? ? "DownstreamPathTemplate": "/UsersApi/{url}",

? ? ? "DownstreamScheme": "http",

? ? ? "ServiceName": "userapi",

? ? ? "LoadBalancer": "RoundRobin",

? ? ? "UseServiceDiscovery": true,

? ? ? "UpstreamPathTemplate": "/UsersApi/{url}",

? ? ? "UpstreamHttpMethod": [ "GET", "POST", "DELETE", "PUT" ] ,

? ? ? "AuthenticationOptions": {

? ? ? ? "AuthenticationProviderKey": "qka_api",

? ? ? ? "AllowedScopes": []

? ? ? }

? ? }

? ],

? "GlobalConfiguration": {

? ? "BaseUrl": "http://localhost:9000",

? ? "ServiceDiscoveryProvider": {

? ? ? "Host": "192.168.2.144",

? ? ? "Port": 8500

? ? }

? }

}

修改StartUp.cs文件的代碼,注意在使用中間件的時候,UseMvc一定要在UseOcelot之前。

public void ConfigureServices(IServiceCollection services)

? ? ? ? {

? ? ? ? ? ? services.AddOcelot();


? ? ? ? ? ? var authenticationProviderKey = "qka_api";

? ? ? ? ? ? services.AddAuthentication("Bearer")

? ? ? ? ? ? ? ? .AddIdentityServerAuthentication(authenticationProviderKey, options =>

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? options.Authority = "http://192.168.2.121:9066/";

? ? ? ? ? ? ? ? ? ? options.RequireHttpsMetadata = false;

? ? ? ? ? ? ? ? ? ? options.ApiName = "UserApi";

? ? ? ? ? ? ? ? });


? ? ? ? ? ? services.AddMvc();

? ? ? ? ? ? services.AddSwaggerGen(options =>

? ? ? ? ? ? {

? ? ? ? ? ? ? ? options.SwaggerDoc("ApiGateway", new Info { Title = "網關服務", Version = "v1" });

? ? ? ? ? ? });

? ? ? ? }


? ? ? ? public void Configure(IApplicationBuilder app, IHostingEnvironment env)

? ? ? ? {

? ? ? ? ? ? app.UseMetricsAllMiddleware();

? ? ? ? ? ? app.UseMetricsAllEndpoints();


? ? ? ? ? ? app.UseCors("default");


? ? ? ? ? ? var apis = new List<string> { "BasicDataApi", "UsersApi" };

? ? ? ? ? ? app.UseMvc()

? ? ? ? ? ? ? ?.UseSwagger()

? ? ? ? ? ? ? ?.UseSwaggerUI(options =>

? ? ? ? ? ? ? ?{

? ? ? ? ? ? ? ? ? ?apis.ForEach(m =>

? ? ? ? ? ? ? ? ? ?{

? ? ? ? ? ? ? ? ? ? ? ?options.SwaggerEndpoint($"/{m}/swagger.json", m);

? ? ? ? ? ? ? ? ? ?});

? ? ? ? ? ? ? ?});


? ? ? ? ? ? app.UseOcelot().Wait();

? ? ? ? }

最后上圖:

相關文章:

  • .NET Core開源API網關 – Ocelot中文文檔

  • Ocelot——初識基于.Net Core的API網關

  • Ocelot API網關的實現剖析

  • 微服務網關Ocelot

  • API網關Ocelot 使用Polly 處理部分失敗問題

  • 談談微服務中的 API 網關(API Gateway)

  • Ocelot網關

  • Ocelot統一權限驗證

  • 應用監控怎么做?

  • ASP.NET Core之跨平臺的實時性能監控

  • .Net Core 2.0+ InfluxDB+Grafana+App Metrics 實現跨平臺的實時性能監控

  • 應用程序的8個關鍵性能指標以及測量方法

  • 使用Metrics監控應用程序的性能

  • 下一個計劃 : .NET/.NET Core應用性能管理

  • Ocelot監控

  • Ocelot 集成Butterfly 實現分布式跟蹤

  • Ocelot中使用Butterfly實踐

  • Ocelot + Consul實踐

  • .NET微服務體系結構中為什么使用Ocelot實現API網關

原文地址:http://www.cnblogs.com/focus-lei/p/9047410.html


.NET社區新聞,深度好文,歡迎訪問公眾號文章匯總 http://www.csharpkit.com

總結

以上是生活随笔為你收集整理的.net core在网关中统一配置Swagger的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。