路由管理优化

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

@ -24,10 +24,10 @@ import static com.gitee.sop.gatewaycommon.bean.SopConstants.SOP_SERVICE_ROUTE_PA
* 路由管理采用zookeeper实现监听路由的增删改并适时更新到本地路由的存储格式为 * 路由管理采用zookeeper实现监听路由的增删改并适时更新到本地路由的存储格式为
* <pre> * <pre>
* /sop-service-route 根节点 * /sop-service-route 根节点
* /serviceId 服务节点名字为服务名 * /serviceId 服务节点名字为服务名
* /route1 路由节点名字为name+version存放路由信息 * /route1 路由节点名字为name+version存放路由信息
* /route2 * /route2
* /... * /...
* </pre> * </pre>
* *
* @param <R> 路由根对象可以理解为最外面的大json{....,routeDefinitionList:[]} * @param <R> 路由根对象可以理解为最外面的大json{....,routeDefinitionList:[]}
@ -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