【README】
java本地连接zk cluster, refer to https://blog.csdn.net/PacosonSWJTU/article/details/111404364;
【1】 创建zk节点 star (明星节点)
/**
* 测试 zk
* @author pacoson
*
*/
public class TestZK {
/**
* zk server 连接串
*/
private String connectString = "192.168.163.201:2181,192.168.163.202:2181,192.168.163.203:2181";
/**
* 超时时间
*/
private int sessionTimeout = 3000;
/**
* zk客户端实例
*/
private ZooKeeper zkClient;
/**
* 0-获取zk连接
* @throws IOException
*/
@Before
public void init() throws IOException {
/* 连接zk服务器 */
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
}
});
}
/**
* 1-创建zk节点
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void createNode() throws KeeperException, InterruptedException {
String path = zkClient.create("/star", "star list".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(path);
}
}
【2】我们可以 在 linux zk 客户端查看其创建的节点