聊聊、Zookeeper API
今天我们来说说 Zookeeper 客户端启动,整个文章分三个部分:第一部分是 Zookeeper 原生 API 客户端,第二部分是开源客户端 ZkClient,第三部分是开源客户端 Curator。
【Zookeeper API】
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
创建会话
- new ZooKeeper(String connectString,int sessionTimeOut,Watcher watcher)
- new ZooKeeper(String connectString,int sessionTimeOut,Watcher watcher,boolean canBeReadOnly)
- new ZooKeeper(String connectString,int sessionTimeOut,Watcher watcher,long sessionId,byte[] sessionPasswd)
- new ZooKeeper(String connectString,int sessionTimeOut,Watcher watcher,long sessionId,byte[] sessionPasswd,boolean canBeReadOnly)
- 上面四个构造函数都可以创建会话,不同的在于构造参数。
- connectString 就是 host:port 字符串,例如 127.0.0.1:2181,也可以是多个,192.168.0.10:2181,192.168.0.11:2181,192.168.0.12:2181,多个地址表示集群。我们也可以指定到根目录,例如 192.168.0.10:2181/test,这样 Zookeeper服务连接后,所有的操作是针对 test 根目录。
- sessionTimeOut 就是会话超时时间,单位是毫秒。如果在会话超时时间内没有检查到心跳,则会话失效。
- watcher 就是监听器,也可以说是通知,如果传给构造函数 watcher 不为空,则会话创建成功后会有通知到客户端。收到通知后,客户端可以做一些事情。但是 watcher 的生命周期很短,通知一次过后就失效了,需要反复的注册。
- canBeReadOnly 是一个 boolean 类型,如果是 true,则当 Zookeeper 服务器故障,服务器还可以提供读服务。
- sessionId 就是会话序列号,sessionPasswd 就是会话密码,它们的作用就是会话复用,可以达到恢复会话作用。
创建节点
- String create(final String path,byte data[],List<ACL> acl,CreateMode createMode)
- void create(final String path,byte data[],List<ACL> acl,CreateMode createMode,StringCallback cb,Object ctx)
- path 就是要创建的节点路径,例如 /app,在根节点 / 下创建 app 节点。
- data[] 就是节点内容,字节数组形式。
- acl 就是节点 ACL 策略。
- createMode 就是节点类型,分为持久,持久顺序,临时,临时顺序。
- cb 就是异步回调方法。实现 StringCallback 接口即可。
- ctx 用于传递一个对象,一般是上下文信息。
package zookeeper.client; import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState; public class ZKClient { private static CountDownLatch down = new CountDownLatch(1); private ZooKeeper zk; public void getNode(){
String path = "/";
try {
zk = new ZooKeeper("127.0.0.1:2181", 5000, new MyWatcher());
down.await();
zk.create(path+"app", "hello".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} class MyWatcher implements Watcher{
public void process(WatchedEvent e) {
if(KeeperState.SyncConnected == e.getState()){
System.out.println("SyncConnected");
if(EventType.None == e.getType() && null == e.getPath()){
down.countDown();
}else if(e.getType() == EventType.NodeChildrenChanged){
try {
System.out.println("NodeChildrenChanged"+zk.getChildren("/", true));
} catch (KeeperException e1) {
e1.printStackTrace();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} } } }

删除节点
- public void delete(final String path,int version)
- public void delete(final String path,int version,VoidCallback cb,Object ctx)
- path 就是要删除的节点路径。
- version 就是节点版本号。
- cb 就是回调方法。
- ctx 就是传递上下文信息对象。
读取节点
(一)、getChildren 获取一个节点所有子节点。
- List<String> getChildren(final String path,Watcher watcher)
- List<String> getChildren(final String path,boolean watcher)
- void getChildren(final String path,Watcher watcher,ChildrenCallback cb,Object ctx)
- void getChildren(final String path,boolean watcher,ChildrenCallback cb,Object ctx)
- List<String> getChildren(final String path,Watcher watcher,Stat stat)
- List<String> getChildren(final String path,boolean watcher,Stat stat)
(二)、getData 获取一个节点内容。
- byte[] getData(final String path,Watcher watcher,Stat stat)
- byte[] getData(final String path,boolean watcher,Stat stat)
- void getData(final String path,Watcher watcher,DataCallback cb,Object ctx)
- void getData(final String path,boolean watcher,DataCallback cb,Object ctx)
- stat 就是节点的状态信息。包括 cZxid、mZxid,pZxid等。
更新节点
- Stat setData(final String path,byte data[],int version)
- void setData(final String path,byte data[],int version,StatCallback cb,Object ctx)
检测节点是否存在
- public Stat exists(final String path,Watcher watcher)
- public Stat exists(final String path,boolean watch)
- public void exists(final String path,Watcher watcher,StatCallback cb,Object ctx)
- public void exists(final String path,boolean watch,StatCallback cb,Object ctx)
谢谢大家观看!
聊聊、Zookeeper API的更多相关文章
- ZooKeeper系列4:ZooKeeper API简介及编程
问题导读: 1.ZooKeeper API 共包含几个包? 2.如何使用ZooKeeper API 创建zookeeper应用程序? 1)ZooKeeper API 简介 ZooKeeper AP ...
- 面向服务的体系架构 SOA(三) --- Zookeeper API、zkClient API的使用
zookeeper简单介绍及API使用 1.1 zookeeper简介 zookeeper是一个针对大型分布式系统的可靠的协调系统,提供的功能包括配置维护.名字服务.分布式同步.组服务等.zookee ...
- Hbase记录-ZooKeeper API
Zookeeper API ZooKeeper有一个Java和C绑定的官方API.ZooKeeper社区提供了对于大多数语言(.NET,Python等)的非官方API.使用ZooKeeper的API, ...
- Zookeeper 系列(三)Zookeeper API
Zookeeper 系列(三)Zookeeper API 本节首先介绍 Zookeeper 的 Shell 命令,再对 Java 操作 Zookeeper 的三种方式进行讲解,本节先介绍 Zookee ...
- Zookeeper api增删改查节点
Exists - 检查Znode的存在 ZooKeeper类提供了 exists 方法来检查znode的存在.如果指定的znode存在,则返回一个znode的元数据.exists方法的签名如下: ex ...
- Zookeeper Api(java)入门与应用(转)
如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...
- zookeeper[3] zookeeper API开发注意事项总结
如下是根据官方接口文档(http://zookeeper.apache.org/doc/r3.4.1/api/org/apache/zookeeper/ZooKeeper.html#register( ...
- Zookeeper Api
如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...
- 聊聊zookeeper的分布式锁
分布式锁就是多台机器,分布在不同的JVM中,这些不同JVM内的方法需要获取一个唯一锁,比如获取锁之后要把数据写入数据库,保证数据在同一时刻只有一台机器写入数据库. 分布式锁的实现有多种实现方法,除了今 ...
随机推荐
- Spring boot 整合jsp、thymeleaf、freemarker
1.创建spring boot 项目 2.pom文件配置如下: <dependencies> <dependency> <groupId>org.springfra ...
- 一个iOS程序员眼中的跨域问题
摘要: 跨域问题是web开发领域一个常见的问题,相信每个web开发者都遇到"跨域"的问题 最近公司的iOS开发任务比较少,所以自己最近开始了Web开发的任务,在用H5做了很多页面, ...
- 实战小项目之嵌入式linux图像采集与传输
项目简介 本次编程实战主要是围绕嵌入式linux v4l2采集框架展开,包括以下几个部分: v4l2视频采集 IPU转码 framebuffer显示 自定义UDP简单协议进行传输 上位机软件 ...
- PHP排序的几种方法
// 冒泡排序 function BubbleSort($arr) { // 获得数组总长度 $num = count($arr); // 正向遍历数组 for ($i = 1; $i < $n ...
- 习题:过路费(kruskal+并查集+LCA)
过路费 [问题描述]在某个遥远的国家里,有 n 个城市.编号为 1,2,3,…,n.这个国家的政府修 建了 m 条双向道路,每条道路连接着两个城市.政府规定从城市 S 到城市 T 需 要收取的过路费 ...
- POJ 2286 The Rotation Game(IDA*)
The Rotation Game Time Limit: 15000MS Memory Limit: 150000K Total Submissions: 6396 Accepted: 21 ...
- 地理课(geography)
地理课(geography) 题目描述 地理课上,老师给出了一个巨大的地图,由于世界日新月异,会有一些道路在某一时刻被删除,也会有一些道路在某一时刻被修建.这里的道路均为双向的. 老师认为,有一些城市 ...
- xor和路径(codevs 2412)
题目描述 Description 给定一个无向连通图,其节点编号为1到N,其边的权值为非负整数.试求出一条从1号节点到 N 号节点的路径,使得该路径上经过的边的权值的“XOR 和”最大.该路径可以重复 ...
- [LeetCode] Find Minimum in Rotated Sorted Array 二分搜索
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 一个简单的NoSQL内存数据库—Berkeley DB基本操作的例子
一个简单的NoSQL内存数据库—Berkeley DB基本操作的例子 最近,由于云计算的发展,数据库技术也从结构式数据库发展到NoSQL数据库,存储模式从结构化的关系存储到现在如火如荼的key/val ...