mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 12:56:28 +08:00
路由管理优化
This commit is contained in:
@@ -122,12 +122,13 @@ 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("获取service列表:");
|
log.info("========== 加载路由信息 ==========");
|
||||||
|
log.info("{} # 根节点", 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());
|
||||||
String servicePath = childData.getPath();
|
String servicePath = childData.getPath();
|
||||||
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:{}", sopRouteRootPath);
|
||||||
@@ -139,20 +140,20 @@ public abstract class BaseRouteManager<R extends BaseServiceRouteInfo<E>, E exte
|
|||||||
synchronized (type) {
|
synchronized (type) {
|
||||||
// 通过判断event type的方式来实现不同事件的触发
|
// 通过判断event type的方式来实现不同事件的触发
|
||||||
if (PathChildrenCacheEvent.Type.CHILD_ADDED.equals(type)) {
|
if (PathChildrenCacheEvent.Type.CHILD_ADDED.equals(type)) {
|
||||||
String nodeData = new String(event.getData().getData());
|
String serviceNodeData = new String(event.getData().getData());
|
||||||
R serviceRouteInfo = JSON.parseObject(nodeData, getServiceRouteInfoClass());
|
R serviceRouteInfo = JSON.parseObject(serviceNodeData, getServiceRouteInfoClass());
|
||||||
// 添加子节点时触发
|
// 添加子节点时触发
|
||||||
String servicePath = event.getData().getPath();
|
String servicePath = event.getData().getPath();
|
||||||
log.info("新增serviceId节点:{},数据为:{}", servicePath, nodeData);
|
log.info("新增serviceId节点:{},节点数据:{}", servicePath, serviceNodeData);
|
||||||
loadServiceRouteItem(client, serviceRouteInfo, servicePath);
|
loadServiceRouteItem(client, serviceRouteInfo, servicePath);
|
||||||
} else if (PathChildrenCacheEvent.Type.CHILD_UPDATED.equals(type)) {
|
} else if (PathChildrenCacheEvent.Type.CHILD_UPDATED.equals(type)) {
|
||||||
// 修改子节点数据时触发,暂时没有什么操作
|
// 修改子节点数据时触发,暂时没有什么操作
|
||||||
String nodeData = new String(event.getData().getData());
|
String nodeData = new String(event.getData().getData());
|
||||||
log.info("修改serviceId节点:{},数据为:{}", event.getData().getPath(), nodeData);
|
log.info("修改serviceId节点:{},节点数据:{}", event.getData().getPath(), nodeData);
|
||||||
} else if (PathChildrenCacheEvent.Type.CHILD_REMOVED.equals(type)) {
|
} else if (PathChildrenCacheEvent.Type.CHILD_REMOVED.equals(type)) {
|
||||||
// 删除service节点
|
// 删除service节点
|
||||||
String nodeData = new String(event.getData().getData());
|
String nodeData = new String(event.getData().getData());
|
||||||
log.info("删除serviceId节点:{},数据为:{}", event.getData().getPath(), nodeData);
|
log.info("删除serviceId节点:{},节点数据:{}", event.getData().getPath(), nodeData);
|
||||||
R serviceRouteInfo = JSON.parseObject(nodeData, getServiceRouteInfoClass());
|
R serviceRouteInfo = JSON.parseObject(nodeData, getServiceRouteInfoClass());
|
||||||
routeRepository.deleteAll(serviceRouteInfo.getServiceId());
|
routeRepository.deleteAll(serviceRouteInfo.getServiceId());
|
||||||
}
|
}
|
||||||
@@ -174,7 +175,7 @@ public abstract class BaseRouteManager<R extends BaseServiceRouteInfo<E>, E exte
|
|||||||
String routeItemPath = servicePath + "/" + pathName;
|
String routeItemPath = servicePath + "/" + pathName;
|
||||||
byte[] routeItemData = client.getData().forPath(routeItemPath);
|
byte[] routeItemData = client.getData().forPath(routeItemPath);
|
||||||
String routeDataJson = buildZookeeperData(routeItemData);
|
String routeDataJson = buildZookeeperData(routeItemData);
|
||||||
log.info("加载路由,path:{}, 数据为:{}", routeItemPath, routeDataJson);
|
log.info("\t\t{} # 路由节点,节点数据:{}", routeItemPath, routeDataJson);
|
||||||
this.saveRouteItem(serviceRouteInfo, routeDataJson);
|
this.saveRouteItem(serviceRouteInfo, routeDataJson);
|
||||||
}
|
}
|
||||||
this.watchRouteItems(client, serviceRouteInfo, servicePath);
|
this.watchRouteItems(client, serviceRouteInfo, servicePath);
|
||||||
@@ -201,17 +202,17 @@ public abstract class BaseRouteManager<R extends BaseServiceRouteInfo<E>, E exte
|
|||||||
if (PathChildrenCacheEvent.Type.CHILD_ADDED.equals(type)) {
|
if (PathChildrenCacheEvent.Type.CHILD_ADDED.equals(type)) {
|
||||||
// 新增单个路由
|
// 新增单个路由
|
||||||
String routeDataJson = buildZookeeperData(event.getData().getData());
|
String routeDataJson = buildZookeeperData(event.getData().getData());
|
||||||
log.info("新增单个路由,serviceId:{}, route:{}", serviceRouteInfo.getServiceId(), routeDataJson);
|
log.info("新增单个路由,serviceId:{}, 路由数据:{}", serviceRouteInfo.getServiceId(), routeDataJson);
|
||||||
saveRouteItem(serviceRouteInfo, routeDataJson);
|
saveRouteItem(serviceRouteInfo, routeDataJson);
|
||||||
} else if (PathChildrenCacheEvent.Type.CHILD_UPDATED.equals(type)) {
|
} else if (PathChildrenCacheEvent.Type.CHILD_UPDATED.equals(type)) {
|
||||||
// 修改单个路由
|
// 修改单个路由
|
||||||
String routeDataJson = buildZookeeperData(event.getData().getData());
|
String routeDataJson = buildZookeeperData(event.getData().getData());
|
||||||
log.info("修改单个路由,serviceId:{}, route:{}", serviceRouteInfo.getServiceId(), routeDataJson);
|
log.info("修改单个路由,serviceId:{}, 路由数据:{}", serviceRouteInfo.getServiceId(), routeDataJson);
|
||||||
updateRouteItem(serviceRouteInfo, routeDataJson);
|
updateRouteItem(serviceRouteInfo, routeDataJson);
|
||||||
} else if (PathChildrenCacheEvent.Type.CHILD_REMOVED.equals(type)) {
|
} else if (PathChildrenCacheEvent.Type.CHILD_REMOVED.equals(type)) {
|
||||||
// 删除单个路由
|
// 删除单个路由
|
||||||
String routeDataJson = buildZookeeperData(event.getData().getData());
|
String routeDataJson = buildZookeeperData(event.getData().getData());
|
||||||
log.info("删除单个路由,serviceId:{}, route:{}", serviceRouteInfo.getServiceId(), routeDataJson);
|
log.info("删除单个路由,serviceId:{}, 路由数据:{}", serviceRouteInfo.getServiceId(), routeDataJson);
|
||||||
deleteRouteItem(serviceRouteInfo, routeDataJson);
|
deleteRouteItem(serviceRouteInfo, routeDataJson);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -174,6 +174,7 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
|
|||||||
// 保存路径
|
// 保存路径
|
||||||
String savePath = SOP_SERVICE_ROUTE_PATH + "/" + serviceRouteInfo.getServiceId();
|
String savePath = SOP_SERVICE_ROUTE_PATH + "/" + serviceRouteInfo.getServiceId();
|
||||||
String serviceRouteInfoJson = JSON.toJSONString(serviceRouteInfo);
|
String serviceRouteInfoJson = JSON.toJSONString(serviceRouteInfo);
|
||||||
|
log.info("上传service目录到zookeeper,路径:{},内容:{}", savePath, serviceRouteInfoJson);
|
||||||
this.saveNode(client, savePath, serviceRouteInfoJson.getBytes());
|
this.saveNode(client, savePath, serviceRouteInfoJson.getBytes());
|
||||||
return savePath;
|
return savePath;
|
||||||
}
|
}
|
||||||
@@ -190,6 +191,7 @@ public class ServiceZookeeperApiMetaManager implements ApiMetaManager {
|
|||||||
// 父目录/子目录
|
// 父目录/子目录
|
||||||
String savePath = parentPath + PATH_START_CHAR + routeDefinition.getId();
|
String savePath = parentPath + PATH_START_CHAR + routeDefinition.getId();
|
||||||
String routeDefinitionJson = JSON.toJSONString(routeDefinition);
|
String routeDefinitionJson = JSON.toJSONString(routeDefinition);
|
||||||
|
log.info("上传路由配置到zookeeper,路径:{},路由数据:{}", savePath, routeDefinitionJson);
|
||||||
this.saveNode(client, savePath, routeDefinitionJson.getBytes());
|
this.saveNode(client, savePath, routeDefinitionJson.getBytes());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user