Spring Cloud Gateway网关模块聚合各微服务的Swagger接口文档
效果如下图:
相关pom依赖
<!-- 网关路由代理 (仅网关)-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<!--swagger2 (网关和服务端)-->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
Gateway配置
因为Swagger暂不支持webflux项目,所以Gateway里不能配置SwaggerConfig
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;
/**
* 功能描述: 采用路由id做资源名称,predicates.Path做映射的方式.
* 如果需要自定义资源名称,可以单独配置一个集合(route.id,name),创建swaggerResource时,
* 通过route.id获取name.
* <p>例如: swagger:
* route_names:
* - name: 订单
* id: order_route
* - name: 订单1
* id: order_route1
* spring: cloud: gateway: routes:
* - id: order_route
* - id: order_route1
* </p>
* 重新Gateway的方法还没研究出来 - -||
*
* @Author:
* @Date: 2021-04-25 15:38
* @Version 1.0
**/
@Component
@Primary
public class Swagger2 implements SwaggerResourcesProvider {
protected static final String API_URI = "/v2/api-docs";//固定后缀
private final RouteLocator routeLocator;
private final GatewayProperties gatewayProperties;
//资源集合
private static List<SwaggerResource> resources;
//自定义资源名称需要在这里初始化
public Swagger2(RouteLocator routeLocator, GatewayProperties gatewayProperties) {
this.routeLocator = routeLocator;
this.gatewayProperties = gatewayProperties;
}
@Override
public List<SwaggerResource> get() {
//做了一个简单缓存,没仔细设计,其实可有可无用处不大
if (resources == null) {
resources = new ArrayList<>();
List<String> routes = new ArrayList<>();
routeLocator.getRoutes().subscribe(route