From 34981c30a78e8bbd7791131059a9210f9928b62c Mon Sep 17 00:00:00 2001
From: 刘光辉 <347230014@qq.com>
Date: 星期四, 17 九月 2026 09:24:09 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master' into master
---
jnpf-gateway/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/utils/ServiceUtils.java | 126 ++++++++++++++++++++++++++++++++++++++++++
1 files changed, 126 insertions(+), 0 deletions(-)
diff --git a/jnpf-gateway/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/utils/ServiceUtils.java b/jnpf-gateway/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/utils/ServiceUtils.java
new file mode 100644
index 0000000..574642b
--- /dev/null
+++ b/jnpf-gateway/src/main/java/com/github/xiaoymin/knife4j/spring/gateway/utils/ServiceUtils.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright 漏 2017-2023 Knife4j(xiaoymin@foxmail.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package com.github.xiaoymin.knife4j.spring.gateway.utils;
+
+import com.github.xiaoymin.knife4j.spring.gateway.Knife4jGatewayProperties;
+import com.github.xiaoymin.knife4j.spring.gateway.conf.GlobalConstants;
+import com.github.xiaoymin.knife4j.spring.gateway.enums.GatewayRouterStrategy;
+import com.github.xiaoymin.knife4j.spring.gateway.enums.OpenApiVersion;
+import com.github.xiaoymin.knife4j.spring.gateway.spec.v2.OpenAPI2Resource;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.util.CollectionUtils;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.util.*;
+import java.util.regex.Pattern;
+
+/**
+ * 鍦ㄦ湇鍔″彂鐜�(Discover)鍦烘櫙涓嬬殑鑱氬悎杈呭姪宸ュ叿绫�
+ * @author <a href="xiaoymin@foxmail.com">xiaoymin@foxmail.com</a>
+ * 2023/7/31 15:05
+ * @since knife4j v4.2.0
+ */
+@Slf4j
+public class ServiceUtils {
+
+ private final static String LB = "lb://";
+
+ /**
+ * 鏍规嵁OpenAPI瑙勮寖鍙婂垎缁勫悕绉颁笉鍚岃幏鍙栦笉鍚岀殑榛樿鍦板潃
+ * @param discover 鏈嶅姟鍙戠幇閰嶇疆
+ * @param contextPath contextPath
+ * @param groupName 鍒嗙粍鍚嶇О
+ * @return openapi鍦板潃
+ * @since v4.3.0
+ */
+ public static String getOpenAPIURL(Knife4jGatewayProperties.Discover discover, String contextPath, String groupName) {
+ OpenApiVersion apiVersion = discover.getVersion();
+ StringBuilder urlBuilder = new StringBuilder();
+ String _defaultPath = PathUtils.processContextPath(contextPath);
+ // String _groupName = StrUtil.defaultTo(groupName, GlobalConstants.DEFAULT_GROUP_NAME);
+ String _groupName = "";
+ String groupUrl = "";
+ if (apiVersion == OpenApiVersion.Swagger2) {
+ groupUrl = GlobalConstants.DEFAULT_OPEN_API_V2_PATH + _groupName;
+ } else if (apiVersion == OpenApiVersion.OpenAPI3) {
+ groupUrl = PathUtils.append(GlobalConstants.DEFAULT_OPEN_API_V3_PATH, _groupName);
+ }
+ urlBuilder.append(PathUtils.append(_defaultPath, groupUrl));
+ return urlBuilder.toString();
+ }
+
+ /**
+ * 鍒ゆ柇鏈嶅姟璺敱鏄惁璐熻浇閰嶇疆
+ * @param uri 璺敱
+ * @return True-鏄紝False-闈瀕b
+ */
+ public static boolean startLoadBalance(URI uri) {
+ if (uri == null) {
+ return false;
+ }
+ String path = uri.toString();
+ if (path == null || path.isEmpty()) {
+ return false;
+ }
+ return path.startsWith(LB);
+ }
+
+ /**
+ * 鍒ゆ柇鏄惁鍖呭惈鏈嶅姟
+ * @param uri 璺敱鏈嶅姟
+ * @param service 鏈嶅姟鍒楄〃
+ * @param excludeService 宸叉帓闄ゆ湇鍔″垪琛�
+ * @return True-鏄紝False-闈�
+ */
+ public static boolean includeService(URI uri, Collection<String> service, Collection<String> excludeService) {
+ String serviceName = uri.getHost();
+ // DUBBO鏈嶅姟鏃犳硶瑙f瀽
+ if(serviceName == null){
+ serviceName = uri.getAuthority();
+ }
+ if(serviceName == null){
+ serviceName = uri.toString();
+ }
+ return service.stream().anyMatch(serviceName::equalsIgnoreCase) && !excludeServices(serviceName, excludeService);
+ }
+
+ /**
+ * 鍒ゆ柇褰撳墠鏈嶅姟鏄惁鍦ㄦ帓闄ゆ湇鍔″垪琛ㄤ腑
+ * @param serviceName 鏈嶅姟鍚嶇О
+ * @param excludeService 鎺掗櫎鏈嶅姟瑙勫垯鍒楄〃锛屾敮鎸佹鍒欒〃杈惧紡(4.3.0鐗堟湰)
+ * @return True-鍦ㄦ帓闄ゆ湇鍔″垪琛ㄤ腑锛孎alse-涓嶆弧瓒宠鍒�
+ * @since v4.3.0
+ */
+ public static boolean excludeServices(String serviceName, Collection<String> excludeService) {
+ if (CollectionUtils.isEmpty(excludeService)) {
+ return false;
+ }
+ for (String es : excludeService) {
+ // 棣栧厛鏍规嵁鏈嶅姟鍚嶇О鐩存帴鍒ゆ柇涓�娆�
+ if (es.equalsIgnoreCase(serviceName)) {
+ return true;
+ }
+ // 澧炲姞姝e垯琛ㄨ揪寮忓垽鏂�
+ if (Pattern.compile(es, Pattern.CASE_INSENSITIVE).matcher(serviceName).matches()) {
+ return true;
+ }
+ }
+ return false;
+ }
+}
--
Gitblit v1.8.0