Zookeeper学习笔记——2 Shell和Java API的使用

ZooKeeper的使用一般都接触不到,因为平时工作甚少直接使用ZK。但是通过手动操作一下ZK,还是能对其中的门道了解各一二。

shell 常用命令

help 查看所有支持的命令

[zk: localhost:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
	stat path [watch]
	set path data [version]
	ls path [watch]
	delquota [-n|-b] path
	ls2 path [watch]
	setAcl path acl
	setquota -n|-b val path
	history 
	redo cmdno
	printwatches on|off
	delete path [version]
	sync path
	listquota path
	rmr path
	get path [watch]
	create [-s] [-e] path data acl
	addauth scheme auth
	quit 
	getAcl path
	close 
	connect host:port

ls 查看路径下的所有节点

[zk: localhost:2181(CONNECTED) 1] ls /
[zk-book2, zk-book1, zk-book, zookeeper]

create 创建节点

创建临时节点:

[zk: localhost:2181(CONNECTED) 3] create /xingoo 123
Created /xingoo
[zk: localhost:2181(CONNECTED) 4] create -e /xingoo-e 123
Created /xingoo-e
[zk: localhost:2181(CONNECTED) 6] ls /
[zk-book2, zk-book1, zk-book, xingoo, zookeeper, xingoo-e]
[zk: localhost:2181(CONNECTED) 8] quit
Quitting...
[zk: localhost:2181(CONNECTED) 0] ls /
[zk-book2, zk-book1, zk-book, xingoo, zookeeper]

创建顺序节点:

[zk: localhost:2181(CONNECTED) 2] create -s /xingoo-s 1
Created /xingoo-s0000000018
[zk: localhost:2181(CONNECTED) 3] create -s /xingoo-s 1
Created /xingoo-s0000000019
[zk: localhost:2181(CONNECTED) 4] create -s /xingoo-s 1
Created /xingoo-s0000000020
[zk: localhost:2181(CONNECTED) 5] create -s /xingoo-s 1
Created /xingoo-s0000000021
[zk: localhost:2181(CONNECTED) 7] ls /
[zk-book2, zk-book1, zk-book, xingoo-s0000000018, zookeeper, xingoo, xingoo-s0000000020, xingoo-s0000000021, xingoo-s0000000019]

delete 删除节点

[zk: localhost:2181(CONNECTED) 10] ls /
[zk-book2, zk-book1, zk-book, xingoo-s0000000018, zookeeper, xingoo, xingoo-s0000000020, xingoo-s0000000021, xingoo-s0000000019]
[zk: localhost:2181(CONNECTED) 11] delete /xingoo-s*
Node does not exist: /xingoo-s*
[zk: localhost:2181(CONNECTED) 12] delete /xingoo-s0000000020
[zk: localhost:2181(CONNECTED) 13] ls /
[zk-book2, zk-book1, zk-book, xingoo-s0000000018, zookeeper, xingoo, xingoo-s0000000021, xingoo-s0000000019]
[zk: localhost:2181(CONNECTED) 14] 

get 查询节点

[zk: localhost:2181(CONNECTED) 14] get /xingoo
123
cZxid = 0x5d
ctime = Mon Apr 09 17:21:50 CST 2018
mZxid = 0x5d
mtime = Mon Apr 09 17:21:50 CST 2018
pZxid = 0x5d
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0

Java API使用

完整的代码,可以参考《从PAXOS到ZOOKEEPER》,或者我的代码样例:
https://github.com/xinghalo/java-in-action/tree/master/src/cn/xingoo/book/zook/chap5

连接ZooKeeper

public class ZooKeeper_Contructor_Usage implements Watcher {

    /**
     * 调用await()阻塞,当减到0时,恢复
     * http://www.importnew.com/15731.html
     */
    private static CountDownLatch connectedSemaphore = new CountDownLatch(1);

    public static void main(String[] args) throws IOException {
        ZooKeeper zookeeper = new ZooKeeper("127.0.0.1:2181",5000,new ZooKeeper_Contructor_Usage());
        System.out.println(zookeeper.getState());
        try{
            connectedSemaphore.await();
        }catch (Exception e){
            System.out.println("zk session established");
        }
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        System.out.println("recieve watched event:"+watchedEvent);
        if(Event.KeeperState.SyncConnected == watchedEvent.getState()){
            connectedSemaphore.countDown();
        }
    }
}

基于CountDownLatch做线程阻塞,ZooKeeper的构造方法中有几个重要的参数:

  • url,是连接地址,如果是多个地址拼接,可以做轮训;url后面还可以跟root目录
  • timeout,连接超时时间;如果连接断开,ZooKeeper会自动重连
  • watcher,默认的监控类

create 创建代码

String path1 = zooKeeper.create("/zk-test-ephemeral-",
                "".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL);

exist 判断是否存在

zooKeeper.exists(path, true);

getData 查询数据

zooKeeper.getData(watchedEvent.getPath(),true,stat)

setData 更新数据

这里的版本号用作CAS,后面会详细介绍

zooKeeper.setData(path, "123".getBytes(), -1);

getChildren 查询所有子节点

zooKeeper.getChildren(watchedEvent.getPath(),true)

注意,ZooKeeper客户端里面所有的watcher都是一次性的,如果想要监控每次事件,需要在watcher里面再次注册。

posted @ 2018-04-09 20:20  xingoo  阅读(1065)  评论(0编辑  收藏  举报