博客
关于我
Zookeeper教程-4 Java API
阅读量:181 次
发布时间:2019-02-26

本文共 2377 字,大约阅读时间需要 7 分钟。

Zookeeper Java API

org.apache.zookeeper.Zookeeper

Zookeeper是在Java中客户端主类,负责建立与zookeeper集群的会话,并提供方法进行操作。
org.apache.zookeeper.Watcher
Watcher接口表示一个标准的事件处理器,其定义了事件通知相关的逻辑,包含KeeperState和EventType两个枚举类,分别代表了通知状态和事件类型,同时定义了事件的回调方法:process(WatchedEvent event)。

process方法是Watcher接口中的一个回调方法,当Zookeeper向客户端发送一个Watcher事件通知时,客户端就会对相应的process方法进行回调,从而实现对事件的处理。

基本使用

1. 搭建zookeeper2020项目

搭建zookeeper2020项目

2. 改pom

添加如下依赖:

org.apache.zookeeper
zookeeper
3.4.9

3. 启动类

创建com.antherd.Application

package com.antherd;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.ZooKeeper;public class Application {     public static void main(String[] args) throws Exception{       // 构造java zk客户端    ZooKeeper zk = new ZooKeeper(        "ip1:2181,ip2:2181,ip3:2181", 30000, new Watcher() {    // 多个ip使用‘,’隔开,注意‘,’后面不能添加空格      // 事件通知的回调方法      @Override      public void process(WatchedEvent event) {               System.out.println("Event State: " + event.getState());            System.out.println("Event Type: " + event.getType());            System.out.println("Event Path: " + event.getPath());      }    });    zk.close();  }}

运行main方法,发现控制台展示了Connect事件信息

事件监听

创建完zk对象后,添加一行创建节点的代码:

zk.create("/myGirls", "性感的".getBytes(StandardCharsets.UTF_8), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);

运行main方法,在Zookeeper服务端查看发现节点创建成功

Create Node
更多操作实例:

// 创建一个目录节点zk.create("/testRootPath", "testRootData".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);// 创建一个子目录节点zk.create("/testRootPath/testChildPathOne", "testChildDataOne".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);// 取出子目录节点列表// 取出节点数据System.out.println(new String(zk.getData("/testRootPath", false, null)));// 取出子目录节点列表,并且监听/testRootPath变化事件,多次修改只会产生一次事件System.out.println(zk.getChildren("/testRootPath", true));// 修改子目录节点数据zk.setData("/testRootPath/testChildPathOne", "modifyChildDataOne".getBytes(), -1);System.out.println("目录节点状态:[" + zk.exists("/testRootPath", true) + "]");// 创建另外一个子目录节点zk.create("/testRootPath/testChildPathTwo", "testChildDataTwo".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);System.out.println(new String(zk.getData("/testRootPath/getChildPathTwo", true, null)));// 删除子目录节点zk.delete("/testRootPath/testChildPathOne", -1);zk.delete("/testRootPath/testChildPathTwo", -1);// 删除父目录节点zk.delete("/testRootPath", -1);

转载地址:http://ldiz.baihongyu.com/

你可能感兴趣的文章
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>