From 9c66ce6cd5a975fb9490d0e9a895e9262f30daa1 Mon Sep 17 00:00:00 2001 From: lihui Date: Wed, 23 Aug 2023 09:26:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?ServiceRouterConvert=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E4=BC=98=E5=8C=96=E3=80=81=E6=B3=A8=E9=87=8A?= =?UTF-8?q?=E8=A1=A5=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../discover/ServiceRouterConvert.java | 38 ++-- .../router/AbstactServiceRouterConvert.java | 167 +++++++++++------- 2 files changed, 125 insertions(+), 80 deletions(-) diff --git a/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/ServiceRouterConvert.java b/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/ServiceRouterConvert.java index 9d3fb056d..0a62e71b5 100644 --- a/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/ServiceRouterConvert.java +++ b/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/ServiceRouterConvert.java @@ -14,27 +14,31 @@ * limitations under the License. */ - package com.github.xiaoymin.knife4j.spring.gateway.discover; /** - * @author xiaoymin@foxmail.com - * 2023/8/3 14:48 + * 服务路由转换接口 + * + * @author xiaoymin@foxmail.com
+ * 2023/8/3 14:48 * @since knife4j 4.3.0 */ public interface ServiceRouterConvert { - - /** - * 处理gateway的路由,在服务发现模式下自动转换为Knife4j-gateway前端所需要分组内容 - * @param routerHolder 当前RouterHolder - */ - void process(ServiceRouterHolder routerHolder); - - /** - * 处理顺序 - * @return 顺序 - */ - default int order() { - return 0; - }; + + /** + * 处理gateway的路由,在服务发现模式下自动转换为Knife4j-gateway前端所需要分组内容 + * + * @param routerHolder 当前RouterHolder + */ + void process(ServiceRouterHolder routerHolder); + + /** + * 处理顺序 + * + * @return 顺序 + */ + default int order() { + return 0; + } + } diff --git a/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/AbstactServiceRouterConvert.java b/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/AbstactServiceRouterConvert.java index 8254b7e51..c5aa8043a 100644 --- a/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/AbstactServiceRouterConvert.java +++ b/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/AbstactServiceRouterConvert.java @@ -14,9 +14,14 @@ * limitations under the License. */ - package com.github.xiaoymin.knife4j.spring.gateway.discover.router; +import java.util.List; +import java.util.Map; + +import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition; +import org.springframework.util.CollectionUtils; + import com.github.xiaoymin.knife4j.spring.gateway.Knife4jGatewayProperties; import com.github.xiaoymin.knife4j.spring.gateway.conf.GlobalConstants; import com.github.xiaoymin.knife4j.spring.gateway.discover.ServiceRouterConvert; @@ -26,83 +31,119 @@ import com.github.xiaoymin.knife4j.spring.gateway.utils.PathUtils; import com.github.xiaoymin.knife4j.spring.gateway.utils.ServiceUtils; import com.github.xiaoymin.knife4j.spring.gateway.utils.StrUtil; + import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition; -import org.springframework.util.CollectionUtils; - -import java.util.List; -import java.util.Map; /** - * @author xiaoymin@foxmail.com - * 2023/8/3 15:03 + * 服务路由转换接口抽象处理类 + * + * @author xiaoymin@foxmail.com
+ * 2023/8/3 15:03 * @since knife4j v4.3.0 */ @AllArgsConstructor @Slf4j public abstract class AbstactServiceRouterConvert implements ServiceRouterConvert { - + /** * 获取路由前缀 + * * @param predicateArgs 参数 * @return 路由前缀 */ abstract String convertPathPrefix(Map predicateArgs); - + + /** + * 解析gateway的路由定义 + * + * @param routerHolder + * @param discover + * @param predicateDefinitions + * @param id + * @param serviceName + */ protected void parseRouteDefinition(ServiceRouterHolder routerHolder, Knife4jGatewayProperties.Discover discover, - List predicateDefinitions, String id, String serviceName) { - predicateDefinitions - .stream() - .filter(predicateDefinition -> GlobalConstants.ROUTER_PATH_NAME.equalsIgnoreCase(predicateDefinition.getName())) - .findFirst() - .ifPresent(predicateDefinition -> { - log.debug("serviceId:{},serviceName:{}", id, serviceName); - Map configInfoMap = discover.getServiceConfig(); - // String pathPrefix = predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0").replace("**",StringUtil.EMPTY_STRING); - String pathPrefix = convertPathPrefix(predicateDefinition.getArgs()); - log.debug("pathPrefix:{}", pathPrefix); - String contextPath = GlobalConstants.EMPTY_STR; - String groupName = id; - int order = 0; - String targetUrl = ServiceUtils.getOpenAPIURL(discover, pathPrefix, null); - // String targetUrl = PathUtils.append(pathPrefix, GlobalConstants.DEFAULT_OPEN_API_V3_PATH); - // 如果自定义了setting内容 拼接 - Knife4jGatewayProperties.ServiceConfigInfo configInfo = configInfoMap.get(serviceName); - if (configInfo != null) { - order = configInfo.getOrder(); - if (discover.getVersion() == OpenApiVersion.OpenAPI3) { - // 如果是springfox-swagger2,springfox框架会自动根据targetUrl访问追加一个basePath路径到Swagger2规范中 - // 避免重复追加ContextPath路径,此规则只作用与openapi3 - contextPath = PathUtils.append(pathPrefix, configInfo.getContextPath()); - } else if (discover.getVersion() == OpenApiVersion.Swagger2) { - // 如果是swagger2场景,判断当前contextPath配置的是否为空 - if (StrUtil.isNotBlank(configInfo.getContextPath()) && !GlobalConstants.DEFAULT_API_PATH_PREFIX.equals(configInfo.getContextPath())) { - // 用户自行设定,追加 - contextPath = configInfo.getContextPath(); - } - } - // 复用contextPath的路径 - targetUrl = PathUtils.append(contextPath, ServiceUtils.getOpenAPIURL(discover, GlobalConstants.DEFAULT_API_PATH_PREFIX, null)); - List groupNames = configInfo.getGroupNames(); - if (CollectionUtils.isEmpty(groupNames)) { - groupName = configInfo.getGroupName(); - } else { - // 服务内接口分组 - int sort = order; - String ctx = contextPath; - String url = targetUrl; - groupNames.forEach(_groupName -> routerHolder.add(new OpenAPI2Resource(PathUtils.append(url, _groupName), sort, true, _groupName, ctx, serviceName))); - return; - } - } else { - // 如果没有配置service-config,追加一个子服务的前缀contextPath - // 仅在openapi3框架下追加,springfox-swagger2会默认根据url的请求,自动在Swagger2规范中设置一个basePath属性 - if (discover.getVersion() == OpenApiVersion.OpenAPI3) { - contextPath = PathUtils.processContextPath(pathPrefix); - } - } - routerHolder.add(new OpenAPI2Resource(targetUrl, order, true, groupName, contextPath, serviceName)); - }); + List predicateDefinitions, String id, String serviceName) { + predicateDefinitions.stream().filter( + predicateDefinition -> GlobalConstants.ROUTER_PATH_NAME.equalsIgnoreCase(predicateDefinition.getName())) + .findFirst().ifPresent(predicateDefinition -> this.processRouteDefinition(routerHolder, discover, id, + serviceName, predicateDefinition)); } + + /** + * 处理gateway的路由定义 + * + * @param routerHolder + * @param discover + * @param id + * @param serviceName + * @param predicateDefinition + */ + private void processRouteDefinition(ServiceRouterHolder routerHolder, Knife4jGatewayProperties.Discover discover, + String id, String serviceName, PredicateDefinition predicateDefinition) { + log.debug("serviceId:{},serviceName:{}", id, serviceName); + Map configInfoMap = discover.getServiceConfig(); + String pathPrefix = this.convertPathPrefix(predicateDefinition.getArgs()); + log.debug("pathPrefix:{}", pathPrefix); + String contextPath = GlobalConstants.EMPTY_STR; + String groupName = id; + int order = 0; + String targetUrl = ServiceUtils.getOpenAPIURL(discover, pathPrefix, null); + // 如果自定义了setting内容 拼接 + Knife4jGatewayProperties.ServiceConfigInfo configInfo = configInfoMap.get(serviceName); + if (configInfo != null) { + order = configInfo.getOrder(); + if (discover.getVersion() == OpenApiVersion.OpenAPI3) { + // 如果是springfox-swagger2,springfox框架会自动根据targetUrl访问追加一个basePath路径到Swagger2规范中 + // 避免重复追加ContextPath路径,此规则只作用与openapi3 + contextPath = PathUtils.append(pathPrefix, configInfo.getContextPath()); + } else if (discover.getVersion() == OpenApiVersion.Swagger2) { + // 如果是swagger2场景,判断当前contextPath配置的是否为空 + if (StrUtil.isNotBlank(configInfo.getContextPath()) + && !GlobalConstants.DEFAULT_API_PATH_PREFIX.equals(configInfo.getContextPath())) { + // 用户自行设定,追加 + contextPath = configInfo.getContextPath(); + } + } + // 复用contextPath的路径 + targetUrl = PathUtils.append(contextPath, + ServiceUtils.getOpenAPIURL(discover, GlobalConstants.DEFAULT_API_PATH_PREFIX, null)); + List groupNames = configInfo.getGroupNames(); + if (CollectionUtils.isEmpty(groupNames)) { + groupName = configInfo.getGroupName(); + } else { + // 服务内接口分组 + int sort = order; + String ctx = contextPath; + String url = targetUrl; + groupNames.forEach(name -> routerHolder + .add(this.buildOpenApi2Resource(serviceName, ctx, name, sort, PathUtils.append(url, name)))); + return; + } + } else { + // 如果没有配置service-config,追加一个子服务的前缀contextPath + // 仅在openapi3框架下追加,springfox-swagger2会默认根据url的请求,自动在Swagger2规范中设置一个basePath属性 + if (discover.getVersion() == OpenApiVersion.OpenAPI3) { + contextPath = PathUtils.processContextPath(pathPrefix); + } + } + routerHolder.add(this.buildOpenApi2Resource(serviceName, contextPath, groupName, order, targetUrl)); + } + + /** + * 构建资源 + * + * @param serviceName + * @param contextPath + * @param groupName + * @param order + * @param targetUrl + * @return + */ + private OpenAPI2Resource buildOpenApi2Resource(String serviceName, String contextPath, String groupName, int order, + String targetUrl) { + return new OpenAPI2Resource(targetUrl, order, true, groupName, contextPath, serviceName); + } + } From b3b2a7e809801668d8b8a516f1c6e2110845b69a Mon Sep 17 00:00:00 2001 From: lihui Date: Thu, 24 Aug 2023 17:49:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9Knife4jGatewayProperties.?= =?UTF-8?q?Discover=E4=BC=A0=E8=BE=93=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../router/AbstactServiceRouterConvert.java | 34 ++++++++++++++----- .../router/ConfigRouteServiceConvert.java | 8 ++++- .../DiscoverClientRouteServiceConvert.java | 8 ++++- .../router/DynamicRouteServiceConvert.java | 8 ++++- 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/AbstactServiceRouterConvert.java b/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/AbstactServiceRouterConvert.java index c5aa8043a..6286545e9 100644 --- a/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/AbstactServiceRouterConvert.java +++ b/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/AbstactServiceRouterConvert.java @@ -54,44 +54,49 @@ public abstract class AbstactServiceRouterConvert implements ServiceRouterConver */ abstract String convertPathPrefix(Map predicateArgs); + /** + * 获取服务发现策略配置信息 + * + * @return 服务发现策略配置信息 + */ + abstract Knife4jGatewayProperties.Discover getDiscover(); + /** * 解析gateway的路由定义 * * @param routerHolder - * @param discover * @param predicateDefinitions * @param id * @param serviceName */ - protected void parseRouteDefinition(ServiceRouterHolder routerHolder, Knife4jGatewayProperties.Discover discover, + protected void parseRouteDefinition(ServiceRouterHolder routerHolder, List predicateDefinitions, String id, String serviceName) { predicateDefinitions.stream().filter( predicateDefinition -> GlobalConstants.ROUTER_PATH_NAME.equalsIgnoreCase(predicateDefinition.getName())) - .findFirst().ifPresent(predicateDefinition -> this.processRouteDefinition(routerHolder, discover, id, - serviceName, predicateDefinition)); + .findFirst().ifPresent(predicateDefinition -> this.processRouteDefinition(routerHolder, id, serviceName, + predicateDefinition)); } /** * 处理gateway的路由定义 * * @param routerHolder - * @param discover * @param id * @param serviceName * @param predicateDefinition */ - private void processRouteDefinition(ServiceRouterHolder routerHolder, Knife4jGatewayProperties.Discover discover, - String id, String serviceName, PredicateDefinition predicateDefinition) { + private void processRouteDefinition(ServiceRouterHolder routerHolder, String id, String serviceName, + PredicateDefinition predicateDefinition) { log.debug("serviceId:{},serviceName:{}", id, serviceName); - Map configInfoMap = discover.getServiceConfig(); String pathPrefix = this.convertPathPrefix(predicateDefinition.getArgs()); log.debug("pathPrefix:{}", pathPrefix); String contextPath = GlobalConstants.EMPTY_STR; String groupName = id; int order = 0; + Knife4jGatewayProperties.Discover discover = this.getDiscover(); String targetUrl = ServiceUtils.getOpenAPIURL(discover, pathPrefix, null); // 如果自定义了setting内容 拼接 - Knife4jGatewayProperties.ServiceConfigInfo configInfo = configInfoMap.get(serviceName); + Knife4jGatewayProperties.ServiceConfigInfo configInfo = this.getServiceConfigInfo(serviceName); if (configInfo != null) { order = configInfo.getOrder(); if (discover.getVersion() == OpenApiVersion.OpenAPI3) { @@ -131,6 +136,17 @@ private void processRouteDefinition(ServiceRouterHolder routerHolder, Knife4jGat routerHolder.add(this.buildOpenApi2Resource(serviceName, contextPath, groupName, order, targetUrl)); } + /** + * 从配置里面获取服务配置信息 + * + * @param serviceName + * @return + */ + private Knife4jGatewayProperties.ServiceConfigInfo getServiceConfigInfo(String serviceName) { + Map configInfoMap = this.getDiscover().getServiceConfig(); + return configInfoMap.get(serviceName); + } + /** * 构建资源 * diff --git a/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/ConfigRouteServiceConvert.java b/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/ConfigRouteServiceConvert.java index 970949933..bcaf3fefc 100644 --- a/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/ConfigRouteServiceConvert.java +++ b/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/ConfigRouteServiceConvert.java @@ -50,7 +50,7 @@ public void process(ServiceRouterHolder routerHolder) { .stream() .filter(routeDefinition -> ServiceUtils.startLoadBalance(routeDefinition.getUri())) .filter(routeDefinition -> ServiceUtils.includeService(routeDefinition.getUri(), routerHolder.getService(), routerHolder.getExcludeService())) - .forEach(routeDefinition -> parseRouteDefinition(routerHolder, this.knife4jGatewayProperties.getDiscover(), routeDefinition.getPredicates(), routeDefinition.getUri().getHost(), + .forEach(routeDefinition -> parseRouteDefinition(routerHolder, routeDefinition.getPredicates(), routeDefinition.getUri().getHost(), routeDefinition.getUri().getHost())); } @@ -67,4 +67,10 @@ String convertPathPrefix(Map predicateArgs) { public int order() { return GatewayRouterStrategy.CONFIG.getOrder(); } + + @Override + Knife4jGatewayProperties.Discover getDiscover() { + return this.knife4jGatewayProperties.getDiscover(); + } + } diff --git a/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/DiscoverClientRouteServiceConvert.java b/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/DiscoverClientRouteServiceConvert.java index bc9503e33..1b2b78f5e 100644 --- a/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/DiscoverClientRouteServiceConvert.java +++ b/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/DiscoverClientRouteServiceConvert.java @@ -54,7 +54,7 @@ public void process(ServiceRouterHolder holder) { discoveryClientRouteDefinitionLocator.getRouteDefinitions() .filter(routeDefinition -> ServiceUtils.startLoadBalance(routeDefinition.getUri())) .filter(routeDefinition -> ServiceUtils.includeService(routeDefinition.getUri(), holder.getService(), holder.getExcludeService())) - .subscribe(routeDefinition -> parseRouteDefinition(holder, this.knife4jGatewayProperties.getDiscover(), routeDefinition.getPredicates(), routeDefinition.getUri().getHost(), + .subscribe(routeDefinition -> parseRouteDefinition(holder, routeDefinition.getPredicates(), routeDefinition.getUri().getHost(), routeDefinition.getUri().getHost())); } @@ -71,4 +71,10 @@ String convertPathPrefix(Map predicateArgs) { public int order() { return GatewayRouterStrategy.REACTIVE.getOrder(); } + + @Override + Knife4jGatewayProperties.Discover getDiscover() { + return this.knife4jGatewayProperties.getDiscover(); + } + } diff --git a/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/DynamicRouteServiceConvert.java b/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/DynamicRouteServiceConvert.java index 472c6b6b0..eb5c1c7fb 100644 --- a/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/DynamicRouteServiceConvert.java +++ b/knife4j/knife4j-gateway-spring-boot-starter/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/discover/router/DynamicRouteServiceConvert.java @@ -48,7 +48,7 @@ public void process(ServiceRouterHolder holder) { routeDefinitionRepository.getRouteDefinitions() .filter(routeDefinition -> ServiceUtils.startLoadBalance(routeDefinition.getUri())) .filter(routeDefinition -> ServiceUtils.includeService(routeDefinition.getUri(), holder.getService(), holder.getExcludeService())) - .subscribe(routeDefinition -> parseRouteDefinition(holder, this.knife4jGatewayProperties.getDiscover(), routeDefinition.getPredicates(), routeDefinition.getUri().getHost(), + .subscribe(routeDefinition -> parseRouteDefinition(holder, routeDefinition.getPredicates(), routeDefinition.getUri().getHost(), routeDefinition.getUri().getHost())); } @@ -65,4 +65,10 @@ String convertPathPrefix(Map predicateArgs) { public int order() { return GatewayRouterStrategy.DYNAMIC.getOrder(); } + + @Override + Knife4jGatewayProperties.Discover getDiscover() { + return this.knife4jGatewayProperties.getDiscover(); + } + }