今天我们来说说 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的更多相关文章

  1. ZooKeeper系列4:ZooKeeper API简介及编程

    问题导读: 1.ZooKeeper API 共包含几个包? 2.如何使用ZooKeeper API 创建zookeeper应用程序? 1)ZooKeeper API 简介   ZooKeeper AP ...

  2. 面向服务的体系架构 SOA(三) --- Zookeeper API、zkClient API的使用

    zookeeper简单介绍及API使用 1.1 zookeeper简介 zookeeper是一个针对大型分布式系统的可靠的协调系统,提供的功能包括配置维护.名字服务.分布式同步.组服务等.zookee ...

  3. Hbase记录-ZooKeeper API

    Zookeeper API ZooKeeper有一个Java和C绑定的官方API.ZooKeeper社区提供了对于大多数语言(.NET,Python等)的非官方API.使用ZooKeeper的API, ...

  4. Zookeeper 系列(三)Zookeeper API

    Zookeeper 系列(三)Zookeeper API 本节首先介绍 Zookeeper 的 Shell 命令,再对 Java 操作 Zookeeper 的三种方式进行讲解,本节先介绍 Zookee ...

  5. Zookeeper api增删改查节点

    Exists - 检查Znode的存在 ZooKeeper类提供了 exists 方法来检查znode的存在.如果指定的znode存在,则返回一个znode的元数据.exists方法的签名如下: ex ...

  6. Zookeeper Api(java)入门与应用(转)

    如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...

  7. zookeeper[3] zookeeper API开发注意事项总结

    如下是根据官方接口文档(http://zookeeper.apache.org/doc/r3.4.1/api/org/apache/zookeeper/ZooKeeper.html#register( ...

  8. Zookeeper Api

    如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...

  9. 聊聊zookeeper的分布式锁

    分布式锁就是多台机器,分布在不同的JVM中,这些不同JVM内的方法需要获取一个唯一锁,比如获取锁之后要把数据写入数据库,保证数据在同一时刻只有一台机器写入数据库. 分布式锁的实现有多种实现方法,除了今 ...

随机推荐

  1. 平衡树 - Luogu 1486 郁闷的出纳员

    这么久没写平衡树了,再来一发... P1486 郁闷的出纳员 题目描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的 ...

  2. 【Single Num II】cpp

    题目: Given an array of integers, every element appears three times except for one. Find that single o ...

  3. 【Candy】cpp

    题目: There are N children standing in a line. Each child is assigned a rating value. You are giving c ...

  4. python 学习分享-函数篇

    函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也可以自己创建函数,这 ...

  5. Selenium - WebDriver Advanced Usage

    Explicit Waits # Python from selenium import webdriver from selenium.webdriver.common.by import By f ...

  6. pc端自适应方案

    一.常见处理方式 定宽 电商类.内容为主的网站几乎采用这种方式 1.网易考拉.京东(1190px) 2.知乎(1000px),果壳(1000px),网易新闻(1200px) 媒体查询+定宽 图片类.简 ...

  7. spring 解决中文乱码问题

    spring 解决中文乱码问题 使用spring的前提下在web.xml中配置 <filter> <filter-name>encodingFilter</filter- ...

  8. Codeforces Round #364 (Div. 2) A 水

    A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  9. RSA DSA

    RSA https://blog.csdn.net/sunmenggmail/article/details/11994013 https://baike.baidu.com/item/RSA%E7% ...

  10. photoshop 安装

    Photoshop 下载: http://www.duote.com/soft/54352.html 下载完后解压选择..\Adobe CS6\Set-up.exe ,点击  Set-up.exe   ...