安装好 ZooKeeper 之后,可以使用telnet来测试是否运行。
telnet 127.0.0.1 2100Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.statZookeeper version: 3.4.6-1569965, built on 02/20/2014 09:09 GMTClients: /127.0.0.1:34656[0](queued=0,recved=1,sent=0) /127.0.0.1:34531[1](queued=0,recved=57,sent=57)Latency min/avg/max: 0/2/73Received: 59Sent: 58Connections: 2Outstanding: 0Zxid: 0x3Mode: standaloneNode count: 5
这里使用的是单机 standalone 模式。
也可以使用客户端脚本来连接服务器
bash zkCli.sh -server 127.0.0.1:2100
连接上之后就可以像使用 NOSQL(memcached,redis) 数据库一样使用 ZooKeeper。
set-get 值
首先使用 create 命令,创建一个 ZooKeeper 节点
create /titus 18189597900
ls /
命令可以查看有哪些节点
[zk: 127.0.0.1:2100(CONNECTED) 3] ls / [zk-book, zookeeper, titus]
get path
命令可以查看指定节点的数据
[zk: 127.0.0.1:2100(CONNECTED) 4] get /titus18189597900cZxid = 0x6ctime = Mon Oct 12 21:44:14 CST 2015mZxid = 0x6mtime = Mon Oct 12 21:44:14 CST 2015pZxid = 0x6cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 11numChildren = 0
第一行就是titus节点的数据18189597900。
set path data
命令可以更改指定节点的数据
[zk: 127.0.0.1:2100(CONNECTED) 5] set /titus 1571111cZxid = 0x6ctime = Mon Oct 12 21:44:14 CST 2015mZxid = 0x7mtime = Mon Oct 12 21:47:06 CST 2015pZxid = 0x6cversion = 0dataVersion = 1aclVersion = 0ephemeralOwner = 0x0dataLength = 7numChildren = 0
版本 dataVersion 由0变为了1。