第4章: zookeeper 基础API使用

来源:互联网 发布:steam中文版 mac 编辑:程序博客网 时间:2024/06/01 07:23

在前面几个章节已经阐述了zookeeper的一些安装和基本命令使用,本章节我们来介绍一下其提供的几个基础API的使用方式,先会介绍一下这些API的功能,再使用一个示例来具体说明展示其使用过程。

1、常用接口说明

客户端要连接 Zookeeper服务器可以通过创建 org.apache.zookeeper. ZooKeeper的一个实例对象,然后调用这个类提供的接口来和服务器交互。

前面说了 ZooKeeper主要是用来维护和监控一个目录节点树中存储的数据的状态,所有我们能够操作 ZooKeeper的也和操作目录节点树大体一样,如创建一个目录节点,给某个目录节点设置数据,获取某个目录节点的所有子目录节点,给某个目录节点设置权限和监控这个目录节点的状态变化。

这些接口如下表所示:
org.apache.zookeeper. ZooKeeper
方法列表,方法名方法功能描述

String create(String path, byte[] data, List<ACL> acl,CreateMode createMode)

创建一个给定的目录节点 path, 并给它设置数据,CreateMode 标识有四种形式的目录节点,分别是 PERSISTENT:持久化目录节点,这个目录节点存储的数据不会丢失;PERSISTENT_SEQUENTIAL:顺序自动编号的目录节点,这种目录节点会根据当前已近存在的节点数自动加 1,然后返回给客户端已经成功创建的目录节点名;EPHEMERAL:临时目录节点,一旦创建这个节点的客户端与服务器端口也就是 session 超时,这种节点会被自动删除;EPHEMERAL_SEQUENTIAL:临时自动编号节点

Stat exists(String path, boolean watch)

判断某个 path 是否存在,并设置是否监控这个目录节点,这里的 watcher 是在创建 ZooKeeper 实例时指定的 watcher,exists方法还有一个重载方法,可以指定特定的watcher

Stat exists(String path,Watcher watcher)

重载方法,这里给某个目录节点设置特定的 watcher,Watcher 在 ZooKeeper 是一个核心功能,Watcher 可以监控目录节点的数据变化以及子目录的变化,一旦这些状态发生变化,服务器就会通知所有设置在这个目录节点上的 Watcher,从而每个客户端都很快知道它所关注的目录节点的状态发生变化,而做出相应的反应

void delete(String path, int version)

删除 path 对应的目录节点,version 为 -1 可以匹配任何版本,也就删除了这个目录节点所有数据

List<String>getChildren(String path, boolean watch)

获取指定 path 下的所有子目录节点,同样 getChildren方法也有一个重载方法可以设置特定的 watcher 监控子节点的状态

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

给 path 设置数据,可以指定这个数据的版本号,如果 version 为 -1 怎可以匹配任何版本

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

获取这个 path 对应的目录节点存储的数据,数据的版本等信息可以通过 stat 来指定,同时还可以设置是否监控这个目录节点数据的状态

voidaddAuthInfo(String scheme, byte[] auth)

客户端将自己的授权信息提交给服务器,服务器将根据这个授权信息验证客户端的访问权限。

Stat setACL(String path,List<ACL> acl, int version)

给某个目录节点重新设置访问权限,需要注意的是 Zookeeper 中的目录节点权限不具有传递性,父目录节点的权限不能传递给子目录节点。目录节点 ACL 由两部分组成:perms 和 id。
Perms 有 ALL、READ、WRITE、CREATE、DELETE、ADMIN 几种 
而 id 标识了访问目录节点的身份列表,默认情况下有以下两种:
ANYONE_ID_UNSAFE = new Id("world", "anyone") 和 AUTH_IDS = new Id("auth", "") 分别表示任何人都可以访问和创建者拥有访问权限。

List<ACL>getACL(String path,Stat stat)

获取某个目录节点的访问权限列表

除了以上这些上表中列出的方法之外还有一些重载方法,如都提供了一个回调类的重载方法以及可以设置特定 Watcher的重载方法,具体的方法可以参考 org.apache.zookeeper. ZooKeeper类的 API说明。

2、常用接口使用示例

我们demo提供一个以上接口的简单封装,来看下一个zookeeper的接口使用java操作的一个简单实现,示例为BaseExcutor,代码如下:

/** *  */package org.zookeeper.example;import java.io.IOException;import java.util.List;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.ZooKeeper;/** * @author huige * @date 2016-10-9 * @time 下午3:55:02 */public class BaseExcutor implements Watcher {private ZooKeeper zk;public BaseExcutor(String host, String port) throws IOException {String connectString = host + ":" + port;zk = new ZooKeeper(connectString, 3000, this);}@Overridepublic void process(WatchedEvent event) {String path = event.getPath();System.out.println("path " + path + " has event!");switch (event.getType()) {case NodeChildrenChanged:System.out.println("path " + path + " NodeChildrenChanged!");break;case NodeCreated:System.out.println("path " + path + " NodeCreated!");break;case NodeDeleted:System.out.println("path " + path + " NodeDeleted!");break;case None:if (event.getState() == Event.KeeperState.SyncConnected) {System.out.println("SyncConnected!");} else if (event.getState() == Event.KeeperState.Expired) {System.out.println("Expired!");}break;}}public void create(String node, String data, CreateMode createMode)throws KeeperException, InterruptedException {zk.create(node, data.getBytes(), Ids.OPEN_ACL_UNSAFE, createMode);}public List<String> list(String node) throws KeeperException,InterruptedException {return zk.getChildren(node, true);}public void set(String node, String value) throws KeeperException,InterruptedException {zk.setData(node, value.getBytes(), -1);}public String get(String node) throws KeeperException, InterruptedException {return new String(zk.getData(node, true, null));}public void delete(String node) throws InterruptedException,KeeperException {zk.delete(node, -1);}public void exsit(String node) throws KeeperException, InterruptedException {zk.exists(node, true);}public void close() throws InterruptedException{zk.close();}public static void main(String[] args) throws IOException, KeeperException, InterruptedException {BaseExcutor baseExcutor = new BaseExcutor("172.16.10.58", "2181");baseExcutor.create("/hg", "number1", CreateMode.PERSISTENT);baseExcutor.create("/hg/example1", "number2", CreateMode.PERSISTENT);List<String> childNodes = baseExcutor.list("/hg");System.out.println("/hg childNodes are "+childNodes);String data1 = baseExcutor.get("/hg/example1");System.out.println("/hg/example1 data "+data1);baseExcutor.set("/hg/example1", "number3");String data2 = baseExcutor.get("/hg/example1");System.out.println("after set /hg/example1 data "+data2);baseExcutor.delete("/hg/example1");List<String> childNodesDelte = baseExcutor.list("/hg");System.out.println("after delete node /hg childNodes are "+childNodesDelte);baseExcutor.delete("/hg");baseExcutor.close();}}

示例系统输出如下:

path null has event!SyncConnected!/hg childNodes are [example1]/hg/example1 data number2path /hg/example1 has event!after set /hg/example1 data number3path /hg/example1 has event!path /hg/example1 NodeDeleted!path /hg has event!path /hg NodeChildrenChanged!after delete node /hg childNodes are []path /hg has event!path /hg NodeDeleted!

到此,我们基于zookeeper的基础篇介绍就结束了,关于基础使用大家有什么问题或者想了解关于zookeeper哪些方面的使用可以直接给我留言。接下来就进入应用篇,我们会通过几个应用示例来展示下zookeeper的几个典型应用,欢迎大家到时关注。




0 0
原创粉丝点击