最近在做微服务的时候,由于我们是采用前后端分离来开发的,提供给前端的直接是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();

}

最后上图:

相关文章:

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


.NET社区新闻,深度好文,欢迎访问公众号文章汇总 http://www.csharpkit.com

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

  1. 一日一技:在Ocelot网关中统一配置Swagger

    概述 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.在Ocelot网关中,我们提供给前端的直接是Swagger,如果Swagger分布在各 ...

  2. 如何为 .NET Core 3.0 中 WPF 配置依赖注入 ?

    咨询区 user842818: 我非常熟悉 ASP.NET Core 和它开箱即用的依赖注入支持,当 Controller 需要依赖注入时,可以在 构造函数 中以参数的形式来实现,这个IOC的理念相当 ...

  3. android gradle 版本部队,gradle中统一配置版本的小技巧。

    在Project/build.gradle中定义,在module/build.gradle中使用 1.直接在Project/build.gradle中定义和引用: // Top-level build ...

  4. 如何在gateway网关中聚合swagger

    前言 由于项目原因,需要将网关从zuul升级到gateway网关,由于 gateway网关底层是基于webflux的,导致原先在网关中集成的swagger不可用. 那么如何在gateway网关中整合s ...

  5. Spring-Cloud中的统一配置中心

    服务拆分以后,服务的数量非常多,如果所有的配置都以配置文件的方式放在应用本地的话,非常难以管理,可以想象当有几百上千个进程中有一个配置出现了问题,是很难将它找出来的,因而需要有统一的配置中心,来管理所 ...

  6. 避免在 ASP.NET Core 3.0 中为启动类注入服务

    本篇是如何升级到ASP.NET Core 3.0系列文章的第二篇. Part 1 - 将.NET Standard 2.0 类库转换为.NET Core 3.0 类库 Part 2 - IHostin ...

  7. properties 配置回车_非常全面的讲解SpringCloud中Zuul网关原理及其配置,看它就够了!...

    本文同步Java知音社区,专注于Java 作者:kosamino http://www.cnblogs.com/jing99/p/11696192.html Zuul是spring cloud中的微服 ...

  8. 网关的作用是什么_SpringCloud中Zuul网关原理及其配置,看它就够了

    正文 Zuul是spring cloud中的微服务网关.网关:是一个网络整体系统中的前置门户入口.请求首先通过网关,进行路径的路由,定位到具体的服务节点上. Zuul是一个微服务网关,首先是一个微服务 ...

  9. asp.net core 3.0 中使用 swagger

    asp.net core 3.0 中使用 swagger Intro 上次更新了 asp.net core 3.0 简单的记录了一下 swagger 的使用,那个项目的 api 比较简单,都是匿名接口 ...

最新文章

  1. python多进程_Python多进程实践
  2. 创建用于 ASP.NET 的分页程序控件
  3. 搞懂了这几点,你就学会了Web编程
  4. SSM中jsp向后台Controller传值中文乱码的奇葩解决!!!
  5. solidworks2018安装教程
  6. C语言——小型图书管理系统(课程设计)
  7. 如何在vs2010中使用SSE指令集
  8. NotePad 常用设置
  9. 天锐绿盾防泄密软件6.0新版本功能已优化!!!
  10. 多尺度卷积稀疏编码的无监督迁移学习
  11. 推荐oracle exadata,Exadata的一些常见误区
  12. Ubuntu 朗文不发音问题
  13. 阿宁的linux学习---vi/vim
  14. led数字字体_led电视质量排行榜
  15. 如何选择一款合适的福禄克数字万用表?FLUKE
  16. FPS显示和修改——unity3D
  17. 微软高性能计算新贵WHS2008
  18. IELTS writing skills——学习笔记
  19. colorkey唇釉是否安全_colorkey唇釉安全吗-colorkey唇釉真假辨别
  20. 华为python面试_记华为面试

热门文章

  1. 消息队列入门(三)JMS标准及实现
  2. 物联网智能硬件设备常见攻击方法
  3. Photoshop脚本 使用ExtendScript编写Ps脚本
  4. CentOS 5 CentOS 6 启动流程及关键步骤
  5. C#中的多线程 - 并行编程 z
  6. android学习笔记之多线程(二)
  7. 我的2021年终总结:初为人父,从头再来
  8. 如何让 Timer 在特定时间点触发?
  9. 从代码角度揭示:华为鸿蒙的“套壳”真相!
  10. 程序员过关斩将--作为一个架构师,我是不是应该有很多职责?