概要
现在相信大部分公司的系统架构都是分布式,微服务 ,在使用swagger文档时,前后端联调或者测试接口每次都要打开各个微服务的文档去找对应的接口比较麻烦,在这里给大家讲讲如何在gateway中集成各个服务的接口文档
整体技术方案
使用gateway+nacos+swagger集成微服务接口文档
实现步骤
- 业务服务集成swagger文档
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
//.title("swagger-bootstrap-ui-demo RESTful APIs")
.description("# swagger-bootstrap-ui-demo RESTful APIs")
.termsOfServiceUrl("http://www.xx.com/")
.contact("xx@qq.com")
.version("1.0")
.build())
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("asset.product.api"))
.paths(PathSelectors.any())
.build();
}
}
- 使用nacos注册服务
cloud:
nacos:
server-addr: xx.xx.x.xx:30887
config:
namespace: xx.xx.x.xx
group: ${
spring.application.name}
refresh-enabled: true
file-extension: yml
discovery:
namespace: xx.xx.x.xx
group: ${
spring.profiles.active}
register-enabled: true
- 配置gateway路由
# 基本配置
server:
servlet:
context-path: /api/asset-gateway
spring:
application:
name: asset-gateway
profiles:
active: dev
jackson:
time-zone: GMT+8
cloud:
gateway:
discovery:
locator:
lower-case-service-id: true
routes:
#交易服务
- id: asset-transaction-svc
uri: lb://asset-transaction-svc
predicates:
- Path=/api/asset-transaction-svc/**