springcloud-gateway聚合knife4j接口文档

版本选择:

本次swagger接口聚合并非引入swagger依赖,而是使用了knife4j,这个依赖包是对swagger前段页面做了美化,使其更加好看,操作性更强

注意:本次文章中接口文档底层为swagger2

版本选择

组件 版本
spring-boot 2.3.12.RELEASE
spring-cloud Hoxton.SR12
knife4j-spring-boot-starter 2.0.9
package com.vcolco.gateway.config;

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;

import java.util.List;
import java.util.Optional;

/**
 * @author lei
 * @create 2022-05-25 10:09
 * @desc swagger资源处理
 **/
@RestController
@RequestMapping("/swagger-resources")
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("/configuration/security")
    public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
        return Mono.just(new ResponseEntity<>(
            Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()),
            HttpStatus.OK));
    }

    @GetMapping("/configuration/ui")
    public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
        return Mono.just(new ResponseEntity<>(
            Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
    }

    @GetMapping
    public Mono<ResponseEntity<List<SwaggerResource>>> swaggerResources() {
        return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
    }
}

获取接口资源列表

package com.vcolco.gateway.config;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
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;

/**
 * @author lei
 * @create 2022-05-25 10:01
 * @desc 网关处获取所有服务swagger文档
 **/
@Slf4j
@Component
@Primary
@AllArgsConstructor
public class SwaggerResourceConfig implements SwaggerResourcesProvider {

    /**
     * swagger2默认的url后缀
     */
    private static final String SWAGGER2URL = "/v2/api-docs";

    /**
     * 路由加载器
     */
    private final RouteLocator routeLocator;

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

    @Autowired
    public SwaggerResourceConfig(RouteLocator routeLocator) {
        this.routeLocator = routeLocator;
    }

    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        List<String> routeHosts = new ArrayList<>();
        Flux<Route> routes = routeLocator.getRoutes();
        // 获取所有可用的host,因为是注册表服务调用,所以实际是服务名
        routes.filter(route -> route.getUri().getHost() != null)
            .filter(route -> !gateway.equals(route.getUri().getHost()))
            .subscribe(route -> routeHosts.add(route.getUri().getHost()));

        // 去重下拉列表相同服务名文档,多个服务展示一个,当网关调用这个接口时,会自动通过负载均衡寻找服务host
        Set<String> docServerSet = new HashSet<>();
        routeHosts.forEach(instance -> {
            // 拼接url. /serviceId/v2/api-info
            String url = "/" + instance.toLowerCase() + SWAGGER2URL;
            if (!docServerSet.contains(url)) {
                docServerSet.add(url);
                SwaggerResource swaggerResource = new SwaggerResource();
                swaggerResource.setUrl(url);
                swaggerResource.setName(instance);
                resources.add(swaggerResource);
            }
        });
        return resources;
    }
}

网关访问接口文档

http://ip:port/doc.html

springcloud-gateway聚合knife4j接口文档_第1张图片

网关获取各微服务列表逻辑剖析

image-20220526214758545

一、获取微服务资源列表

GET http://网关IP:网关端口/swagger-resources

上方获取接口资源列表中,底层使用routeLocator获取所有路由列表(我本身是设置根据服务名路由)

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true

springcloud-gateway聚合knife4j接口文档_第2张图片

二、根据选中资源获取对应服务资源明细

image-20220526215018286

springcloud-gateway聚合knife4j接口文档_第3张图片

跟据此接口,则可获取到对应服务的所有接口文档资源以及接口文档信息

springcloud-gateway聚合knife4j接口文档_第4张图片

你可能感兴趣的:(springcloud,spring,cloud,gateway,java,中文分词)