diff --git a/doc/docs/files/90100_常见问题.md b/doc/docs/files/90100_常见问题.md
index 7bcc2b33..67eb4f8b 100644
--- a/doc/docs/files/90100_常见问题.md
+++ b/doc/docs/files/90100_常见问题.md
@@ -121,3 +121,11 @@ spring.cloud.nacos.discovery.metadata.server.servlet.context-path=${server.servl
# 排除服务,多个用,隔开
sop.service.exclude=your-serviceId1,your-serviceId2
```
+
+或者使用正则:
+
+```properties
+# 排除以"test-"开头的
+# 多个正则用英文分号(;)隔开
+sop.service.exclude-regex=test\\-.*
+```
diff --git a/sop-common/sop-gateway-common/pom.xml b/sop-common/sop-gateway-common/pom.xml
index 78661016..19b577f0 100644
--- a/sop-common/sop-gateway-common/pom.xml
+++ b/sop-common/sop-gateway-common/pom.xml
@@ -83,6 +83,13 @@
2.3.0
true
+
+
+ junit
+ junit
+ 4.12
+ test
+
diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java
index 7ef06db6..f05fa2eb 100644
--- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java
+++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/EnvironmentKeys.java
@@ -31,7 +31,11 @@ public enum EnvironmentKeys {
/**
* 排除其它微服务,多个用英文逗号隔开
*/
- SOP_SERVICE_EXCLUDE("sop.service.exclude")
+ SOP_SERVICE_EXCLUDE("sop.service.exclude"),
+ /**
+ * 排除其它微服务,正则形式,多个用英文逗号隔开
+ */
+ SOP_SERVICE_EXCLUDE_REGEX("sop.service.exclude-regex")
;
private String key;
diff --git a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java
index 260f7e3b..4167e93b 100644
--- a/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java
+++ b/sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/route/BaseRegistryListener.java
@@ -53,11 +53,9 @@ public abstract class BaseRegistryListener implements RegistryListener {
}
protected boolean canOperator(String serviceId) {
- List excludeServiceIdList = getExcludeServiceId();
- for (String excludeServiceId : excludeServiceIdList) {
- if (excludeServiceId.equalsIgnoreCase(serviceId)) {
- return false;
- }
+ // 被排除的服务,不能操作
+ if (isExcludeService(serviceId)) {
+ return false;
}
// nacos会不停的触发事件,这里做了一层拦截
// 同一个serviceId5秒内允许访问一次
@@ -70,6 +68,32 @@ public abstract class BaseRegistryListener implements RegistryListener {
return can;
}
+ /**
+ * 是否是被排除的服务
+ *
+ * @param serviceId 服务id
+ * @return true,是
+ */
+ private boolean isExcludeService(String serviceId) {
+ List excludeServiceIdList = getExcludeServiceId();
+ for (String excludeServiceId : excludeServiceIdList) {
+ if (excludeServiceId.equalsIgnoreCase(serviceId)) {
+ return true;
+ }
+ }
+ // 匹配正则
+ String sopServiceExcludeRegex = EnvironmentKeys.SOP_SERVICE_EXCLUDE_REGEX.getValue();
+ if (StringUtils.isNotBlank(sopServiceExcludeRegex)) {
+ String[] regexArr = sopServiceExcludeRegex.split(";");
+ for (String regex : regexArr) {
+ if (serviceId.matches(regex)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
private List getExcludeServiceId() {
String excludeServiceIds = EnvironmentKeys.SOP_SERVICE_EXCLUDE.getValue();
List excludeServiceIdList = new ArrayList<>(8);
@@ -80,4 +104,6 @@ public abstract class BaseRegistryListener implements RegistryListener {
excludeServiceIdList.addAll(EXCLUDE_SERVICE_ID_LIST);
return excludeServiceIdList;
}
+
+
}
diff --git a/sop-common/sop-gateway-common/src/test/java/com/gitee/sop/gatewaycommon/ExcludeTest.java b/sop-common/sop-gateway-common/src/test/java/com/gitee/sop/gatewaycommon/ExcludeTest.java
new file mode 100644
index 00000000..0d7f7b2d
--- /dev/null
+++ b/sop-common/sop-gateway-common/src/test/java/com/gitee/sop/gatewaycommon/ExcludeTest.java
@@ -0,0 +1,22 @@
+package com.gitee.sop.gatewaycommon;
+
+import junit.framework.TestCase;
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * @author tanghc
+ */
+public class ExcludeTest extends TestCase {
+ public void testRegex() {
+ String serviceId = "com.aaa.bbb.story-service";
+ String sopServiceExcludeRegex = "com\\..*;story\\-.*";
+ if (StringUtils.isNotBlank(sopServiceExcludeRegex)) {
+ String[] regexArr = sopServiceExcludeRegex.split(";");
+ for (String regex : regexArr) {
+ if (serviceId.matches(regex)) {
+ System.out.println("111");
+ }
+ }
+ }
+ }
+}