package com.crown.simple;
import org.I0Itec.zkclient.ZkClient;
import org.junit.Test;
import java.util.List;
import java.util.UUID;
public class ZkClientTest {
private ZkClient zkClient = new ZkClient("localhost:2181");
/**
* 创建持久节点
*
* 如果节点已经存在将会抛出异常
*/
@Test
public void createPNode(){
if (!zkClient.exists("/crown"))
zkClient.createPersistent("/crown");
}
/**
* 创建临时节点
*
* 必须指明全路径
*/
@Test
public void tmpNode(){
String path = "/crown/redis";
if (!zkClient.exists(path)){
zkClient.createPersistent(path);
}
}
/**
* 创建数据节点
*
*/
@Test
public void crateDataNode(){
String data = UUID.randomUUID().toString().replaceAll("-","");
String path = "/crown/redis";
String dataPath = path + "/" + data ;
if (!zkClient.exists(dataPath)){
zkClient.createEphemeral(dataPath);
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 获取节点
*/
@Test
public void getNode(){
List<String> children = zkClient.getChildren("/crown/redis");
children.forEach(System.out::println);
}
}
ZkClient
最新推荐文章于 2024-09-11 00:05:05 发布