聊聊、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内的方法需要获取一个唯一锁,比如获取锁之后要把数据写入数据库,保证数据在同一时刻只有一台机器写入数据库. 分布式锁的实现有多种实现方法,除了今 ...
随机推荐
- 我对于js注入的理解
资料:http://blog.csdn.net/gisredevelopment/article/details/41778671 js注入就是在前端利用使用js的地方 在这其中注入你写的js代码 使 ...
- gcc学习记录
-Wall: 使输出中包含警告信息,提示一些可以避免的错误.如果没有错误,则不会输出信息. -o:后面加上可执行文件的名字.如果不加-o选项,会默认生成a.out可执行文件.举例:gcc -Wall ...
- [oldboy-django][4python面试]有关csrf跨站伪造请求攻击
1 csrf定义 - csrf定义:Cross Site Request Forgery,跨站请求伪造 举例来说: 网站A伪造了一个图片链接: <a href="http://www. ...
- 爬虫:Scrapy1
Python 2.7 npm install scrapy 步骤: 创建一个 Scrapy 项目 定义提取的 Item 编写爬取网站的 Spider 并提取 Item 编写 Item Pipeline ...
- 一小时学会用Python Socket 开发可并发的FTP服务器!!
socket是什么 什么是socket所谓socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络发出请求 ...
- BZOJ-1043 [HAOI2008]下落的圆盘
几何题... 先把所有圆储存起来,然后对于每个圆我们求得之后放下的圆挡住了的部分,求个并集,并把没被挡到的周长加进答案. #include <cstdlib> #include <c ...
- [AHOI2014&&JSOI2014][bzoj3876] 支线剧情 [上下界费用流]
题面 传送门 思路 转化模型:给一张有向无环图,每次你可以选择一条路径走,花费的时间为路径上边权的总和,问要使所有边都被走至少一遍(可以重复),至少需要花费多久 走至少一遍,等价于覆盖这条边 也就是说 ...
- Teleportation(tel)
Teleportation(tel) 题目描述 Zy大帝拥有n个星球,因为距离非常遥远,所以Zy在他所居住的1号星球和他的军事基地霸中所在的2号星球建造了两个传送门,这样从1号星球到2号星球就只需要2 ...
- JavaScript (JS)基础:BOM 浅析 (含window对象相关基本方法、属性解析)
① window对象(Math方法也属于window对象): window对象是JavaScript中的顶级对象,所有定义在全局作用域中的变量.函数都会变成window对象的属性和方法,window对 ...
- linux之参数实用讲解
<1>linux文件参数 在Windows下是使用 %1 %2 %3 而在Linux下是使用 $1 $2 $3 ------------------- 如: 1.某bat文件 cd ...