Spring Cloud Gateway:整合knife4j实现网关聚合接口文档

修改配置文件,增加knife4j、Swagger2的配置

1、application.yml中添加:

gateway:
  discovery:
    locator:
      enabled: true

2、添加MySwaggerResourceProvider类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 使用Spring Boot单体架构集成swagger时,是通过包路径进行业务分组,然后在前端进行不同模块的展示,而在微服务架构下,单个服务类似于原来业务组;
 * springfox-swagger提供的分组接口是swagger-resource,返回的是分组接口名称、地址等信息;
 * 在Spring Cloud微服务架构下,需要swagger-resource重写接口,由网关的注册中心动态发现所有的微服务文档
 */
@Primary
@Configuration
public class MySwaggerResourceProvider implements SwaggerResourcesProvider {
    @Autowired
    private RouteLocator routeLocator;

    // 网关应用名称
    @Value("${spring.application.name}")
    private String applicationName;

    //接口地址
    private static final String API_URI = "/v2/api-docs";

    @Override
    public List get() {
        //接口资源列表
        List resources = new ArrayList<>();
        //服务名称列表
        List routeHosts = new ArrayList<>();

        // 获取所有可用的应用名称
        routeLocator.getRoutes()
                .filter(route -> route.getUri().getHost() != null)
                .filter(route -> !applicationName.equals(route.getUri().getHost()))
                .subscribe(route -> routeHosts.add(route.getUri().getHost()));

        // 去重,多负载服务只添加一次
        Set existsServer = new HashSet<>();
        routeHosts.forEach(host -> {
            // 拼接url
            String url = "/" + host + API_URI;
            //不存在则添加
            if (!existsServer.contains(url)) {
                existsServer.add(url);
                SwaggerResource swaggerResource = new SwaggerResource();
                swaggerResource.setUrl(url);
                swaggerResource.setName(host);
                resources.add(swaggerResource);
            }
        });
        return resources;
    }
}

3、添加 SwaggerResourceController类

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger.web.SwaggerResource;

import java.util.List;

@RestController
@RequestMapping("/swagger-resources")
@RequiredArgsConstructor
public class SwaggerResourceController {

    private final MySwaggerResourceProvider mySwaggerResourceProvider;

    @RequestMapping
    public ResponseEntity> swaggerResources() {
        return new ResponseEntity<>(mySwaggerResourceProvider.get(), HttpStatus.OK);
    }
}

借鉴的文章:

Spring Cloud Gateway系列【13】 整合knife4j实现网关聚合接口文档_云烟成雨TD的博客-CSDN博客_gateway整合knife4j

SpringCloud微服务实战——搭建企业级开发框架(十九):Gateway使用knife4j聚合微服务文档 - 全栈程序猿 - 博客园

springcloud gateway: discovery: locator: enabled: true 解释 - 星回中道 - 博客园 

你可能感兴趣的:(spring,cloud,java,微服务)