## :-: 微服务聚合Swagger文档
>[success] 微服务项目中,没有做API文档聚合,访问每个服务的API文档都需要访问单独的\`swagger-ui.html\`页面,既然我们使用了微服务,就应该有统一的API文档入口,\`knife4j\`有这方面的支持,本文将详细介绍其实现,希望对大家有所帮助!
> 采用Nacos作为注册中心,Gateway作为网关,使用`knife4j`来生成API文档。
相关服务划分:
* hjmall-gateway:网关服务,作为微服务API文档的访问入口,聚合所有API文档,需要引入文档前端UI包;
* hjmall-accout:用户服务,普通API服务,不需要引入文档前端UI包;
* hjmall-market:营销服务,普通API服务,不需要引入文档前端UI包。
## 具体实现
>[danger] 下面详细介绍下Spring Cloud Gateway + knife4j 聚合API文档的具体实现,依次搭建用户服务、营销服务和网关服务
每个服务引入如下jar包
~~~
<!--swagger2:API 文档生成工具 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
~~~
* hjmall-accout
>[danger] 我们首先来搭建用户服务,一个普通的API服务,很简单,仅需三步即可集成knife4j。
* 在`pom.xml`中添加相关依赖,一个SpringBoot的web功能依赖,knife4j的微服务依赖(不包含API文档的前端UI包);
~~~
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-micro-spring-boot-starter</artifactId>
</dependency>
~~~
>[danger] 添加Swagger相关配置,非常常规的配置,添加`@EnableKnife4j`注解开启knife4j的增强功能。
~~~
Swagger2Config 配置如下
~~~
~~~
package com.hjf.market.core;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.hjf.market.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("营销系统接口Api")
.description("营销系统接口Api")
.termsOfServiceUrl("")
.contact("devteam")
.version("1.0")
.build();
}
}
~~~
>[danger] 我们接下来搭建营销服务,一个普通的API服务,直接参考上面用户服务的搭建即可。
>[danger] 最后我们搭建网关服务,作为微服务API文档的的统一入口,
> 聚合所有微服务的API文档。在`pom.xml`中添加相关依赖,
> Gateway相关依赖和knife4j的Starter(包含API文档的前端UI包);
~~~
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>
~~~
>[danger] 在网关上添加Swagger资源配置,用于聚合其他微服务中Swagger的`api-docs`访问路径;
> SwaggerResourceConfig 配置如下
~~~
package com.hjf.gateway.swagger;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.support.NameUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import java.util.ArrayList;
import java.util.List;
/**
* Swagger资源配置
*/
@Slf4j
@Component
@Primary
@AllArgsConstructor
public class SwaggerResourceConfig implements SwaggerResourcesProvider {
private final RouteLocator routeLocator;
private final GatewayProperties gatewayProperties;
@Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();
List<String> routes = new ArrayList<>();
routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));
gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId())).forEach(route -> {
route.getPredicates().stream()
.filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
.forEach(predicateDefinition -> resources.add(swaggerResource(route.getId(),
predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0")
.replace("**", "v2/api-docs"))));
});
return resources;
}
private SwaggerResource swaggerResource(String name, String location) {
log.info("name:{},location:{}",name,location);
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion("2.0");
return swaggerResource;
}
}
~~~
什么是Swagger的`api-docs`访问路径?该路径会返回JSON格式数据,Swagger渲染API文档页面的所有数据就是来源于此,比如我们的用户服务会返回如下信息,访问地址:[http://localhost:9000/api/account/v2/api-docs](http://localhost:9000/api/account/v2/api-docs)
![](https://img.kancloud.cn/1c/44/1c443af00848dce15dbf28a56d47e307_775x430.png)
>[danger] 接下来我们需要自定义Swagger各个配置的节点,简单来说就是自定义Swagger内部的各个获取数据的接口;
> SwaggerHandler配置
~~~
package com.hjf.gateway.swagger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import springfox.documentation.swagger.web.*;
import java.util.Optional;
@RestController
public class SwaggerHandler {
@Autowired(required = false)
private SecurityConfiguration securityConfiguration;
@Autowired(required = false)
private UiConfiguration uiConfiguration;
private final SwaggerResourcesProvider swaggerResources;
@Autowired
public SwaggerHandler(SwaggerResourcesProvider swaggerResources) {
this.swaggerResources = swaggerResources;
}
@GetMapping("/swagger-resources/configuration/security")
public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
return Mono.just(new ResponseEntity<>(
Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
}
@GetMapping("/swagger-resources/configuration/ui")
public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
return Mono.just(new ResponseEntity<>(
Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
}
@GetMapping("/swagger-resources")
public Mono<ResponseEntity> swaggerResources() {
return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
}
}
~~~
比如说`swagger-resources`这个接口,可用于获取所有微服务的`api-docs`访问路径,获取信息如下,访问地址:[http://localhost:9000](http://localhost:9000)[/swagger-resources](http://localhost:9201/swagger-resources)
![](https://img.kancloud.cn/ff/bc/ffbcdb0f3f8bb8b679b9af7e6ff89080_743x479.png)
## 功能演示
>[danger] 接下来我们来演示下微服务API文档聚合的功能,仅需要访问网关的API文档页面即可,可自行切换到相关服务的API文档。
> 在此之前先启动我们的Nacos注册中心,然后依次启动`hjmall-gateway、hjmall-accout、hjmall-market`服务;
> 访问 localhost:9000/doc.html
![](https://img.kancloud.cn/53/73/5373c11c8de9d4f758b40320a72fdaca_708x420.png)
## 总结
>[danger] 对比knife4j和原生Swagger的微服务使用,再次证明knife4j是springfox-swagger的增强UI实现,完全遵循了springfox-swagger中的使用方式。
> 官方文档:[https://doc.xiaominfo.com/guide/ui-front-gateway.html](https://doc.xiaominfo.com/guide/ui-front-gateway.html)
- 项目介绍
- 项目声明
- 项目简介
- 架构设计
- 项目亮点功能介绍
- 技术栈介绍
- 核心功能
- 运行环境
- 项目更新日志
- 文档更新日志
- F&Q
- 部署教程
- JDK安装
- 环境准备
- 必要启动模块
- 扩展模块(可选)
- 打包工程
- 核心模块
- 登录认证
- 缓存功能
- 日志模块
- 分布式锁
- 消息队列
- 异常处理
- 系统接口
- 参数验证
- es检索
- 系统设计
- 系统总体架构
- 注册&配置中心alibaba/nacos
- 介绍
- Nacos安装
- Nacos配置中心
- Nacos注册发现
- Nacos生产部署方案
- 限流熔断alibaba/sentinel
- 使用Sentinel实现gateway网关及服务接口限流
- Sentinel使用Nacos存储规则及同步
- 服务调用Feign
- Feign基本介绍
- 如何使用
- 负载均衡
- 请求超时
- 请求拦截器
- 分布式缓存Redis
- 单机版
- 集群
- 分布式任务调度
- XXL-JOB
- 分布式事务
- TX-LCN
- Seata
- Seata原理解析
- 数据库分库分表
- 链路追踪
- 基本介绍
- SkyWalking
- 消息队列
- Kafka
- docker安装kafka
- Linux集群
- swagger文档
- 分布式ID生成器解决方案
- 统一日志解决方案
- 日志解决方案设计
- 介绍与相关资料
- ELK安装部署
- elasticsearch 7.5
- logstash-7.5
- kibana-7.5
- filebeat
- 全文搜索elasticsearch
- windows集群搭建
- docker安装es
- ElasticHD
- linux集群部署
- docker
- 安装
- Docker安装Nginx
- Docker部署启动springboot
- dockerCompose
- harbor
- Docker私有镜像仓库
- Portainer
- Docker远程连接设置
- 服务网关
- 基本介绍
- 使用网关
- 路由配置
- 全局过滤器
- 服务认证授权架构设计
- 认证服务流程
- 授权服务流程
- 系统幂等性设计与实践
- 分布式日志链路跟踪
- 实时搜索系统设计
- 服务监控
- 基本介绍
- 如何使用
- 整合Admin-Ui
- 客户端配置
- 持续集成部署CICD
- 自动化部署Jenkins
- 安装部署win
- 打包发布远程执行
- 安装部署linux
- jenkins+gitlab+docker容器化工程自动化部署
- Git
- maven
- CICD说明
- 应用性能
- 压力测试工具
- Apache JMeter介绍和安装
- ApacheJMeter使用
- JVM
- JVM性能调优
- 常见JVM内存错误及解决方案
- JVM 分析工具详解
- Spring Cloud性能调优
- 企业开发环境搭建
- 研发项目管理软件(禅道)
- Maven私服
- nexus安装部署
- nexus使用介绍
- 容器管理平台