package com.zhengmo.test;

import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper; /**
* @author zhengmo
*/
public class ZkMove {
private static int deleteCount = 0;
private static int moveCount = 0;
public static void main(String[] args) throws Exception {
//
//旧zk服务器
ZooKeeper oldzk = new ZooKeeper("192.168.1.112:2181", 60000, null);
//新zk服务器
ZooKeeper newzk = new ZooKeeper("172.17.32.105:2181", 60000, null);
//需要迁移的节点
String node = "/dubbo";
//删除指定节点
delete(newzk, node);
System.out.println("删除节点数:" + deleteCount);
//获取节点下的一级子节点
List<String> children = oldzk.getChildren(node, false);
move(oldzk, newzk, children, node);
System.out.println("移动节点数:" + moveCount);
oldzk.close();
newzk.close();
}
/**
* 递归删除指定节点的子节点.
* @param newzk zk
* @param node 节点
* @return 成功否
* @throws Exception 异常
*/
private static boolean delete(ZooKeeper newzk, String node) throws Exception {
List<String> children = newzk.getChildren(node, false);
if (children == null || children.size() == 0) {
//System.out.println("delete node:" + node);
newzk.delete(node, -1);
deleteCount++;
return true;
} else {
for (String child : children) {
while (!delete(newzk, node + "/" + child)) {
}
}
return false;
}
}
/**
* 移动指定zk1的指定节点到指定zk2的节点下.
* @param oldzk 旧zk
* @param newzk 新zk
* @param children 子节点
* @param parent 父节点
* @throws KeeperException 异常
* @throws InterruptedException 异常
*/
private static void move(ZooKeeper oldzk, ZooKeeper newzk, List<String> children, String parent) throws KeeperException, InterruptedException {
if (newzk.exists(parent, false) == null) {
newzk.create(parent, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("create " + parent);
}
if (children == null || children.isEmpty()) {
return;
} else {
for (String child : children) {
String c = parent + "/" + child;
//System.out.println(c);
byte[] data = oldzk.getData(c, false, null);
if (newzk.exists(c, false) == null) {
newzk.create(c, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
moveCount++;
} else {
newzk.setData(c, data, -1, null, null);
moveCount++;
}
//递归移动
move(oldzk, newzk, oldzk.getChildren(c, false), c);
}
}
}
}

zookeeper 有时候需要移动节点。

最新文章

  1. div层调整zindex属性无效原因分析及解决方法
  2. 阿里云服务器Linux CentOS安装配置(零)目录
  3. Java、Android 开发环境搭建
  4. mysql btree与hash索引的适用场景和限制
  5. ORA-14404
  6. 关于自然常数e的理解
  7. C++/C#/java开发支持求解
  8. Apache配置多个监听端口和不同的网站目录的简单方法(转)
  9. UILabel,UITextField 以及UIButton应用
  10. SQL整理5
  11. JavaScript使用button提交表单
  12. 使用Varnish+ESI实现静态页面的局部缓存(思路篇)
  13. 上传数据插件 Easy Populate 遇到问题 ?langer=remove
  14. matlab对文件目录进行自然排序
  15. jspsmart(保存文件)+poi(读取excel文件)操作excel文件
  16. 如何使用Android Studio在安卓平台对Unity开发的应用进行性能检查?
  17. leetcode刷题正则表达式
  18. python中的__len__,__getitem__ __setitem__ __delitem__ __contains__
  19. ssh 免密登陆
  20. centos下配置nginx遇到的一些基本的坑

热门文章

  1. PHP常用的文件操作函数集锦
  2. Android学习十一:高德地图使用
  3. REPEATABLE-READ下批量更新数据的问题
  4. ApiResponse 在 Swagger 1 和Swagger 2中的不同
  5. mysql之高可靠
  6. spring mvc fastJson 自定义类型转换(返回数据) 实现对ObjectId类型转换
  7. 浅入浅出EmguCv(二)EmguCv打开指定图片
  8. [2016.01.22]万峰文本处理专家 v2.1
  9. TCP打洞和UDP打洞的区别 (转)
  10. 去掉mac终端里面hostname提示处的bogon