1、pom文件
<dependencies>
<!--zookeeper-->
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!--其它-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
</dependencies>
2、创建会话
public class zookeeperTest {
public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";
public static void main(String[] args) throws IOException {
//创建一个会话,url,超时时间,监听机制
ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, null);
System.out.println(zooKeeper.getState());
}
}
参数表:
3、创建节点
需要注意几点:
1)、zookeeper不支持递归创建,没有父节点,不支持建立子节点。
2)、关于权限控制,如果你的应用场景没有太高的权限要求,那么就不用关注acl这个参数。
public class zookeeperTest {
public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";
public static void main(String[] args) throws Exception {
//创建一个会话,url,超时时间,监听机制
ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, null);
//创建一个节点,url,data,无权限,持久性
zooKeeper.create("/test1", "test1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("############创建成功############");
}
}
4、删除节点
public class zookeeperTest {
public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";
public static void main(String[] args) throws Exception {
//创建一个会话,url,超时时间,监听机制
ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, null);
zooKeeper.delete("/test1", 0);
}
}
5、读取数据
public class zookeeperTest {
public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";
public static void main(String[] args) throws Exception {
//创建一个会话,url,超时时间,监听机制
ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, null);
zooKeeper.create("/list", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT );
zooKeeper.create("/list/list1", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT );
zooKeeper.create("/list/list2", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT );
//获取/list下所有子节点,不注册监听
List<String> children = zooKeeper.getChildren("/list", false);
System.out.println(children);
}
}
6、监听
常用监听事件:
EventType.NodeCreated : 节点创建事件类型
EventType.NodeDeleted : 节点被删除
EventType.NodeDataChanged : 节点被修改
EventType.None : 客户端与服务器成功建立会话
EventType.NodeChildrenChanged : 子节点列表发生变更
public class zookeeperTest {
public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";
private static CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String[] args) throws Exception {
//创建一个会话,url,超时时间,监听机制
ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, new Watcher() {
//绑定监听事件
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
System.out.println("连接成功");
countDownLatch.countDown();
}
System.out.println(watchedEvent.getType());
}
});
countDownLatch.await();
zooKeeper.create("/list5", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zooKeeper.getChildren("/list5", true);
zooKeeper.create("/list5/list1", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
7、获取节点内容
public class zookeeperTest {
public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";
private static CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String[] args) throws Exception {
//创建一个会话,url,超时时间,监听机制
ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, new Watcher() {
//绑定监听事件
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
countDownLatch.countDown();
}
System.out.println(watchedEvent.getType());
}
});
countDownLatch.await();
Stat stat = new Stat();
//设置监听事件
zooKeeper.getData("/list", true, stat);
//修改值
zooKeeper.setData("/list", "lalal".getBytes(), -1);
}
}
8、检查节点是否存在
public class zookeeperTest {
public static final String ZOOKEEPER_SERVER = "192.168.1.38:2181";
private static CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String[] args) throws Exception {
//创建一个会话,url,超时时间,监听机制
ZooKeeper zooKeeper = new ZooKeeper(ZOOKEEPER_SERVER, 5000, new Watcher() {
//绑定监听事件
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
countDownLatch.countDown();
}
System.out.println(watchedEvent.getType());
}
});
countDownLatch.await();
zooKeeper.exists("/test", true);
zooKeeper.create("/test", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zooKeeper.delete("/test", -1);
}
}
9、ZkClient
9.1、pom
<!--zookeeper-->
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
9.2、demo
public class SessionDemo {
private final static String CONNECTIONSTR = "192.168.1.38:2181";
public static void main(String[] args) throws InterruptedException {
ZkClient zkClient = new ZkClient(CONNECTIONSTR, 4000);
//递归创建父节点
zkClient.createPersistent("/test/testa",true);
//递归删除
//zkClient.deleteRecursive("/test");
//获取子节点
List<String> children = zkClient.getChildren("/test");
System.out.println(children);
//监听test节点变化时
zkClient.subscribeDataChanges("/test", new IZkDataListener() {
public void handleDataChange(String s, Object o) throws Exception {
System.out.println("节点名称: " + s + "-> 节点修改后的值为" + o);
}
public void handleDataDeleted(String s) throws Exception {
}
});
zkClient.writeData("/test", "node1");
zkClient.writeData("/test", "value");
TimeUnit.SECONDS.sleep(2);
//当节点变化时
zkClient.subscribeChildChanges("/test", new IZkChildListener() {
public void handleChildChange(String s, List<String> list) throws Exception {
System.out.println("节点名称:" + s + "->" + "当前节点列表: " + list);
}
});
zkClient.delete("/test/testa");
TimeUnit.SECONDS.sleep(2);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/15174.html