1. zookeeper 的java接口(1)
package com.rundy.zk;
import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.junit.Before;
import org.junit.Test;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.apache.zookeeper.ZooKeeper;
public class zkClient {
private static final String connectString = "mini1:2181,mini2:2181,mini3:2181";
private static final int sessionTimeout = 2000;
ZooKeeper zkClient = null;
@Before
public void init() throws Exception{
zkClient = new ZooKeeper(connectString, sessionTimeout,new Watcher() {
@Override
public void process(WatchedEvent event) {
// 收到事件通知后的回调函数,处理事件业务逻辑
System.out.println(event.getType()+"---"+event.getPath());
try {
zkClient.getChildren("/", true);
} catch (Exception e) {
}
}
});
}
/**
* 数据的增删改查
* @throws InterruptedException
* @throws KeeperException
*/
//创建数据节点到ZK中
public void textCreate() throws KeeperException, InterruptedException{
//参数1:节点的路径; 参数2:节点的数据; 参数3:节点的权限; 参数4:节点的类型
String nodeCreated = zkClient.create("/eclipse", "Hello".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
//创建子节点
@Test
public void getChilden() throws Exception{
List<String> children = zkClient.getChildren("/", true);
for(String str:children){
System.out.println(str);
}
Thread.sleep(Long.MAX_VALUE);
}
}