Zookeeper命令与Java函数
1.引入jar包
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
2.连接ZK客户端
public class ZkHelper {
private static String connectString = "localhost:2181";
private static int sessionTimeout = 9000;
private static ZooKeeper zooKeeper = null;
final CountDownLatch countDownLatch = new CountDownLatch(1);
public ZooKeeper connect() throws InterruptedException, IOException {
System.out.println("ZK客户端初始化");
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == Event.KeeperState.SyncConnected) {
System.out.println("ZK客户端建立连接");
countDownLatch.countDown();
}
}
});
countDownLatch.await();
return zooKeeper;
}
public void close() throws InterruptedException {
zooKeeper.close();
}
public String getConnectString() {
return connectString;
}
}
-----------------------------------------
main(){
zkHelper = new ZkHelper();
zooKeeper = zkHelper.connect();
zkHelper.close();
System.out.println("ZK客户端运行完毕,关闭连接");
}
3.创建节点
private static ZkHelper zkHelper;
private static ZooKeeper zooKeeper;
public static void create(String path, byte[] data) throws KeeperException, InterruptedException {
String node = zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("创建节点:" + node);
}
------------------------------------------
main(){
String path = "/mynode1";
byte[] data = "hello mynode1".getBytes();
zkHelper = new ZkHelper();
zooKeeper = zkHelper.connect();
create(path, data);
zkHelper.close();
System.out.println("关闭ZK");
}
4.判断节点是否存在
public class Test03_nodeExist_stat_update {
private static ZkHelper zkHelper;
private static ZooKeeper zooKeeper;
private static CountDownLatch countDownLatch = new CountDownLatch(3);
public static Stat znodeExits(String path) throws KeeperException, InterruptedException {
return zooKeeper.exists(path, true);
}
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
String path = "/mynode1";
zkHelper = new ZkHelper();
zooKeeper = zkHelper.connect();
Stat stat = znodeExits(path);
if (stat == null) {
System.out.println("节点不存在!");
return;
}
System.out.println("**********************");
System.out.println("初始状态:" + ZnodeUtil.printZnodeInfo(stat));
System.out.println("**********************");
byte[] bytes = zooKeeper.getData(path, new MyWatcher(path, countDownLatch, zooKeeper), stat);
String da = new String(bytes, "UTF-8");
System.out.println("主线程获取节点" + path + " 原始数据 " + da);
System.out.println(path + " 新信息");
System.out.println(ZnodeUtil.printZnodeInfo(stat));
countDownLatch.await();
zkHelper.close();
System.out.println("关闭ZK");
}
}
class MyWatcher implements Watcher {
private String path;
private CountDownLatch countDownLatch;
private ZooKeeper zooKeeper;
public MyWatcher(String path, CountDownLatch countDownLatch, ZooKeeper zooKeeper) {
this.path = path;
this.countDownLatch = countDownLatch;
this.zooKeeper = zooKeeper;
}
@SneakyThrows
@Override
public void process(WatchedEvent event) {
if (event.getType() == Event.EventType.NodeDataChanged) {
Stat stat = new Stat();
byte[] data = zooKeeper.getData(path, MyWatcher.this, stat);
String da = new String(data, "UTF-8");
System.out.println(path + " 新数据 " + da);
System.out.println(path + " 新信息");
System.out.println(ZnodeUtil.printZnodeInfo(stat));
countDownLatch.countDown();
} else {
System.out.println("事件类型:" + event.getType());
}
}
}
5.更新节点
public static void update(String path, byte[] data) throws KeeperException, InterruptedException {
Stat stat = zooKeeper.exists(path, true);
zooKeeper.setData(path, data, stat.getVersion());
}
6.获取子节点
public static Stat znodeExits(String path) throws KeeperException, InterruptedException {
return zooKeeper.exists(path, true);
}
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
String path = "/";
zkHelper = new ZkHelper();
zooKeeper = zkHelper.connect();
Stat stat = znodeExits(path);
if (stat == null) {
System.out.println("子节点不存在");
return;
}
List<String> list = zooKeeper.getChildren(path, true);
list.forEach(System.out::println);
zkHelper.close();
System.out.println("关闭ZK");
}
public static Stat znodeExits(String path) throws KeeperException, InterruptedException {
return zooKeeper.exists(path, true);
}
public static void showChildNode(String path, int level) throws Exception {
Stat stat = znodeExits(path);
if (stat == null) {
System.out.println("子节点不存在");
return;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < level; i++) {
sb.append(" ");
}
System.out.println(sb.toString() + path);
List<String> list;
try {
list = zooKeeper.getChildren(path, true);
} catch (Exception e) {
return;
}
list.forEach(childNode -> {
if (level == 0) {
try {
showChildNode(path + childNode, level + 1);
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
showChildNode(path + "/" + childNode, level + 1);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public static void main(String[] args) throws Exception {
String path = "/";
zkHelper = new ZkHelper();
zooKeeper = zkHelper.connect();
showChildNode(path, 0);
zkHelper.close();
System.out.println("关闭ZK");
}
7.删除节点
public static void delete(String path) throws KeeperException, InterruptedException {
zooKeeper.delete(path, zooKeeper.exists(path, true).getVersion());
}