补充watchZk中节点更新的逻辑
This commit is contained in:
parent
a0b7918c7a
commit
d00cd159d3
@ -27,6 +27,16 @@ public class serviceCache {
|
||||
cache.put(serviceName,addressList);
|
||||
}
|
||||
}
|
||||
//修改服务地址
|
||||
public void replaceServiceAddress(String serviceName,String oldAddress,String newAddress){
|
||||
if(cache.containsKey(serviceName)){
|
||||
List<String> addressList=cache.get(serviceName);
|
||||
addressList.remove(oldAddress);
|
||||
addressList.add(newAddress);
|
||||
}else {
|
||||
System.out.println("修改失败,服务不存在");
|
||||
}
|
||||
}
|
||||
//从缓存中取服务地址
|
||||
public List<String> getServcieFromCache(String serviceName){
|
||||
if(!cache.containsKey(serviceName)) {
|
||||
|
||||
@ -39,10 +39,7 @@ public class watchZK {
|
||||
// 节点创建时没有赋予值 create /curator/app1 只创建节点,在这种情况下,更新前节点的 data 为 null,获取不到更新前节点的数据
|
||||
switch (type.name()) {
|
||||
case "NODE_CREATED": // 监听器第一次执行时节点存在也会触发次事件
|
||||
//获取更新的节点的路径
|
||||
String path=new String(childData1.getPath());
|
||||
//按照格式 ,读取
|
||||
String[] pathList= path.split("/");
|
||||
String[] pathList= pasrePath(childData1);
|
||||
if(pathList.length<=2) break;
|
||||
else {
|
||||
String serviceName=pathList[1];
|
||||
@ -57,12 +54,13 @@ public class watchZK {
|
||||
} else {
|
||||
System.out.println("节点第一次赋值!");
|
||||
}
|
||||
String[] oldPathList=pasrePath(childData);
|
||||
String[] newPathList=pasrePath(childData1);
|
||||
cache.replaceServiceAddress(oldPathList[1],oldPathList[2],newPathList[2]);
|
||||
System.out.println("修改后的数据: " + new String(childData1.getData()));
|
||||
break;
|
||||
case "NODE_DELETED": // 节点删除
|
||||
String path_d=new String(childData.getPath());
|
||||
//按照格式 ,读取
|
||||
String[] pathList_d= path_d.split("/");
|
||||
String[] pathList_d= pasrePath(childData);
|
||||
if(pathList_d.length<=2) break;
|
||||
else {
|
||||
String serviceName=pathList_d[1];
|
||||
@ -79,4 +77,11 @@ public class watchZK {
|
||||
//开启监听
|
||||
curatorCache.start();
|
||||
}
|
||||
//解析节点对应地址
|
||||
public String[] pasrePath(ChildData childData){
|
||||
//获取更新的节点的路径
|
||||
String path=new String(childData.getPath());
|
||||
//按照格式 ,读取
|
||||
return path.split("/");
|
||||
}
|
||||
}
|
||||
@ -30,6 +30,16 @@ public class serviceCache {
|
||||
cache.put(serviceName,addressList);
|
||||
}
|
||||
}
|
||||
//修改服务地址
|
||||
public void replaceServiceAddress(String serviceName,String oldAddress,String newAddress){
|
||||
if(cache.containsKey(serviceName)){
|
||||
List<String> addressList=cache.get(serviceName);
|
||||
addressList.remove(oldAddress);
|
||||
addressList.add(newAddress);
|
||||
}else {
|
||||
System.out.println("修改失败,服务不存在");
|
||||
}
|
||||
}
|
||||
//从缓存中取服务地址列表
|
||||
public List<String> getServiceListFromCache(String serviceName){
|
||||
if(!cache.containsKey(serviceName)) {
|
||||
|
||||
@ -17,7 +17,7 @@ public class watchZK {
|
||||
//本地缓存
|
||||
serviceCache cache;
|
||||
|
||||
public watchZK(CuratorFramework client,serviceCache cache){
|
||||
public watchZK(CuratorFramework client, serviceCache cache){
|
||||
this.client=client;
|
||||
this.cache=cache;
|
||||
}
|
||||
@ -39,10 +39,7 @@ public class watchZK {
|
||||
// 节点创建时没有赋予值 create /curator/app1 只创建节点,在这种情况下,更新前节点的 data 为 null,获取不到更新前节点的数据
|
||||
switch (type.name()) {
|
||||
case "NODE_CREATED": // 监听器第一次执行时节点存在也会触发次事件
|
||||
//获取更新的节点的路径
|
||||
String path=new String(childData1.getPath());
|
||||
//按照格式 ,读取
|
||||
String[] pathList= path.split("/");
|
||||
String[] pathList= pasrePath(childData1);
|
||||
if(pathList.length<=2) break;
|
||||
else {
|
||||
String serviceName=pathList[1];
|
||||
@ -57,12 +54,13 @@ public class watchZK {
|
||||
} else {
|
||||
System.out.println("节点第一次赋值!");
|
||||
}
|
||||
String[] oldPathList=pasrePath(childData);
|
||||
String[] newPathList=pasrePath(childData1);
|
||||
cache.replaceServiceAddress(oldPathList[1],oldPathList[2],newPathList[2]);
|
||||
System.out.println("修改后的数据: " + new String(childData1.getData()));
|
||||
break;
|
||||
case "NODE_DELETED": // 节点删除
|
||||
String path_d=new String(childData.getPath());
|
||||
//按照格式 ,读取
|
||||
String[] pathList_d= path_d.split("/");
|
||||
String[] pathList_d= pasrePath(childData);
|
||||
if(pathList_d.length<=2) break;
|
||||
else {
|
||||
String serviceName=pathList_d[1];
|
||||
@ -79,4 +77,11 @@ public class watchZK {
|
||||
//开启监听
|
||||
curatorCache.start();
|
||||
}
|
||||
//解析节点对应地址
|
||||
public String[] pasrePath(ChildData childData){
|
||||
//获取更新的节点的路径
|
||||
String path=new String(childData.getPath());
|
||||
//按照格式 ,读取
|
||||
return path.split("/");
|
||||
}
|
||||
}
|
||||
@ -27,6 +27,16 @@ public class serviceCache {
|
||||
cache.put(serviceName,addressList);
|
||||
}
|
||||
}
|
||||
//修改服务地址
|
||||
public void replaceServiceAddress(String serviceName,String oldAddress,String newAddress){
|
||||
if(cache.containsKey(serviceName)){
|
||||
List<String> addressList=cache.get(serviceName);
|
||||
addressList.remove(oldAddress);
|
||||
addressList.add(newAddress);
|
||||
}else {
|
||||
System.out.println("修改失败,服务不存在");
|
||||
}
|
||||
}
|
||||
//从缓存中取服务地址列表
|
||||
public List<String> getServiceListFromCache(String serviceName){
|
||||
if(!cache.containsKey(serviceName)) {
|
||||
|
||||
@ -17,6 +17,7 @@ public class watchZK {
|
||||
//本地缓存
|
||||
serviceCache cache;
|
||||
|
||||
|
||||
public watchZK(CuratorFramework client,serviceCache cache){
|
||||
this.client=client;
|
||||
this.cache=cache;
|
||||
@ -39,10 +40,7 @@ public class watchZK {
|
||||
// 节点创建时没有赋予值 create /curator/app1 只创建节点,在这种情况下,更新前节点的 data 为 null,获取不到更新前节点的数据
|
||||
switch (type.name()) {
|
||||
case "NODE_CREATED": // 监听器第一次执行时节点存在也会触发次事件
|
||||
//获取更新的节点的路径
|
||||
String path=new String(childData1.getPath());
|
||||
//按照格式 ,读取
|
||||
String[] pathList= path.split("/");
|
||||
String[] pathList= pasrePath(childData1);
|
||||
if(pathList.length<=2) break;
|
||||
else {
|
||||
String serviceName=pathList[1];
|
||||
@ -57,12 +55,13 @@ public class watchZK {
|
||||
} else {
|
||||
System.out.println("节点第一次赋值!");
|
||||
}
|
||||
String[] oldPathList=pasrePath(childData);
|
||||
String[] newPathList=pasrePath(childData1);
|
||||
cache.replaceServiceAddress(oldPathList[1],oldPathList[2],newPathList[2]);
|
||||
System.out.println("修改后的数据: " + new String(childData1.getData()));
|
||||
break;
|
||||
case "NODE_DELETED": // 节点删除
|
||||
String path_d=new String(childData.getPath());
|
||||
//按照格式 ,读取
|
||||
String[] pathList_d= path_d.split("/");
|
||||
String[] pathList_d= pasrePath(childData);
|
||||
if(pathList_d.length<=2) break;
|
||||
else {
|
||||
String serviceName=pathList_d[1];
|
||||
@ -79,4 +78,11 @@ public class watchZK {
|
||||
//开启监听
|
||||
curatorCache.start();
|
||||
}
|
||||
//解析节点对应地址
|
||||
public String[] pasrePath(ChildData childData){
|
||||
//获取更新的节点的路径
|
||||
String path=new String(childData.getPath());
|
||||
//按照格式 ,读取
|
||||
return path.split("/");
|
||||
}
|
||||
}
|
||||
@ -27,6 +27,16 @@ public class serviceCache {
|
||||
cache.put(serviceName,addressList);
|
||||
}
|
||||
}
|
||||
//修改服务地址
|
||||
public void replaceServiceAddress(String serviceName,String oldAddress,String newAddress){
|
||||
if(cache.containsKey(serviceName)){
|
||||
List<String> addressList=cache.get(serviceName);
|
||||
addressList.remove(oldAddress);
|
||||
addressList.add(newAddress);
|
||||
}else {
|
||||
System.out.println("修改失败,服务不存在");
|
||||
}
|
||||
}
|
||||
//从缓存中取服务地址列表
|
||||
public List<String> getServiceListFromCache(String serviceName){
|
||||
if(!cache.containsKey(serviceName)) {
|
||||
|
||||
@ -17,6 +17,7 @@ public class watchZK {
|
||||
//本地缓存
|
||||
serviceCache cache;
|
||||
|
||||
|
||||
public watchZK(CuratorFramework client,serviceCache cache){
|
||||
this.client=client;
|
||||
this.cache=cache;
|
||||
@ -39,10 +40,7 @@ public class watchZK {
|
||||
// 节点创建时没有赋予值 create /curator/app1 只创建节点,在这种情况下,更新前节点的 data 为 null,获取不到更新前节点的数据
|
||||
switch (type.name()) {
|
||||
case "NODE_CREATED": // 监听器第一次执行时节点存在也会触发次事件
|
||||
//获取更新的节点的路径
|
||||
String path=new String(childData1.getPath());
|
||||
//按照格式 ,读取
|
||||
String[] pathList= path.split("/");
|
||||
String[] pathList= pasrePath(childData1);
|
||||
if(pathList.length<=2) break;
|
||||
else {
|
||||
String serviceName=pathList[1];
|
||||
@ -57,12 +55,13 @@ public class watchZK {
|
||||
} else {
|
||||
System.out.println("节点第一次赋值!");
|
||||
}
|
||||
String[] oldPathList=pasrePath(childData);
|
||||
String[] newPathList=pasrePath(childData1);
|
||||
cache.replaceServiceAddress(oldPathList[1],oldPathList[2],newPathList[2]);
|
||||
System.out.println("修改后的数据: " + new String(childData1.getData()));
|
||||
break;
|
||||
case "NODE_DELETED": // 节点删除
|
||||
String path_d=new String(childData.getPath());
|
||||
//按照格式 ,读取
|
||||
String[] pathList_d= path_d.split("/");
|
||||
String[] pathList_d= pasrePath(childData);
|
||||
if(pathList_d.length<=2) break;
|
||||
else {
|
||||
String serviceName=pathList_d[1];
|
||||
@ -79,4 +78,11 @@ public class watchZK {
|
||||
//开启监听
|
||||
curatorCache.start();
|
||||
}
|
||||
//解析节点对应地址
|
||||
public String[] pasrePath(ChildData childData){
|
||||
//获取更新的节点的路径
|
||||
String path=new String(childData.getPath());
|
||||
//按照格式 ,读取
|
||||
return path.split("/");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user