查看状态与启动客户端

查看状态

[worker@103-17-143-sh-K05 bin]$ /ssd1/workspace/zookeeper/zookeeper2-3.4.12/bin/./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /ssd1/workspace/zookeeper/zookeeper2-3.4.12/bin/../conf/zoo.cfg
Mode: leader

启动客户端

[worker@103-17-143-sh-K05 bin]$ /ssd1/workspace/zookeeper/zookeeper2-3.4.12/bin/zkCli.sh -server localhost:2181
Connecting to localhost:2181
...(省略启动信息)
[zk: localhost:2181(CONNECTED) 0]

基本命令

帮助help

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

查看目录ls

[zk: localhost:2181(CONNECTED) 3] ls /
[jodis, zk, zookeeper, codis, codis3, worker_id_forever]

创建目录create

[zk: localhost:2181(CONNECTED) 4] create /zk_test hanchao_test
Created /zk_test

获取数据get

[zk: localhost:2181(CONNECTED) 5] get /zk_test
hanchao_test
cZxid = 0x4000092ec
ctime = Thu Mar 07 10:34:59 CST 2019
mZxid = 0x4000092ec
mtime = Thu Mar 07 10:34:59 CST 2019
pZxid = 0x4000092ec
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 0
  • cZxid Zookeeper为节点分配的Id
  • cTime 节点创建时间
  • mZxid 修改后的id
  • mtime 修改时间
  • pZxid 子节点id
  • cversion 子节点的version
  • dataVersion 当前节点数据的版本号
  • aclVersion 权限Version
  • dataLength 数据长度
  • numChildren 子节点个数

设置数据set

[zk: localhost:2181(CONNECTED) 6] set /zk_test hanchaohanchao
cZxid = 0x4000092ec
ctime = Thu Mar 07 10:34:59 CST 2019
mZxid = 0x4000092ed
mtime = Thu Mar 07 10:35:55 CST 2019
pZxid = 0x4000092ec
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 14
numChildren = 0

检查状态stat

[zk: localhost:2181(CONNECTED) 12] stat /hanchao_test/defatul_node
cZxid = 0x4000092f3
ctime = Thu Mar 07 10:50:52 CST 2019
mZxid = 0x4000092f6
mtime = Thu Mar 07 10:54:22 CST 2019
pZxid = 0x4000092f3
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 22
numChildren = 0

删除路径delete

[zk: localhost:2181(CONNECTED) 8] delete /zk_test
[zk: localhost:2181(CONNECTED) 9] ls /
[jodis, zk, zookeeper, codis, codis3, worker_id_forever]

四类节点

create /path data

无序持久节点(默认的)

[zk: localhost:2181(CONNECTED) 20] create /hanchao_test/defatul_node "persistent,no-sequence"
Created /hanchao_test/defatul_node
[zk: localhost:2181(CONNECTED) 21] ls /hanchao_test
[defatul_node]

有序持久节点

  • 有序节点名称=有序节点路基+10位顺序号
  • -s:sequence
[zk: localhost:2181(CONNECTED) 3] create -s /hanchao_test/sequence_node "persistent,sequence"
Created /hanchao_test/sequence_node0000000001
[zk: localhost:2181(CONNECTED) 4] ls /hanchao_test
[defatul_node, sequence_node0000000001]
[zk: localhost:2181(CONNECTED) 5] create -s /hanchao_test/sequence_node "persistent,sequence"
Created /hanchao_test/sequence_node0000000002
[zk: localhost:2181(CONNECTED) 6] ls /hanchao_test
[defatul_node, sequence_node0000000001, sequence_node0000000002]

无序临时节点

  • 临时节点在client端口连接之后关闭
  • -e: temp
# client1 创建之后断开连接
[zk: localhost:2181(CONNECTED) 3] create -e /hanchao_test/temp_node "no-persistent,no-sequence"
Created /hanchao_test/temp_node
[zk: localhost:2181(CONNECTED) 4] ls /hanchao_test
[temp_node, defatul_node, sequence_node0000000001, sequence_node0000000002]

# client2
# client1断开之前的状态
[zk: localhost:2181(CONNECTED) 7] ls /hanchao_test
[temp_node, defatul_node, sequence_node0000000001, sequence_node0000000002]
# client1断开之后的状态
[zk: localhost:2181(CONNECTED) 8] ls /hanchao_test
[defatul_node, sequence_node0000000001, sequence_node0000000002]

有序临时节点

[zk: localhost:2181(CONNECTED) 1] create -s -e /hanchao_test/temp_node_p "no-persistent,order"
Created /hanchao_test/temp_node_p0000000004
[zk: localhost:2181(CONNECTED) 2] ls /hanchao_test
[defatul_node, temp_node_p0000000004, sequence_node0000000001, sequence_node0000000002]
[zk: localhost:2181(CONNECTED) 3] create -s -e /hanchao_test/temp_node_p "no-persistent,order"
Created /hanchao_test/temp_node_p0000000005
[zk: localhost:2181(CONNECTED) 4] ls /hanchao_test
[defatul_node, temp_node_p0000000005, temp_node_p0000000004, sequence_node0000000001, sequence_node0000000002]
  • 同一个目录下,顺序节点的顺序号是共用的

监视watch

  • 当指定的znode或znode的子数据更改时,监视器会显示通知。
  • 你只能在 get 命令中设置watch。
# 在client1中监视
[zk: localhost:2181(CONNECTED) 14] get /hanchao_test/defatul_node 1
persistent,no-sequence
cZxid = 0x4000092f3
ctime = Thu Mar 07 10:50:52 CST 2019
mZxid = 0x4000092f6
mtime = Thu Mar 07 10:54:22 CST 2019
pZxid = 0x4000092f3
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 22
numChildren = 0

# 在client2中set
[zk: localhost:2181(CONNECTED) 0] set /hanchao_test/defatul_node "hhhhh"
cZxid = 0x4000092f3
ctime = Thu Mar 07 10:50:52 CST 2019
mZxid = 0x400009301
mtime = Thu Mar 07 11:10:26 CST 2019
pZxid = 0x4000092f3
cversion = 0
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0

# client1监视到数据变化
[zk: localhost:2181(CONNECTED) 15]
WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/hanchao_test/defatul_node
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐