主要jar包在主目录下
项目需要的相关依赖的jar包在zookeeper的解压文件的lib目录下就有
<dependencies>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.6version>
<type>pomtype>
dependency>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
ps:一定要把超时时间设置长一点,不然显示连接成功,但操作可能失败,还一直找不要原因。
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
/**
* @author: cjw
* @date 2019/8/8 10:33
* 功能:
*/
public class Test01 {
// zookeeper的服务器地址
// private String connectString = "192.168.197.131:2181,192.168.197.132:2181,192.168.197.133:2181";
private String connectString = "192.168.197.131:2181,192.168.197.132:2181";
// 连接超时时间,足够长(必须)
private int sessionTimeout = 20000;
private ZooKeeper zk = null;
/**
* 设置zookeeper对象
* @throws IOException
*/
@Before
public void setZookeeper() throws IOException {
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
/**
* 事件触发的回调方法
*/
@Override
public void process(WatchedEvent event) {
}
});
System.out.println("---"+zk);
}
/**
* 功能:测试连接成功
*/
@Test
public void Test(){
ZooKeeper.States state = zk.getState();
System.out.println(state.isAlive());
System.out.println(state.toString());
System.out.println(state.name());
}
}
Test()运行截图
/**
* create 方法参数
* 第一个参数 路径
* 第二个参数 值 bytes
* 第三个参数 对节点的访问控制
* 第四个参数 节点的类型 短暂 永久 序号
* @throws KeeperException
* @throws InterruptedException
*/
@Test
public void create() throws KeeperException, InterruptedException {
String s = zk.create("/node5", "cjw".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(s);
}
对节点的访问控制
选项 | 含义 |
---|---|
ZooDefs.Ids.OPEN_ACL_UNSAFE | 开放的ACL不安全 |
ZooDefs.Ids.ANYONE_ID_UNSAFE | 任何ID不安全 |
ZooDefs.Ids.AUTH_IDS | 身份验证id |
ZooDefs.Ids.CREATOR_ALL_ACL | 创造者所有ACL |
ZooDefs.Ids.READ_ACL_UNSAFE | 读ACL不安全 |
/**
* 判断节点是否存在
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void exit() throws KeeperException, InterruptedException {
// 设置为 true 会调用 zk中的监听器
Stat exists = zk.exists("/node5", true);
if(exists == null) {
System.out.println("该节点不存在!");
} else {
System.out.println("该节点存在!");
System.out.println(exists.getDataLength());
}
}
/**
* 获取子节点
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void getchildrens() throws KeeperException, InterruptedException {
List<String> children = zk.getChildren("/", true);
for (int i = 0; i < children.size(); i++) {
String s = children.get(i);
System.out.println(s);
}
}
/**
* 获取节点的内容
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void getData() throws KeeperException, InterruptedException {
byte[] data = zk.getData("/node3", true, null);
System.out.println(new String(data));
}
/**
* 修改节点内容
* version -1 自动维护
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void update() throws KeeperException, InterruptedException {
Stat stat = zk.setData("/node3", "相对论".getBytes(), -1);
System.out.println(stat.getVersion());
}
/**
* 删除节点
* 非空节点删除不掉
* @throws InterruptedException
* @throws KeeperException
*/
@Test
public void remove() throws KeeperException, InterruptedException {
zk.delete("/node3", 2);
}
监听器是某些方法的必须参数,实现一个 Watcher
接口内部类即可。
在创建连接的时候,可以自定义一个监听器。方便以后用
/**
* 设置zookeeper对象
* @throws IOException
*/
@Before
public void setZookeeper() throws IOException {
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
/**
* 事件触发的回调方法
*/
@Override
public void process(WatchedEvent event) {
}
});
System.out.println("---"+zk);
}
节点的数量发送改变时,触发监听器。
/**
* 监听事件:子节点的数量发生改变时触发(只会触发一次)
*/
@Test
public void getchildrens2() throws KeeperException, InterruptedException {
List<String> children = zk.getChildren("/", new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("子节点的数量发生改变!");
}
});
for (int i = 0; i < children.size(); i++) {
String s = children.get(i);
System.out.println(s);
}
Thread.sleep(Long.MAX_VALUE);
}
/**
* 监听事件:节点数据发送改变时触发(只会触发一次)
*/
@Test
public void getdata() throws KeeperException, InterruptedException {
byte[] data = zk.getData("/name2", new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("节点数据发送了改变!");
}
}, null);
System.out.println(new String(data));
Thread.sleep(Long.MAX_VALUE);
}
项目打包 提取码:qxj7
环境:idea