From f1330bf4e8f9db38bc6d4db57d4eb41f6c8e03d9 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 14 Apr 2023 09:23:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20Knife4j=204.1.0=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yudao-dependencies/pom.xml | 2 +- .../SwaggerResourceHandlerFunction.java | 105 ------------------ .../YudaoSwaggerAutoConfiguration.java | 42 ------- .../src/main/resources/application.yaml | 14 ++- 4 files changed, 13 insertions(+), 150 deletions(-) delete mode 100644 yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/swagger/SwaggerResourceHandlerFunction.java delete mode 100644 yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/swagger/YudaoSwaggerAutoConfiguration.java diff --git a/yudao-dependencies/pom.xml b/yudao-dependencies/pom.xml index 5dfd51e32..1a0510290 100644 --- a/yudao-dependencies/pom.xml +++ b/yudao-dependencies/pom.xml @@ -23,7 +23,7 @@ 2.5 2.2.8 1.6.14 - 4.0.0 + 4.1.0 1.2.15 3.5.3.1 diff --git a/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/swagger/SwaggerResourceHandlerFunction.java b/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/swagger/SwaggerResourceHandlerFunction.java deleted file mode 100644 index 08bbf2828..000000000 --- a/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/swagger/SwaggerResourceHandlerFunction.java +++ /dev/null @@ -1,105 +0,0 @@ -package cn.iocoder.yudao.gateway.swagger; - -import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.util.StrUtil; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.cloud.gateway.config.GatewayProperties; -import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition; -import org.springframework.cloud.gateway.route.RouteDefinition; -import org.springframework.cloud.gateway.support.NameUtils; -import org.springframework.http.MediaType; -import org.springframework.web.reactive.function.server.HandlerFunction; -import org.springframework.web.reactive.function.server.ServerRequest; -import org.springframework.web.reactive.function.server.ServerResponse; -import reactor.core.publisher.Mono; - -import java.util.*; - -/** - * 获得 Swagger 资源的 {@link HandlerFunction} 实现类 - * - * @author zxliu - * @since 2022-10-25 11:23 - */ -@RequiredArgsConstructor -@Slf4j -public class SwaggerResourceHandlerFunction implements HandlerFunction { - - private final GatewayProperties gatewayProperties; - - @Override - public Mono handle(ServerRequest request) { - return ServerResponse.ok() - .contentType(MediaType.APPLICATION_JSON) - .bodyValue(getSwaggerResourceList()); - } - - /** - * 获得 SwaggerResource 列表 - * - * @return SwaggerResource 列表 - */ - public List> getSwaggerResourceList() { - // 将 RouteDefinition 转换成 SwaggerResource - List> resources = new ArrayList<>(); - Set serviceNames = new HashSet<>(); // 已处理的服务名,避免重复 - gatewayProperties.getRoutes().forEach(route -> { - // 已存在的服务,直接忽略 - String serviceName = route.getUri().getHost(); - if (StrUtil.isEmpty(serviceName)) { - return; - } - if (!serviceNames.add(serviceName)) { - return; - } - - // 获得 Path PredicateDefinition - String path = getRoutePath(route); - if (path == null) { - return; - } - - // 重要:构建最终的 SwaggerResource 对象 - resources.add(buildSwaggerResource(serviceName, path)); - }); - return resources; - } - - private Map buildSwaggerResource(String name, String location) { - Map swaggerResource = new HashMap<>(); - swaggerResource.put("name", name); - swaggerResource.put("location", location); - swaggerResource.put("url", location); - swaggerResource.put("swaggerVersion", "3.0.3"); - return swaggerResource; - } - - /** - * 获得路由的 Path - * - * ① 输入: - * predicates: - * - Path=/admin-api/system/** - * ② 输出: - * /admin-api/system/v3/api-docs - * - * @param route 路由 - * @return 路由 - */ - private String getRoutePath(RouteDefinition route) { - PredicateDefinition pathDefinition = CollUtil.findOne(route.getPredicates(), - predicateDefinition -> "Path".equals(predicateDefinition.getName())); - if (pathDefinition == null) { - log.info("[get][Route({}) 没有 Path 条件,忽略接口文档]", route.getId()); - return null; - } - String path = pathDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0"); - if (StrUtil.isEmpty(path)) { - log.info("[get][Route({}) Path 的值为空,忽略接口文档]", route.getId()); - return null; - } - return path.replace("/**", "/v3/api-docs"); - } - -} diff --git a/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/swagger/YudaoSwaggerAutoConfiguration.java b/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/swagger/YudaoSwaggerAutoConfiguration.java deleted file mode 100644 index 313a956f8..000000000 --- a/yudao-gateway/src/main/java/cn/iocoder/yudao/gateway/swagger/YudaoSwaggerAutoConfiguration.java +++ /dev/null @@ -1,42 +0,0 @@ -package cn.iocoder.yudao.gateway.swagger; - -import com.github.xiaoymin.knife4j.spring.gateway.configuration.Knife4jGatewayAutoConfiguration; -import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.cloud.gateway.config.GatewayProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.reactive.function.server.RouterFunction; -import org.springframework.web.reactive.function.server.RouterFunctions; -import org.springframework.web.reactive.function.server.ServerResponse; - -/** - * 网关 Swagger 接口文档的自动配置类 - * - * 参考 {@link Knife4jGatewayAutoConfiguration} 实现,进行功能的增强,核心实现在 {@link SwaggerResourceHandlerFunction} 类中 - * 它通过解析 spring.cloud.gateway.routes 配置,获得 Swagger 资源分组。 - * - * 另外,目前官方 Knif4j 网关的实现,不会通过注册中心加载对应的 URL 地址。等到他们完善了,就可以去掉自己的这个实现了。 - * - * @see Knife4j + Spring Cloud Gateway 网关聚合 - * - * @author 芋道源码 - */ -@Configuration -@ConditionalOnProperty(name = "knife4j.gateway.enable", havingValue = "true") -@Slf4j -public class YudaoSwaggerAutoConfiguration { - - /** - * Swagger 资源分组 URL - */ - public static final String GATEWAY_SWAGGER_GROUP_URL = "/swagger-resources"; - - @Bean - public RouterFunction swaggerResourceHandlerFunction(GatewayProperties gatewayProperties) { - log.info("[swaggerResourceHandlerFunction][初始化完成]"); - SwaggerResourceHandlerFunction handlerFunction = new SwaggerResourceHandlerFunction(gatewayProperties); - return RouterFunctions.route().GET(GATEWAY_SWAGGER_GROUP_URL, handlerFunction).build(); - } - -} diff --git a/yudao-gateway/src/main/resources/application.yaml b/yudao-gateway/src/main/resources/application.yaml index 2a800891d..7e9f626e5 100644 --- a/yudao-gateway/src/main/resources/application.yaml +++ b/yudao-gateway/src/main/resources/application.yaml @@ -55,6 +55,16 @@ spring: prefix-enabled: false # 避免 Swagger 重复带上额外的 /admin-api/system 前缀 knife4j: - # 聚合 Swagger 文档 + # 聚合 Swagger 文档,参考 https://doc.xiaominfo.com/docs/action/springcloud-gateway 文档 gateway: - enable: true + enabled: true + routes: + - name: system-server + service-name: system-server + url: /admin-api/system/v3/api-docs + - name: infra-server + service-name: infra-server + url: /admin-api/infra/v3/api-docs + - name: bpm-server + service-name: bpm-server + url: /admin-api/bpm/v3/api-docs