最近在作微服務的時候,因爲咱們是採用先後端分離來開發的,提供給前端的直接是Swagger,若是Swagger分佈在各個API中,前端查看Swagger的時候很是不便,所以,咱們試着將Swagger集中放到網關中。前端
這裏我用兩個API項目(一個BasicDataApi,一個UsersApi)和一個網關項目(ApiGateway)作示例,下面直接上代碼。json
首先在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中同樣的配置:api
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不要受權:app
"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以前。前後端分離
:ide
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(); }
最後上圖:微服務