博客
关于我
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/

你可能感兴趣的文章
Neat Stuff to Do in List Controls Using Custom Draw
查看>>
Necurs僵尸网络攻击美国金融机构 利用Trickbot银行木马窃取账户信息和欺诈
查看>>
Needle in a haystack: efficient storage of billions of photos 【转】
查看>>
NeHe OpenGL教程 07 纹理过滤、应用光照
查看>>
NeHe OpenGL教程 第四十四课:3D光晕
查看>>
Neighbor2Neighbor 开源项目教程
查看>>
neo4j图形数据库Java应用
查看>>
Neo4j图数据库_web页面关闭登录实现免登陆访问_常用的cypher语句_删除_查询_创建关系图谱---Neo4j图数据库工作笔记0013
查看>>
Neo4j图数据库的介绍_图数据库结构_节点_关系_属性_数据---Neo4j图数据库工作笔记0001
查看>>
Neo4j图数据库的数据模型_包括节点_属性_数据_关系---Neo4j图数据库工作笔记0002
查看>>
Neo4j安装部署及使用
查看>>
Neo4j电影关系图Cypher
查看>>
Neo4j的安装与使用
查看>>
Neo4j(1):图数据库Neo4j介绍
查看>>
Neo4j(2):环境搭建
查看>>
Neo4j(3):Neo4j Desktop安装
查看>>
Neo4j(4):Neo4j - CQL使用
查看>>
Neo图数据库与python交互
查看>>
NEO改进协议提案1(NEP-1)
查看>>
Neo私链
查看>>