1.新建Maven项目
2.导入依赖
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.9</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
3.API的使用
3.1 连接Zookeeper服务端
package com.biao.test;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Test;
import java.io.IOException;
public class Test1 {
private String connectString="192.168.134.129:2181,192.168.134.131:2181,192.168.134.132:2181";
private int sessionTimeout=5000;
/**
* 连接Zookeeper服务端
* @throws IOException
*/
@Test
public void test1() throws IOException {
ZooKeeper zooKeeper=new ZooKeeper(connectString, sessionTimeout, new Watcher() {
/**
* 触发监听事件的回调方法
* @param watchedEvent
*/
public void process(WatchedEvent watchedEvent) {
System.out.println("触发了....");
}
});
System.out.println("-->"+zooKeeper);
}
}
结果
3.2 创建节点
3.2.1 创建节点前将连接操作前置
private String connectString="192.168.134.129:2181,192.168.134.131:2181,192.168.134.132:2181";
private int sessionTimeout=100000;
ZooKeeper zooKeeper=null;
/**
* 连接Zookeeper服务端
* @throws IOException
*/
@Before
public void test1() throws IOException {
zooKeeper=new ZooKeeper(connectString, sessionTimeout, new Watcher() {
/**
* 触发监听事件的回调方法
* @param watchedEvent
*/
public void process(WatchedEvent watchedEvent) {
System.out.println("触发了....");
}
});
}
3.2.2 创建节点
/**
* 创建节点
* @throws Exception
*/
@Test
public void test2() throws Exception{
String path = zooKeeper.create("/apptest",//节点路径
"HelloZookeeper".getBytes(), //节点的数据
ZooDefs.Ids.OPEN_ACL_UNSAFE, //权限
CreateMode.PERSISTENT);//节点类型
System.out.println(path);
}
结果
3.3 判断节点是否存在
/**
* 判断节点是否存在
* @throws Exception
*/
@Test
public void exist() throws Exception{
//true表示的是使用Zookeeper中的watch
Stat exists = zooKeeper.exists("/apptest", true);
if (exists != null){
System.out.println("节点存在"+exists.getNumChildren());
}else {
System.out.println("节点不存在");
}
}
结果
3.4 获取某个节点下面的所有的子节点
/**
* 获取某个节点下面的所有的节点
* @throws Exception
*/
@Test
public void getChildrens() throws Exception{
List<String> childrens = zooKeeper.getChildren("/apptest", true);
for (String children:childrens
) {
//获取子节点中数据
byte[] data = zooKeeper.getData("/apptest/" + children, false, null);
System.out.println(children+":"+new String(data));
}
}
结果
3.5 修改节点的内容
/**
* 修改节点的内容
* @throws Exception
*/
@Test
public void setData() throws Exception{
//-1 不指定版本 自动维护
Stat stat = zooKeeper.setData("/apptest/a1", "setaaa".getBytes(), -1);
System.out.println(stat);
}
结果
3.6 删除节点(不支持删除非空)
/**
* 删除节点
* @throws Exception
*/
@Test
public void deleteNode() throws Exception{
zooKeeper.delete("/app3",-1);
}
结果
3.7 监听节点下的子节点的变更
/**
* 监听节点下的子节点变更
* @throws Exception
*/
@Test
public void nodeChildrenChange() throws Exception{
List<String> list = zooKeeper.getChildren("/apptest", new Watcher() {
/**
* None(-1),
* NodeCreated(1),
* NodeDeleted(2),
* NodeDataChanged(3),
* NodeChildrenChanged(4),
* DataWatchRemoved(5),
* ChildWatchRemoved(6);
* @param watchedEvent
*/
public void process(WatchedEvent watchedEvent) {
System.out.println("--->"+ watchedEvent.getType());
}
});
for (String s :
list) {
System.out.println(s);
}
Thread.sleep(Integer.MAX_VALUE);
}
3.8 监听节点内容变更
/*** 监听节点内容变更 */
@Test
public void nodeDataChanged() throws Exception{
byte[] data = zooKeeper.getData("/apptest/a1", new Watcher() {
public void process(WatchedEvent watchedEvent) {
System.out.println("--->" + watchedEvent.getType());
}
}, null);
System.out.println("--->" + new String(data));
Thread.sleep(Integer.MAX_VALUE);
}