引进 maven 依赖
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
</dependency>
链接测试代码:
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.util.concurrent.CountDownLatch;
public class ZookeeperClientDemo {
private static final String CONNECTION_STRING = "127.0.0.1:2181";
private static final int SESSION_TIMEOUT = 5000;
private static CountDownLatch latch = new CountDownLatch(1);
public static void main(String[] args) throws Exception {
//创建Zookeeper链接
ZooKeeper zooKeeper = new ZooKeeper(CONNECTION_STRING, SESSION_TIMEOUT, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
if(watchedEvent.getState() == Event.KeeperState.SyncConnected){
latch.countDown();
}
}
});
latch.await();
//打印Zookeeper 客户端对象
System.out.println("=======" zooKeeper);
}
}
//以同步的方式列出根节点下的所有子节点
List<String> children = zooKeeper.getChildren("/",null);
children.forEach((string) -> {
System.out.println(string);
});
//以异步方式列出根节点下的所有子节点
zooKeeper.getChildren("/", null, new AsyncCallback.ChildrenCallback() {
@Override
public void processResult(int i, String s, Object o, List<String> children) {
children.forEach((node) -> {
System.out.println("异步方式:" node);
});
}
},null);
//确保异步回调方法被执行
Thread.sleep(Long.MAX_VALUE);
//同步方式判断节点是否存在
Stat stat = zooKeeper.exists("/phpfzh",null);
if(stat != null){
System.out.println("节点存在");
}else{
System.out.println("节点不存在");
}
//异步方式判断节点是否存在
zooKeeper.exists("/phpfzh", null, new AsyncCallback.StatCallback() {
@Override
public void processResult(int i, String s, Object o, Stat stat) {
if(stat != null){
System.out.println("节点存在");
}else {
System.out.println("节点不存在");
}
}
},null);
//同步方式创建持久节点,无ACL 权限 ,数据为phpnodehi
String nodeName = zooKeeper.create("/phpnode","phpnodehi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
System.out.println(nodeName);
//异步方式创建持久节点,无ACL 权限 ,数据为phpnodehi
zooKeeper.create("/phpnode2","phpnodehi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT,new AsyncCallback.StringCallback(){
@Override
public void processResult(int i, String s, Object o, String s1) {
System.out.println(s1);
}
},null);
//同步方式获取节点数据
byte[] data = zooKeeper.getData("/phpnode",null,null);
System.out.println(new String(data));
//异步方式获取节点数据
zooKeeper.getData("/phpnode2", null, new AsyncCallback.DataCallback() {
@Override
public void processResult(int i, String s, Object o, byte[] bytes, Stat stat) {
System.out.println(new String(bytes));
}
},null);
Thread.sleep(Long.MAX_VALUE);
//同步方式更新节点信息 更新节点信息需要指定版本号,如果更新最新版的节点数据,指定为-1 即可
Stat stat = zooKeeper.setData("/phpnode","update data".getBytes(),-1);
if(stat != null){
System.out.println("更新成功");
}else {
System.out.println("更新失败");
}
//异步方式更新节点信息 更新节点信息需要指定版本号,如果更新最新版的节点数据,指定为-1 即可
zooKeeper.setData("/phpnode", "update data".getBytes(), -1, new AsyncCallback.StatCallback() {
@Override
public void processResult(int i, String s, Object o, Stat stat) {
if(stat != null){
System.out.println("节点更新成功");
}else {
System.out.println("节点更新失败");
}
}
},null);
Thread.sleep(Long.MAX_VALUE);
//同步删除节点信息 该方法没有返回值,程序没有异常则说明删除成功
zooKeeper.delete("/phpnode",-1);
//异步删除节点信息
zooKeeper.delete("/phpnode2", -1, new AsyncCallback.VoidCallback() {
@Override
public void processResult(int i, String s, Object o) {
System.out.println(i == 0);
}
},null);
Thread.sleep(Long.MAX_VALUE);
常用API
//以同步方式列出子节点
List<String> getChildren(String path,Watcher watcher)
//以异步方式列出子节点 watcher 监视器,cb 回调方法,ctx 上下文对象
void getChildren(String path,Watcher watcher,ChildrenCallback cb,Object ctx)
//以异步方式列出子节点
void getChildren(String path,Watcher watcher,Children2Callback cb,Object ctx)
//以同步方式判断节点是否已存在
Stat exists(String path,Watcher watcher)
//以异步步方式判断节点是否已存在
void exists(String path,Watcher watcher,StatCallback cb,Object ctx)
//以同步方式创建节点 ,path 节点路径,data 节点数据,acl 权限
String create(String path,byte[] data,List<ACL> acl,CreateMode createMode)
//以异步方式创建节点 ,path 节点路径,data 节点数据,acl 权限 ,cb 回调方法,ctx 上下文对象
void create(String path,byte[] data,List<ACL> acl,CreateMode createMode,StringCallback cb,Object ctx)
//以同步方式获取节点数据
byte[] getData(String path,Watcher watcher,Stat stat)
//异步方式获取节点数据
void getData(String path,Watcher watcher,DataCallback cb,Object ctx)
//同步方式更新节点
Stat setData(String path,byte[] data,int version)
//异步方式更新节点
void setData(String path,byte[] data,int version,StatCallback cb,Object ctx)
//同步方式删除节点
void delete(String path,int version)
//异步方式删除节点
void delete(String path,int version,VoidCallback,cb,Object ctx)