路由管理优化

1.x
tanghc 6 years ago
parent 448b1ece1a
commit a38b8c1030
  1. 11
      sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/manager/BaseRouteManager.java

@ -74,7 +74,6 @@ public abstract class BaseRouteManager<R extends BaseServiceRouteInfo<E>, E exte
@Override @Override
public void refresh() { public void refresh() {
log.info("刷新本地接口信息");
try { try {
String zookeeperServerAddr = environment.getProperty("spring.cloud.zookeeper.connect-string"); String zookeeperServerAddr = environment.getProperty("spring.cloud.zookeeper.connect-string");
if (StringUtils.isEmpty(zookeeperServerAddr)) { if (StringUtils.isEmpty(zookeeperServerAddr)) {
@ -104,13 +103,13 @@ public abstract class BaseRouteManager<R extends BaseServiceRouteInfo<E>, E exte
* 监听微服务更改 * 监听微服务更改
* *
* @param client * @param client
* @param sopServiceApiPath * @param sopRouteRootPath
* @throws Exception * @throws Exception
*/ */
protected void watchServiceChange(CuratorFramework client, String sopServiceApiPath) throws Exception { protected void watchServiceChange(CuratorFramework client, String sopRouteRootPath) throws Exception {
// 为子节点添加watcher // 为子节点添加watcher
// PathChildrenCache: 监听数据节点的增删改,可以设置触发的事件 // PathChildrenCache: 监听数据节点的增删改,可以设置触发的事件
PathChildrenCache childrenCache = new PathChildrenCache(client, sopServiceApiPath, true); PathChildrenCache childrenCache = new PathChildrenCache(client, sopRouteRootPath, true);
/** /**
* StartMode: 初始化方式 * StartMode: 初始化方式
@ -123,7 +122,7 @@ public abstract class BaseRouteManager<R extends BaseServiceRouteInfo<E>, E exte
// 列出子节点数据列表,需要使用BUILD_INITIAL_CACHE同步初始化模式才能获得,异步是获取不到的 // 列出子节点数据列表,需要使用BUILD_INITIAL_CACHE同步初始化模式才能获得,异步是获取不到的
List<ChildData> childDataList = childrenCache.getCurrentData(); List<ChildData> childDataList = childrenCache.getCurrentData();
log.info("========== 加载路由信息 =========="); log.info("========== 加载路由信息 ==========");
log.info("{} # 根节点", sopRouteRootPath); log.info("{} # 根节点", this.sopRouteRootPath);
for (ChildData childData : childDataList) { for (ChildData childData : childDataList) {
String serviceNodeData = new String(childData.getData()); String serviceNodeData = new String(childData.getData());
R serviceRouteInfo = JSON.parseObject(serviceNodeData, getServiceRouteInfoClass()); R serviceRouteInfo = JSON.parseObject(serviceNodeData, getServiceRouteInfoClass());
@ -131,7 +130,7 @@ public abstract class BaseRouteManager<R extends BaseServiceRouteInfo<E>, E exte
log.info("\t{} # service节点,节点数据:{}", servicePath, serviceNodeData); log.info("\t{} # service节点,节点数据:{}", servicePath, serviceNodeData);
this.loadServiceRouteItem(client, serviceRouteInfo, servicePath); this.loadServiceRouteItem(client, serviceRouteInfo, servicePath);
} }
log.info("监听服务节点增删改,rootPath:{}", sopRouteRootPath); log.info("监听服务节点增删改,rootPath:{}", this.sopRouteRootPath);
// 监听根节点下面的子节点 // 监听根节点下面的子节点
childrenCache.getListenable().addListener(new PathChildrenCacheListener() { childrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override @Override

Loading…
Cancel
Save