一,查看当前zookeeper的版本:
[root@localhost conf]# echo stat|nc 127.0.0.1 2181 Zookeeper version: 3.5.6-c11b7e26bc554b8523dc929761dd28808913f091, built on 10/08/2019 20:18 GMT
说明:架构森林是一个专注架构的博客,对应的源码可以访问这里获取
说明:作者邮箱: 371125307@qq.com
二,启动zookeeper客户端
[root@localhost conf]# zkCli.sh
三,使用 ls 命令来查看当前 ZooKeeper 中所包含的内容
[zk: localhost:2181(CONNECTED) 0] ls / [zookeeper]
四,创建一个新的 znode
[zk: localhost:2181(CONNECTED) 1] create /lockdemo 'demo content' Created /lockdemo [zk: localhost:2181(CONNECTED) 2] ls / [lockdemo, zookeeper]
五,获取一个znode的value
[zk: localhost:2181(CONNECTED) 3] get /lockdemo demo content
六,设置一个znode的 value
[zk: localhost:2181(CONNECTED) 22] set /lockdemo 'demo content2'
六,删除一个znode
说明:使用delete命令
[zk: localhost:2181(CONNECTED) 4] create /lockdemo2 Created /lockdemo2 [zk: localhost:2181(CONNECTED) 5] ls / [lockdemo, lockdemo2, zookeeper] [zk: localhost:2181(CONNECTED) 6] delete /lockdemo2 [zk: localhost:2181(CONNECTED) 7] ls / [lockdemo, zookeeper]
七,列出zkCli上执行过的命令
[zk: localhost:2181(CONNECTED) 8] history
八,创建一个子节点
[zk: localhost:2181(CONNECTED) 10] create /lockdemo/son Created /lockdemo/son [zk: localhost:2181(CONNECTED) 11] ls / [lockdemo, zookeeper]
九,列出节点时,附带上统计信息
给ls 加上 s参数即可
[zk: localhost:2181(CONNECTED) 12] ls -s /lockdemo [son]cZxid = 0x2 ctime = Tue Feb 18 21:44:25 CST 2020 mZxid = 0x2 mtime = Tue Feb 18 21:44:25 CST 2020 pZxid = 0x5 cversion = 1 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 12 numChildren = 1
十,统计节点的信息
[zk: localhost:2181(CONNECTED) 14] stat /lockdemo/son cZxid = 0x5 ctime = Tue Feb 18 21:51:09 CST 2020 mZxid = 0x5 mtime = Tue Feb 18 21:51:09 CST 2020 pZxid = 0x5 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 0
说明:各个字段的含义是什么
cZxid:节点创建时的zxid ctime:节点创建时间 mZxid:节点最近一次更新时的zxid mtime:节点最近一次更新的时间 cversion:子节点数据更新次数 dataVersion:本节点数据更新次数 aclVersion:节点ACL(授权信息)的更新次数 ephemeralOwner:如果该节点为临时节点,ephemeralOwner值表示与该节点绑定的session id. 如果该节点不是临时节点,ephemeralOwner值为0 dataLength:节点数据长度,本例中为根节点/的长度 numChildren:子节点个数
十一,删除带子节点的节点
节点如果带有子节点时,不能直接使用delete做删除
[zk: localhost:2181(CONNECTED) 15] delete /lockdemo Node not empty: /lockdemo [zk: localhost:2181(CONNECTED) 16] deleteall /lockdemo [zk: localhost:2181(CONNECTED) 17] ls / [zookeeper]
十二,watch一个节点的value变化
在终端一上操作:
[zk: localhost:2181(CONNECTED) 27] set /lockdemo 'demo content2' WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/lockdemo [zk: localhost:2181(CONNECTED) 28] get -w /lockdemo demo content2
说明: get -w 给当前的key添加了watcher
在终端二上操作
[zk: localhost:2181(CONNECTED) 4] set /lockdemo 'change2'
此时回到终端一查看
[zk: localhost:2181(CONNECTED) 29] WATCHER:: WatchedEvent state:SyncConnected type:NodeDataChanged path:/lockdemo
watcher提示: 有NodeDataChanged发生
十三,watch一个节点的node变化
说明:这种主要是子节点的变化,不包括value的变化
在终端一操作:
[zk: localhost:2181(CONNECTED) 32] ls -w /lockdemo []
在终端二操作:
[zk: localhost:2181(CONNECTED) 7] create /lockdemo/son 'son value' Created /lockdemo/son
回到终端一查看
[zk: localhost:2181(CONNECTED) 33] WATCHER:: WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/lockdemo
说明:可以看到watcher提示的事件是:NodeChildrenChanged
十四,退出zkcli
[zk: localhost:2181(CONNECTED) 9] quit
来源:https://www.cnblogs.com/architectforest/p/12329084.html