getChildrenData(String parentPath) throws Exception {
+ PathChildrenCache pathChildrenCache = buildPathChildrenCache(parentPath);
+ if (pathChildrenCache == null) {
+ return Collections.emptyList();
+ }
+ return pathChildrenCache.getCurrentData();
+ }
+
+ public static PathChildrenCache buildPathChildrenCache(String path) throws Exception {
+ if (!isPathExist(path)) {
+ return null;
+ }
+ // PathChildrenCache: 监听数据节点的增删改,可以设置触发的事件
+ // 且第三个参数要设置为true,不然ChildData对象中的getData返回null
+ PathChildrenCache childrenCache = new PathChildrenCache(client, path, true);
+ // 列出子节点数据列表,需要使用BUILD_INITIAL_CACHE同步初始化模式才能获得,异步是获取不到的
+ childrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
+ return childrenCache;
+ }
+
+}
diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/WebConfig.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/WebConfig.java
index 98ccd478..e051661b 100644
--- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/WebConfig.java
+++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/config/WebConfig.java
@@ -1,14 +1,26 @@
package com.gitee.sop.adminserver.config;
+import org.apache.commons.lang.math.NumberUtils;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.CuratorFrameworkFactory;
+import org.apache.curator.retry.ExponentialBackoffRetry;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
+
+/**
+ * @author thc
+ */
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
+
+
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//配置映射关系
diff --git a/sop-admin/sop-admin-server/src/main/resources/application-dev.properties b/sop-admin/sop-admin-server/src/main/resources/application-dev.properties
deleted file mode 100644
index 3e962752..00000000
--- a/sop-admin/sop-admin-server/src/main/resources/application-dev.properties
+++ /dev/null
@@ -1,38 +0,0 @@
-server.port=8082
-
-#################easyopen基础配置#################
-# 显示文档
-easyopen.show-doc=true
-# 本地秘钥
-easyopen.app-secret.test=123456
-
-# 关闭跨域,默认开启
-#easyopen.cors=false
-
-# 开启webflux
-easyopen.mono=false
-
-## 拦截器
-#easyopen.interceptors[0]=com.gitee.easyopen.support.LimitInterceptor
-#easyopen.interceptors[1]=com.gitee.easyopen.support.PermissionInterceptor
-
-# 配置中心,config-server-port对应easyopen-config中的netty.server.port
-#easyopen.app-name=app-normal
-#easyopen.config-server-ip=127.0.0.1
-#easyopen.config-server-port=8071
-#easyopen.doc-url=http://127.0.0.1:8081/api/doc
-
-
-#################redis基础配置#################
-spring.redis.database=1
-spring.redis.host=10.1.11.48
-spring.redis.password=0987654321rfvujmtgbyhn
-spring.redis.port=6379
-# 连接超时时间 单位 ms(毫秒)
-spring.redis.timeout=3000
-
-
-logging.file=D:/logs/server/server
-logging.level.com.gitee=debug
-
-
diff --git a/sop-admin/sop-admin-server/src/main/resources/application-dev.yml b/sop-admin/sop-admin-server/src/main/resources/application-dev.yml
new file mode 100644
index 00000000..9afe91d4
--- /dev/null
+++ b/sop-admin/sop-admin-server/src/main/resources/application-dev.yml
@@ -0,0 +1,27 @@
+server:
+ port: 8082
+
+spring:
+ application:
+ name: sop-admin
+
+ cloud:
+ zookeeper:
+ connect-string: localhost:2181
+ baseSleepTimeMs: 3000
+ maxRetries: 3
+
+easyopen:
+ show-doc: true
+ mono: false
+ ignore-validate: true
+
+sop-admin:
+ profiles: default,prod,dev,test
+
+
+logging:
+ level:
+ com:
+ gitee: debug
+
diff --git a/sop-admin/sop-admin-server/src/main/resources/application.properties b/sop-admin/sop-admin-server/src/main/resources/application.properties
deleted file mode 100644
index 257b3064..00000000
--- a/sop-admin/sop-admin-server/src/main/resources/application.properties
+++ /dev/null
@@ -1 +0,0 @@
-spring.profiles.active=dev
\ No newline at end of file
diff --git a/sop-admin/sop-admin-server/src/main/resources/application.yml b/sop-admin/sop-admin-server/src/main/resources/application.yml
new file mode 100644
index 00000000..caf4dfcd
--- /dev/null
+++ b/sop-admin/sop-admin-server/src/main/resources/application.yml
@@ -0,0 +1,3 @@
+spring:
+ profiles:
+ active: dev
\ No newline at end of file
diff --git a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/BaseRouteDefinition.java b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/BaseRouteDefinition.java
index e95605ca..6869c6f5 100644
--- a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/BaseRouteDefinition.java
+++ b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/BaseRouteDefinition.java
@@ -26,4 +26,9 @@ public class BaseRouteDefinition {
* 是否忽略验证,业务参数验证除外
*/
private boolean ignoreValidate;
+
+ /**
+ * 是否禁用
+ */
+ private boolean disabled;
}
diff --git a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java
index 6466edf5..191804cc 100644
--- a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java
+++ b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/bean/SopConstants.java
@@ -34,5 +34,5 @@ public class SopConstants {
/**
* zookeeper存放接口路由信息的根目录
*/
- public static final String SOP_SERVICE_ROUTE_PATH = "/sop-service-route";
+ public static final String SOP_SERVICE_ROUTE_PATH = "/com.gitee.sop.route";
}
diff --git a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/NameVersionRoutePredicateFactory.java b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/NameVersionRoutePredicateFactory.java
index 6331a244..6da345b6 100644
--- a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/NameVersionRoutePredicateFactory.java
+++ b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/route/NameVersionRoutePredicateFactory.java
@@ -1,6 +1,8 @@
package com.gitee.sop.gatewaycommon.gateway.route;
import com.gitee.sop.gatewaycommon.bean.SopConstants;
+import com.gitee.sop.gatewaycommon.bean.TargetRoute;
+import com.gitee.sop.gatewaycommon.manager.RouteRepositoryContext;
import com.gitee.sop.gatewaycommon.param.ParamNames;
import com.gitee.sop.gatewaycommon.util.RequestUtil;
import lombok.extern.slf4j.Slf4j;
@@ -59,7 +61,14 @@ public class NameVersionRoutePredicateFactory extends AbstractRoutePredicateFact
String nameVersion = config.param;
String name = params.getOrDefault(ParamNames.API_NAME, "");
String version = params.getOrDefault(ParamNames.VERSION_NAME, "");
- return (name + version).equals(nameVersion);
+ boolean match = (name + version).equals(nameVersion);
+ if (match) {
+ TargetRoute targetRoute = RouteRepositoryContext.getRouteRepository().get(nameVersion);
+ if (targetRoute != null && targetRoute.getRouteDefinition().isDisabled()) {
+ return false;
+ }
+ }
+ return match;
};
}
diff --git a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/BaseRouteManager.java b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/BaseRouteManager.java
index a5ab7b52..39e8168e 100644
--- a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/BaseRouteManager.java
+++ b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/BaseRouteManager.java
@@ -23,7 +23,7 @@ import static com.gitee.sop.gatewaycommon.bean.SopConstants.SOP_SERVICE_ROUTE_PA
/**
* 路由管理,采用zookeeper实现,监听路由的增删改,并适时更新到本地。路由的存储格式为:
*
- * /sop-service-route 根节点
+ * /com.gitee.sop.route 根节点
* /serviceId 服务节点,名字为服务名
* /route1 路由节点,名字为:name+version,存放路由信息
* /route2
@@ -38,12 +38,12 @@ import static com.gitee.sop.gatewaycommon.bean.SopConstants.SOP_SERVICE_ROUTE_PA
@Slf4j
public abstract class BaseRouteManager, E extends BaseRouteDefinition, T extends TargetRoute> implements RouteManager {
- protected String sopRouteRootPath = SOP_SERVICE_ROUTE_PATH;
-
protected Environment environment;
protected RouteRepository routeRepository;
+ protected String routeRootPath;
+
/**
* 返回路由根对象class
*
@@ -70,12 +70,15 @@ public abstract class BaseRouteManager, E exte
public BaseRouteManager(Environment environment, RouteRepository routeRepository) {
this.environment = environment;
this.routeRepository = routeRepository;
+ String profile = environment.getProperty("spring.profiles.active", "default");
+ this.routeRootPath = SOP_SERVICE_ROUTE_PATH + "-" + profile;
}
@Override
public void refresh() {
try {
String zookeeperServerAddr = environment.getProperty("spring.cloud.zookeeper.connect-string");
+ String profile = environment.getProperty("spring.profiles.active", "default");
if (StringUtils.isEmpty(zookeeperServerAddr)) {
throw new RuntimeException("未指定spring.cloud.zookeeper.connect-string参数");
}
@@ -91,9 +94,9 @@ public abstract class BaseRouteManager, E exte
.orSetData()
// 如果指定节点的父节点不存在,则Curator将会自动级联创建父节点
.creatingParentContainersIfNeeded()
- .forPath(sopRouteRootPath, "".getBytes());
+ .forPath(routeRootPath, "".getBytes());
- this.watchServiceChange(client, sopRouteRootPath);
+ this.watchServiceChange(client, routeRootPath);
} catch (Exception e) {
e.printStackTrace();
}
@@ -103,13 +106,13 @@ public abstract class BaseRouteManager, E exte
* 监听微服务更改
*
* @param client
- * @param sopRouteRootPath
+ * @param rootPath
* @throws Exception
*/
- protected void watchServiceChange(CuratorFramework client, String sopRouteRootPath) throws Exception {
+ protected void watchServiceChange(CuratorFramework client, String rootPath) throws Exception {
// 为子节点添加watcher
// PathChildrenCache: 监听数据节点的增删改,可以设置触发的事件
- PathChildrenCache childrenCache = new PathChildrenCache(client, sopRouteRootPath, true);
+ PathChildrenCache childrenCache = new PathChildrenCache(client, rootPath, true);
/**
* StartMode: 初始化方式
@@ -122,7 +125,7 @@ public abstract class BaseRouteManager, E exte
// 列出子节点数据列表,需要使用BUILD_INITIAL_CACHE同步初始化模式才能获得,异步是获取不到的
List childDataList = childrenCache.getCurrentData();
log.info("========== 加载路由信息 ==========");
- log.info("{} # 根节点", this.sopRouteRootPath);
+ log.info("{} # 根节点", rootPath);
for (ChildData childData : childDataList) {
String serviceNodeData = new String(childData.getData());
R serviceRouteInfo = JSON.parseObject(serviceNodeData, getServiceRouteInfoClass());
@@ -130,7 +133,7 @@ public abstract class BaseRouteManager, E exte
log.info("\t{} # service节点,节点数据:{}", servicePath, serviceNodeData);
this.loadServiceRouteItem(client, serviceRouteInfo, servicePath);
}
- log.info("监听服务节点增删改,rootPath:{}", this.sopRouteRootPath);
+ log.info("监听服务节点增删改,rootPath:{}", rootPath);
// 监听根节点下面的子节点
childrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
diff --git a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/SopRouteLocator.java b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/SopRouteLocator.java
index 5d341e07..e95a2f15 100644
--- a/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/SopRouteLocator.java
+++ b/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/route/SopRouteLocator.java
@@ -49,6 +49,9 @@ public class SopRouteLocator implements RouteLocator, Ordered {
if (zuulTargetRoute == null) {
return null;
}
+ if (zuulTargetRoute.getRouteDefinition().isDisabled()) {
+ return null;
+ }
return zuulTargetRoute.getTargetRouteDefinition();
}
diff --git a/sop-gateway/src/test/java/com/gitee/sop/gateway/CuratorTest.java b/sop-gateway/src/test/java/com/gitee/sop/gateway/CuratorTest.java
index 79173a81..40b6cc24 100644
--- a/sop-gateway/src/test/java/com/gitee/sop/gateway/CuratorTest.java
+++ b/sop-gateway/src/test/java/com/gitee/sop/gateway/CuratorTest.java
@@ -16,6 +16,7 @@ public class CuratorTest extends TestCase {
/**
* 递归删除节点,只能在测试环境用。
+ *
* @throws Exception
*/
public void testDel() throws Exception {
@@ -26,6 +27,21 @@ public class CuratorTest extends TestCase {
client.start();
- client.delete().deletingChildrenIfNeeded().forPath(SopConstants.SOP_SERVICE_ROUTE_PATH);
+ try {
+ client.delete().deletingChildrenIfNeeded().forPath(SopConstants.SOP_SERVICE_ROUTE_PATH);
+ } catch (Exception e) {
+ }
+ try {
+ client.delete().deletingChildrenIfNeeded().forPath(SopConstants.SOP_SERVICE_ROUTE_PATH + "-default");
+ } catch (Exception e) {
+ }
+ try {
+ client.delete().deletingChildrenIfNeeded().forPath(SopConstants.SOP_SERVICE_ROUTE_PATH + "-dev");
+ } catch (Exception e) {
+ }
+ try {
+ client.delete().deletingChildrenIfNeeded().forPath(SopConstants.SOP_SERVICE_ROUTE_PATH + "-test");
+ } catch (Exception e) {
+ }
}
}
diff --git a/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java b/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java
index c3784a71..af187fd3 100644
--- a/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java
+++ b/sop-service-common/src/main/java/com/gitee/sop/servercommon/manager/ServiceZookeeperApiMetaManager.java
@@ -31,7 +31,7 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
/**
* zookeeper存放接口路由信息的根目录
*/
- public static final String SOP_SERVICE_ROUTE_PATH = "/sop-service-route";
+ public static final String SOP_SERVICE_ROUTE_PATH = "/com.gitee.sop.route";
public static final String PATH_START_CHAR = "/";
/**
@@ -42,10 +42,14 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
private static ServiceApiInfo.ApiMeta FIRST_API_META = new ServiceApiInfo.ApiMeta("_first.route_", "/", "v_000");
+ private final String routeRootPath;
+
private Environment environment;
public ServiceZookeeperApiMetaManager(Environment environment) {
this.environment = environment;
+ String profile = environment.getProperty("spring.profiles.active", "default");
+ this.routeRootPath = SOP_SERVICE_ROUTE_PATH + "-" + profile;
}
@Override
@@ -70,6 +74,8 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
}
ServiceRouteInfo serviceRouteInfo = new ServiceRouteInfo();
serviceRouteInfo.setServiceId(serviceApiInfo.getServiceId());
+ String description = environment.getProperty("spring.application.description");
+ serviceRouteInfo.setDescription(description);
serviceRouteInfo.setRouteDefinitionList(routeDefinitionList);
return serviceRouteInfo;
}
@@ -133,13 +139,14 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
*/
protected void uploadServiceRouteInfoToZookeeper(ServiceRouteInfo serviceRouteInfo) {
String zookeeperServerAddr = environment.getProperty("spring.cloud.zookeeper.connect-string");
+ String profile = environment.getProperty("spring.profiles.active", "default");
if (StringUtils.isEmpty(zookeeperServerAddr)) {
throw new RuntimeException("未指定spring.cloud.zookeeper.connect-string参数");
}
CuratorFramework client = null;
try {
// 保存路径
- String savePath = SOP_SERVICE_ROUTE_PATH + "/" + serviceRouteInfo.getServiceId();
+ String savePath = routeRootPath + "/" + serviceRouteInfo.getServiceId();
client = CuratorFrameworkFactory.builder()
.connectString(zookeeperServerAddr)
@@ -172,7 +179,7 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
*/
protected String uploadFolder(CuratorFramework client, ServiceRouteInfo serviceRouteInfo) throws Exception {
// 保存路径
- String savePath = SOP_SERVICE_ROUTE_PATH + "/" + serviceRouteInfo.getServiceId();
+ String savePath = routeRootPath + "/" + serviceRouteInfo.getServiceId();
String serviceRouteInfoJson = JSON.toJSONString(serviceRouteInfo);
log.info("上传service目录到zookeeper,路径:{},内容:{}", savePath, serviceRouteInfoJson);
this.saveNode(client, savePath, serviceRouteInfoJson.getBytes());
diff --git a/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/GatewayRouteDefinition.java b/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/GatewayRouteDefinition.java
index 5d70d1be..7e898cb2 100644
--- a/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/GatewayRouteDefinition.java
+++ b/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/GatewayRouteDefinition.java
@@ -22,4 +22,9 @@ public class GatewayRouteDefinition {
private int order = 0;
/** 是否忽略验证,业务参数验证除外 */
private boolean ignoreValidate;
+
+ /**
+ * 是否禁用
+ */
+ private boolean disabled;
}
\ No newline at end of file
diff --git a/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/ServiceRouteInfo.java b/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/ServiceRouteInfo.java
index 1fcaba9d..aa85c235 100644
--- a/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/ServiceRouteInfo.java
+++ b/sop-service-common/src/main/java/com/gitee/sop/servercommon/route/ServiceRouteInfo.java
@@ -1,8 +1,10 @@
package com.gitee.sop.servercommon.route;
import com.alibaba.fastjson.annotation.JSONField;
+import com.gitee.easyopen.doc.annotation.ApiDocField;
import lombok.Data;
+import java.util.Date;
import java.util.List;
/**
@@ -12,6 +14,15 @@ import java.util.List;
public class ServiceRouteInfo {
/** 服务名称,对应spring.application.name */
private String serviceId;
+
+ @JSONField(format = "yyyy-MM-dd HH:mm:ss")
+ private Date createTime = new Date();
+
+ @JSONField(format = "yyyy-MM-dd HH:mm:ss")
+ private Date updateTime = new Date();
+
+ private String description;
+
@JSONField(serialize = false)
private List routeDefinitionList;
}
\ No newline at end of file
diff --git a/sop-story/sop-story-web/src/main/resources/bootstrap.yml b/sop-story/sop-story-web/src/main/resources/bootstrap.yml
index 37f39ac9..0a4dcf3e 100644
--- a/sop-story/sop-story-web/src/main/resources/bootstrap.yml
+++ b/sop-story/sop-story-web/src/main/resources/bootstrap.yml
@@ -1,6 +1,7 @@
spring:
application:
name: story-service
+ description: story服务
cloud:
zookeeper: