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

修改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在网关中统一配置Swagger的更多相关文章

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

    最近在做微服务的时候,由于我们是采用前后端分离来开发的,提供给前端的直接是Swagger,如果Swagger分布在各个API中,前端查看Swagger的时候非常不便,因此,我们试着将Swagger集中 ...

  2. asp.net core 2.0中的配置(1)---Configuration

    配置就是一个装配数据字典的过程,一个字典也就是一个键值对,所以从配置就是键值对. 在asp.net core中关于配置是由四个基本的类型来支撑的,是①IConfigurationSource②ICon ...

  3. 重新整理 .net core 实践篇————网关中的身份签名认证[三十七]

    前言 简单整理一下网关中的jwt,jwt用于授权认证的,其实关于认证授权这块https://www.cnblogs.com/aoximin/p/12268520.html 这个链接的时候就已经写了,当 ...

  4. .NET Core微服务之基于Apollo实现统一配置中心

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 一.关于统一配置中心与Apollo 在微服务架构环境中,项目中配置文件比较繁杂,而且不同环境的不同配置修改相对频繁,每次发布都需要对应修改 ...

  5. .NET Core WebApi帮助文档使用Swagger生成Api说明文档

    Swagger也称为Open API,Swagger从API文档中手动完成工作,并提供一系列用于生成,可视化和维护API文档的解决方案.简单的说就是一款让你更好的书写API文档的框架. 我们为什么选择 ...

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

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

  7. 15.ASP.NET Core 应用程序中的静态文件中间件

    在这篇文章中,我将向大家介绍,如何使用中间件组件来处理静态文件.这篇文章中,我们讨论下面几个问题: 在ASP.NET Core中,我们需要把静态文件存放在哪里? 在ASP.NET Core中 wwwr ...

  8. 微服务之十四如何在 Ocelot 网关中配置多实例 Swagger 访问

    一.介绍 当我们开发基于微服务的应用程序的时候,有一个环节总是跳不过去的,那就是要创建 WebApi,然后,我们的应用程序基于 WebApi 接口去访问.在没有 Swagger 以前,我们开发好了 W ...

  9. 基于Apollo实现.NET Core微服务统一配置(测试环境-单机)

    一.前言 注:此篇只是为测试环境下的快速入门.后续会给大家带来生产环境下得实战开发. 具体的大家可以去看官方推荐.非常的简单明了.以下介绍引用官方内容: Apollo(阿波罗)是携程框架部门研发的分布 ...

随机推荐

  1. Mongodb3.6 基操命令(二)——如何使用help

    前言 在上一篇文章Mongodb3.6 快速入门(一)中,我们主要使用两个命令: 1.mongod #启动服务 2.mongo #连接mongodb 对于刚接触mongo的人来说,该怎么给命令传递参数 ...

  2. htmldom操作添加标签顺序

    <!DOCTYPE html> <html> <body> <div id="div1"> </div> <scr ...

  3. storm中的Scheduler

    Scheduler是storm的调度器,负责为topology分配当前集群中可用的资源.Storm分别提供了3中调度器: EvenScheduler:会将系统中的可用资源均匀地分配给当前需要任务分配的 ...

  4. Spring Boot开发MongoDB应用实践

    本文继续上一篇定时任务中提到的邮件服务,简单讲解Spring Boot中如何使用MongoDB进行应用开发. 上文中提到的这个简易邮件系统大致设计思路如下: 1.发送邮件支持同步和异步发送两种 2.邮 ...

  5. AUTOSAR的前期开源实现Arctic Core

    AUTOSAR (AUTomotive Open System ARchitecture) is a worldwide development partnership of vehicle manu ...

  6. springmvc 请求经过controller后静态资源无法访问的问题

    经过RequestMapping(“xx”)后 转发请求时会在url里面附带地址, 导致访问静态资源文件失败, 解决办法是在 spring-mvc.xml文件中加上 <mvc:default-s ...

  7. PCB布线要求

    时钟线要求 时钟驱动器布局在PCB中心而非电路板外围,布局尽量靠近,走线圆滑.短,非直角.非T形,布线可选4~8mil,过窄会导致高频信号衰减,并降低信号之间电容性耦合. 避免时钟之间.与信号之间的干 ...

  8. PAT1003:Emergency

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  9. redis Web服务器

    redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  10. linux常用的时间获取函数(time,gettimeofday,clock_gettime,_ftime,localtime,strftime )

    time()提供了秒级的精确度 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在的秒 ...