一、客户端脚本

1.1、客户端连接

cd /usr/local/services/zookeeper/zookeeper-3.4.13/bin
##连接本地Zookeeper服务器
sh zkCli.sh
##连接主机server1上的zookeeper服务
sh zkCli.sh -server server1:2181

1.2、CRUD操作

创建

##语法:create [-s] [-e] path data acl
create /zk-temp "tmp"

说明:-s / -e分别制定节点特性:顺序或临时节点。默认情况下,创建的是持久节点。

读取

##获取节点子节点列表,语法:ls path [watch]
ls /zk-temp
##获取节点数据,语法:get path [watch]
get /zk-temp

更新

##语法:set path data [version]
set /zk-temp "zk"

删除

##语法:delete path [version]
delete /zk-temp

二、Java客户端API

2.1、创建会话

创建会话Java代码

package zookeeper.javaapi; import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.ZooKeeper; public class CreateSession implements Watcher {
private static CountDownLatch connectedSemaphore = new CountDownLatch(1);

public void process(WatchedEvent event) {
System.out.println("Receive watched event:" + event);
if (Event.KeeperState.SyncConnected == event.getState()) {
connectedSemaphore.countDown();
}
} public static void main(String[] args) throws Exception{
ZooKeeper zooKeeper = new ZooKeeper("server1:2181",1000,new CreateSession());
System.out.println(zooKeeper.getState());
try {
connectedSemaphore.await();
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("Zookeeper session established.");
}

}

![](https://img2018.cnblogs.com/blog/1659331/201907/1659331-20190707134733773-503584414.png)

2.2、创建节点

2.2.1、同步创建节点

方法:String create(final String path, byte data[], List acl, CreateMode createMode)

参数说明:

同步创建节点Java代码

package zookeeper.javaapi; import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.Watcher.Event.KeeperState;

import org.apache.zookeeper.ZooDefs.Ids;

import org.apache.zookeeper.ZooKeeper; /**
  • ZooKeeper API创建节点,使用同步(sync)接口。

    */

    public class Sync_Create_Usage implements Watcher { private static CountDownLatch connectedSemaphore = new CountDownLatch(1); public static void main(String[] args) throws Exception{
     ZooKeeper zookeeper = new ZooKeeper("server1:2181", 5000, new Sync_Create_Usage());
    connectedSemaphore.await(); //创建临时节点
    String path1 = zookeeper.create("/zk-test-ephemeral-", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    System.out.println("Success create znode: " + path1); //临时顺序节点
    String path2 = zookeeper.create("/zk-test-ephemeral-", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
    System.out.println("Success create znode: " + path2); //阻塞线程,查看临时节点创建结果
    Thread.sleep( Integer.MAX_VALUE );

    }

    public void process(WatchedEvent event) {

    if (KeeperState.SyncConnected == event.getState()) {

    connectedSemaphore.countDown();

    }

    }

    }

2.2.2、异步创建节点

方法:void create(final String path, byte data[], List acl, CreateMode createMode, StringCallback cb, Object ctx)

参数说明:

异步创建节点Java代码

package zookeeper.javaapi; import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback;

import org.apache.zookeeper.CreateMode;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.ZooKeeper;

import org.apache.zookeeper.Watcher.Event.KeeperState;

import org.apache.zookeeper.ZooDefs.Ids; /**
  • ZooKeeper API创建节点,使用异步(Async)接口。

    */

    public class Async_Create_Usage implements Watcher {

    private static CountDownLatch connectedSemaphore = new CountDownLatch(1); public static void main(String[] args) throws Exception {

    ZooKeeper zookeeper = new ZooKeeper("server1:2181", 5000, new Async_Create_Usage());

    System.out.println(zookeeper.getState());

    connectedSemaphore.await();
     //创建临时节点
    zookeeper.create("/zk-test-demo-", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL,
    new IStringCallback(), "I am context. ");
    //创建临时节点
    zookeeper.create("/zk-test-demo-", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL,
    new IStringCallback(), "I am context. ");
    //阻塞线程,查看临时节点创建结果
    zookeeper.create("/zk-test-demo-", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL,
    new IStringCallback(), "I am context. ");
    Thread.sleep(Integer.MAX_VALUE);

    }

    public void process(WatchedEvent event) {

    if (KeeperState.SyncConnected == event.getState()) {

    connectedSemaphore.countDown();

    }

    }

    }

class IStringCallback implements AsyncCallback.StringCallback {

public void processResult(int rc, String path, Object ctx, String name) {

System.out.println("Create path result: [" + rc + ", " + path + ", " + ctx + ", real path name: " + name);

}

}

![](https://img2018.cnblogs.com/blog/1659331/201907/1659331-20190707152913115-60968538.png)
![](https://img2018.cnblogs.com/blog/1659331/201907/1659331-20190707152849873-1815609224.png)

2.2.3、回调函数方法说明

方法:void processResult(int rc, String path, Object ctx, String name)

参数说明:

2.3、删除节点

2.3.1、同步删除节点

方法:public void delete(final String path, int version)

参数说明:

同步删除节点Java代码

package zookeeper.javaapi; import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.Watcher.Event.EventType;

import org.apache.zookeeper.Watcher.Event.KeeperState;

import org.apache.zookeeper.ZooDefs.Ids;

import org.apache.zookeeper.ZooKeeper; public class Sync_Delete_Usage implements Watcher {

private static CountDownLatch connectedSemaphore = new CountDownLatch(1);

private static ZooKeeper zookeeper;
public static void main(String[] args) throws Exception {
String path = "/zk-delete-demo";
zookeeper = new ZooKeeper("server1:2181", 5000, new Sync_Delete_Usage());
connectedSemaphore.await(); //同步创建持久节点 /zk-delete-demo
zookeeper.create(path, "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("success create znode: " + path);
//同步创建持久节点 /zk-delete-demo/test
zookeeper.create(path + "/test", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("success create znode: " + path + "/test"); //删除非叶子节点将报错
try {
zookeeper.delete(path, -1);
} catch (Exception e) {
System.out.println("fail to delete znode: " + path);
} //删除叶子节点 /zk-delete-demo/test
zookeeper.delete(path + "/test", -1);
System.out.println("success delete znode: " + path + "/c1");
//删除叶子节点 /zk-delete-demo
zookeeper.delete(path, -1);
System.out.println("success delete znode: " + path); Thread.sleep(Integer.MAX_VALUE);
} public void process(WatchedEvent event) {
if (KeeperState.SyncConnected == event.getState()) {
if (EventType.None == event.getType() && null == event.getPath()) {
connectedSemaphore.countDown();
}
}
}

}

![](https://img2018.cnblogs.com/blog/1659331/201907/1659331-20190707161503791-943167895.png)

2.3.2、异步删除节点

方法:public void delete(final String path, int version, VoidCallback cb, Object ctx)

参数说明:

异步删除节点Java代码

package zookeeper.javaapi; import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback;

import org.apache.zookeeper.CreateMode;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.ZooKeeper;

import org.apache.zookeeper.Watcher.Event.EventType;

import org.apache.zookeeper.Watcher.Event.KeeperState;

import org.apache.zookeeper.ZooDefs.Ids; public class Async_Delete_Usage implements Watcher {

private static CountDownLatch connectedSemaphore = new CountDownLatch(1);

private static ZooKeeper zookeeper;
public static void main(String[] args) throws Exception {
String path = "/zk-delete-demo";
zookeeper = new ZooKeeper("server1:2181", 5000, new Async_Delete_Usage());
connectedSemaphore.await(); //同步创建持久节点 /zk-delete-demo
zookeeper.create(path, "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("success create znode: " + path); //同步创建持久节点 /zk-delete-demo/test
zookeeper.create(path + "/test", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("success create znode: " + path + "/test"); //删除非叶子节点将报错
zookeeper.delete(path, -1, new IVoidCallback(), null);
//删除叶子节点 /zk-delete-demo/test
zookeeper.delete(path + "/test", -1, new IVoidCallback(), null);
//删除叶子节点 /zk-delete-demo
zookeeper.delete(path, -1, new IVoidCallback(), null); Thread.sleep(Integer.MAX_VALUE);
} public void process(WatchedEvent event) {
if (KeeperState.SyncConnected == event.getState()) {
if (EventType.None == event.getType() && null == event.getPath()) {
connectedSemaphore.countDown();
}
}
}

}

class IVoidCallback implements AsyncCallback.VoidCallback {

public void processResult(int rc, String path, Object ctx) {

System.out.println(rc + ", " + path + ", " + ctx);

}

}

![](https://img2018.cnblogs.com/blog/1659331/201907/1659331-20190707165818262-1510544827.png)

2.4、获取节点

2.4.1、同步获取节点列表

方法:

  List getChildren(final String path, Watch watcher)

  List getChildren(String path, boolean watch)

  List getChildren(final String path, Watch watcher, Stat stat)

  List getChildren(String path, boolean watch, Stat stat)

参数说明:



同步获取节点列表Java代码

package zookeeper.javaapi; import java.util.List;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.ZooKeeper;

import org.apache.zookeeper.Watcher.Event.EventType;

import org.apache.zookeeper.Watcher.Event.KeeperState;

import org.apache.zookeeper.ZooDefs.Ids; public class Sync_Update_Usage implements Watcher {

private static CountDownLatch connectedSemaphore = new CountDownLatch(1);

private static ZooKeeper zookeeper = null;
public static void main(String[] args) throws Exception {
String path = "/zk-update-demo";
zookeeper = new ZooKeeper("server1:2181", 5000, new Sync_Update_Usage());
connectedSemaphore.await(); //同步创建持久节点 /zk-delete-demo
zookeeper.create(path, "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("success create znode: " + path); //同步创建持久节点 /zk-delete-demo/test_1
zookeeper.create(path + "/test_1", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
System.out.println("success create znode: " + path + "/test_1"); //同步获取节点,同时使用默认Watcher
List<String> childrenList = zookeeper.getChildren(path, true);
System.out.println(childrenList); //同步创建持久节点 /zk-delete-demo/test_2
zookeeper.create(path + "/test_2", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
System.out.println("success create znode: " + path + "/test_2"); Thread.sleep(3000); //同步创建持久节点 /zk-delete-demo/test_3
zookeeper.create(path + "/test_3", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
System.out.println("success create znode: " + path + "/test_3");
Thread.sleep(Integer.MAX_VALUE);
} public void process(WatchedEvent event) {
if (KeeperState.SyncConnected == event.getState()) {
if (EventType.None == event.getType() && null == event.getPath()) {
connectedSemaphore.countDown();
} else if (event.getType() == EventType.NodeChildrenChanged) {
try {
//Watcher通知是一次性的,一旦触发一次通知后,该Watcher就失效了
//因此客户端需要反复注册Watcher,即程序中在process里面再次注册了Watcher,否则将无法获取之后该节点的变动通知
System.out.println("ReGet Child:" + zookeeper.getChildren(event.getPath(), true));
} catch (Exception e) {
}
}
}
}

}

![](https://img2018.cnblogs.com/blog/1659331/201907/1659331-20190707171557000-1963604897.png)

2.4.2、异步获取节点列表

方法:

  void getChildren(final String path, Watch watcher, ChildrenCallback cb, Object ctx)

  void getChildren(String path, boolean watch, ChildrenCallback cb, Object ctx)

  void getChildren(final String path, Watch watcher, ChildrenCallback cb, Object ctx, Stat stat)

  void getChildren(String path, boolean watch, ChildrenCallback cb, Object ctx, Stat stat)

参数说明:

异步获取节点列表Java代码

package zookeeper.javaapi; import java.util.List;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback;

import org.apache.zookeeper.CreateMode;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.Watcher.Event.EventType;

import org.apache.zookeeper.Watcher.Event.KeeperState;

import org.apache.zookeeper.ZooDefs.Ids;

import org.apache.zookeeper.ZooKeeper;

import org.apache.zookeeper.data.Stat; public class Async_Update_Usage implements Watcher {

private static CountDownLatch connectedSemaphore = new CountDownLatch(1);

private static ZooKeeper zookeeper = null;
public static void main(String[] args) throws Exception {
String path = "/zk-get-demo";
zookeeper = new ZooKeeper("server1:2181", 5000, new Async_Update_Usage());
connectedSemaphore.await(); zookeeper.create(path, "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("success create znode: " + path); zookeeper.create(path + "/test_1", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
System.out.println("success create znode: " + path + "/test_1"); //异步获取节点,使用默认Watcher
zookeeper.getChildren(path, true, new IChildren2Callback(), null); zookeeper.create(path + "/test_2", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
System.out.println("success create znode: " + path + "/test_2"); Thread.sleep(Integer.MAX_VALUE);
} public void process(WatchedEvent event) {
if (KeeperState.SyncConnected == event.getState()) {
if (EventType.None == event.getType() && null == event.getPath()) {
connectedSemaphore.countDown();
} else if (event.getType() == EventType.NodeChildrenChanged) {
try {
System.out.println("ReGet Child:" + zookeeper.getChildren(event.getPath(), true));
} catch (Exception e) {
}
}
}
}

}

class IChildren2Callback implements AsyncCallback.Children2Callback {

public void processResult(int rc, String path, Object ctx, List children, Stat stat) {

System.out.println("Get Children znode result: [response code: " + rc + ", param path: " + path + ", ctx: "

+ ctx + ", children list: " + children + ", stat: " + stat);

}

}

![](https://img2018.cnblogs.com/blog/1659331/201907/1659331-20190707174315216-1862803024.png)

说明:stat对象中记录了一个节点的基本属性信息,例如节点创建时的事务ID、最后一次修改的事务ID和节点数据内容的长度等。有时候,我们不仅需要获取节点最新的子节点列表,还要获取这个节点最新的节点状态信息。对于这种情况,我们可以将一个旧的stat变量传入API接口,该stat变量会在方法执行过程中,被来自服务器响应的新stat对象替换;

妈呀!写着好累,剩下的介绍下方法,代码就不贴了,大同小异。

2.4.3、同步获取节点数据

同步方法:

  byte[] getData(final String path, Watcher watcher, Stat stat);

  byte[] getData(String path, boolean watch, Stat stat);

异步方法:

  void getData(String path, boolean watch, DataCallback cb, Object ctx);

  void getData(String path, boolean watch, DataCallback cb, Object ctx);

参数说明:

说明:客户端在获取一个节点的数据内容时,可进行watcher注册,一旦该节点的状态发生变更,那么zookeeper服务端就会向客户端发送一NodeDataChanged(EventType.NodeDataChanged)的事件通知。注意,节点数据内容或者节点版本的变化都被看做是zookeeper节点的变化,都会触发NodeDataChanged通知。

2.5、更新节点

同步方法:

  Stat setData(final String path, byte data[], int version);

异步方法:

  void setData(final String path, byte data[], int version, StatCallback cb, Object ctx);

参数说明:

说明:在调用更新操作的时候,就可以添加version这个参数,该参数可以对应于CAS原理中的“预期值”,表明是针对该数据版本进行更新的;在zookeeper中,数据版本都是从0开始计数的,所以严格的讲,“-1”并不是一个合法的数据版本,它仅仅是一个标识符,如果客户端传入的版本参数是“-1”,就是告诉zookeeper服务器,客户端需要基于数据的最新版本进行更新操作。如果对zookeeper数据节点的更新操作没有原子性要求,那么就可以使用“-1”。

2.6、检测节点是否存在

同步方法:

  Stat exists(final String path, Watcher watcher);

  Stat exists(String path, boolean watch);

异步方法:

  void exists(final String path, Watcher watcher, StatCallback cb, Object ctx);

  void exists(String path, boolean watch, StatCallback cb, Object ctx);

参数说明:

说明:无论指定节点是否存在,通过调用exists()接口都可以注册watcher;exists()接口中注册的watcher,能够对节点创建、节点删除和节点数据更新事件进行监听;对于指定节点的子节点的各种变化,都不会通知客户端。

2.7、权限控制

方法:

void addAuthInfo(String scheme, byte auth[]);

参数说明:

说明:对于节点的权限,当一个客户端为一个节点添加权限信息的时候,该权限信息是添加到了该节点的叶子节点上,操作这些授权节点需要权限信息;但如果操作该父节点,是不需要权限的。

参考资料

参考书籍:从Paxos到Zookeeper:分布式一致性原理与实践

  • 作者:
    DeepInThought


    出处:
    https://www.cnblogs.com/DeepInThought


    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 【6】Zookeeper脚本及API的更多相关文章

    1. Linux -- 基于zookeeper的java api(二)

      Linux -- 基于zookeeper的java api(二) 写一个关于基于集群的zookeeper的自定义实现HA 基于客户端和监控器:使用监控的方法查看每个注册过的节点的状态来做出操作. Wa ...

    2. Linux -- 基于zookeeper的java api(一)

      Linux -- 基于zookeeper的java api 首先启动你所有的 zkService.sh 查看状态:检查是否启动正确 [root@hu-hadoop2 ~]# zkServer.sh s ...

    3. ZooKeeper客户端原生API的使用以及ZkClient第三方API的使用

      这两部分内容的介绍主要讲的是节点及节点内容和子节点的操作,并且讲解的节点的事件监听以及ACL授权 ZooKeeper客户端原生API的使用 百度网盘地址: http://pan.baidu.com/s ...

    4. zookeeper的java api操作

      zookeeper的java api操作 创建会话: Zookeeper(String connectString,int sessionTimeout,Watcher watcher) Zookee ...

    5. 【分布式】Zookeeper使用--Java API

      一.前言 上一篇博客我们通过命令行来操作Zookeper的客户端和服务端并进行相应的操作,这篇主要介绍如何通过API(JAVA)来操作Zookeeper. 二.开发环境配置 首先打开Zookeeper ...

    6. (原) 1.3 zookeeper脚本使用

      本文为原创文章,转载请注明出处,谢谢 zookeeper自带脚本使用 1.进入zookeeper客户端 找到安装目录下的bin目录,执行以下脚本(中括号标示被必填) ./zkCli.sh  -time ...

    7. Zookeeper Java客户端API的使用

      1. 原生api         具体查看下面github代码 2. ZkClient ZkClient是Github上一个开源的ZooKeeper客户端.ZkClient在ZooKeeper原生 A ...

    8. JDK1.7新特性(3):java语言动态性之脚本语言API

      简要描述:其实在jdk1.6中就引入了支持脚本语言的API.这使得java能够很轻松的调用其他脚本语言.具体API的使用参考下面的代码: package com.rampage.jdk7.chapte ...

    9. Zookeeper(二)Zookeeper原理与API应用

      一 Zookeeper概述 1.1 概述 Zookeeper是Google的Chubby一个开源的实现.它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务. 分布式同步.组服 ...

    随机推荐

    1. BigDecimal数据的加 减 乘 除 N次幂运算 以及比较大小

      在实际开开发过程中BigDecimal是一个经常用到的类: 它可以进行大数值的精确却运算,下面介绍一下它的加-减-乘-除以及N次幂的操作操作 import java.math.BigDecimal; ...

    2. vim 全局替换

      :%s/foo/bar/g 把全部foo替换为bar,全局替换

    3. centos安装tidy扩展

      wget http://pecl.php.net/get/tidy-1.2.tgztar -xvzf tidy-1.2.tgzcd tidy-1.2/usr/local/php/bin/phpize. ...

    4. 【ARTS】01_23_左耳听风-201900415~2019004021

      ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

    5. 【手记】解决Intel Management Engine Interface黄色感叹号

      安装补丁KB2685811.重启.

    6. 初学django框架 (urls,include子路由,render模板渲染)(一)

      一.urls url的使用为了告诉django哪个url调用那一段代码 如上左图所示,后面的test,test1如下图所示,为视图函数,通过前面的路径,调用后面函数的代码: 只有输入正确的url才会在 ...

    7. 基于LSTM + keras 的诗歌生成器

          最近在github 上发现了一个好玩的项目,一个基于LSTM + keras 实现的诗歌生成器,地址是:https://github.com/youyuge34/Poems_generator ...

    8. centOS 安装 pm2

      安装 npm install -g pm2 自启 pm2 startup centos 启动 pm2 start server/add.js (以实际路径为准) 其他命令: pm2 list #查看进 ...

    9. Vue CLI 3 如何自定义 js 的文件名

      参考链接:https://blog.csdn.net/weixin_33979363/article/details/88742342

    10. python xlrd模块

      一.什么是xlrd模块? Python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库. 二.使用介绍 1.常用单元格中的数据类型 类型 含义 e ...