#一、命令
[root@VM_31_182_centos bin]# ./zkCli.sh -server 127.0.0.1:2181
![35158-20170126152740941-1740414789.png](https://images2015.cnblogs.com/blog/35158/201701/35158-20170126152740941-1740414789.png)
#二、帮助命令
help
![35158-20170126152741956-802848799.png](https://images2015.cnblogs.com/blog/35158/201701/35158-20170126152741956-802848799.png)
#三、创建、修改、删除、退出demo(ls和/之间要有空格)
##1 )使用 ls 命令来查看当前 ZooKeeper 中所包含的内容:
[zk: 202.115.36.251:2181(CONNECTED) 1] ls /
[zookeeper]
##2 )创建一个新的 znode ,使用 create /zk myData 。这个命令创建了一个新的 znode 节点“ zk ”以及与它关联的字符串:
[zk: 202.115.36.251:2181(CONNECTED) 2] create /zk "myData"
Created /zk
##3 )我们运行 get 命令来确认 znode 是否包含我们所创建的字符串:
[zk: 202.115.36.251:2181(CONNECTED) 3] get /zk
cZxid = 0x6
ctime = Thu Sep 12 15:49:16 CST 2013
mZxid = 0x7
mtime = Thu Sep 12 15:49:38 CST 2013
pZxid = 0x6
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 0
##4 )下面我们通过 set 命令来对 zk 所关联的字符串进行设置:
[zk: 202.115.36.251:2181(CONNECTED) 4] set /zk "zsl"
"myData"
cZxid = 0x4
ctime = Thu Sep 12 15:46:37 CST 2013
mZxid = 0x4
mtime = Thu Sep 12 15:46:37 CST 2013
pZxid = 0x4
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
##5 )下面我们将刚才创建的 znode 删除:
[zk: 202.115.36.251:2181(CONNECTED) 5] delete /zk
当然,我们还可以创建有曾次的目录,比如使用create /zk/node1在zk目录下创建新的目录node1
[zk: 192.168.255.133:2181(CONNECTED) 18] create /zk/node1 "node1"
Created /zk/node1
[zk: 192.168.255.133:2181(CONNECTED) 19] ls /zk
[node1]
#四、四字命令
[zk: 202.115.36.251:2181(CONNECTED) 1] ls /
[zookeeper]
##2 )创建一个新的 znode ,使用 create /zk myData 。这个命令创建了一个新的 znode 节点“ zk ”以及与它关联的字符串:
[zk: 202.115.36.251:2181(CONNECTED) 2] create /zk "myData"
Created /zk
##3 )我们运行 get 命令来确认 znode 是否包含我们所创建的字符串:
[zk: 202.115.36.251:2181(CONNECTED) 3] get /zk
cZxid = 0x6
ctime = Thu Sep 12 15:49:16 CST 2013
mZxid = 0x7
mtime = Thu Sep 12 15:49:38 CST 2013
pZxid = 0x6
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 0
##4 )下面我们通过 set 命令来对 zk 所关联的字符串进行设置:
[zk: 202.115.36.251:2181(CONNECTED) 4] set /zk "zsl"
"myData"
cZxid = 0x4
ctime = Thu Sep 12 15:46:37 CST 2013
mZxid = 0x4
mtime = Thu Sep 12 15:46:37 CST 2013
pZxid = 0x4
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
##5 )下面我们将刚才创建的 znode 删除:
[zk: 202.115.36.251:2181(CONNECTED) 5] delete /zk
当然,我们还可以创建有曾次的目录,比如使用create /zk/node1在zk目录下创建新的目录node1
[zk: 192.168.255.133:2181(CONNECTED) 18] create /zk/node1 "node1"
Created /zk/node1
[zk: 192.168.255.133:2181(CONNECTED) 19] ls /zk
[node1]
1、需要先安装nc命令,否则会提示nc command not found的错误
yum install -y nc
2、常用四字命令(执行目录不限)
![35158-20170126152743441-710204474.png](https://images2015.cnblogs.com/blog/35158/201701/35158-20170126152743441-710204474.png)
2.1 conf [ echo conf | nc 127.0.0.1 2181 ] (输出相关服务配置的详细信息)
![35158-20170126152746722-255898028.png](https://images2015.cnblogs.com/blog/35158/201701/35158-20170126152746722-255898028.png)
2.2 stat [ echo stat | nc 127.0.0.1 2181 ] (输出关于性能和连接的客户端的列表)
![35158-20170126152747878-1870215016.png](https://images2015.cnblogs.com/blog/35158/201701/35158-20170126152747878-1870215016.png)
2.3 cons [ echo cons | nc 127.0.0.1 2181 ] (列出所有连接到服务器的客户端的完全的连接)
![35158-20170126152748925-1934178745.png](https://images2015.cnblogs.com/blog/35158/201701/35158-20170126152748925-1934178745.png)
2.4 envi [ echo envi | nc 127.0.0.1 2181 ] (输出关于服务环境的详细信息)
![35158-20170126152750237-1308526628.png](https://images2015.cnblogs.com/blog/35158/201701/35158-20170126152750237-1308526628.png)
2.5 ruok[ echo ruok| nc 127.0.0.1 2181 ] (测试服务是否处于正确状态)
![35158-20170126152751441-1754389816.png](https://images2015.cnblogs.com/blog/35158/201701/35158-20170126152751441-1754389816.png)
#五、参考资料