zookeeper相关命令
zk服务器端命令
命令 | 功能 |
zkServer.sh start | 启动zookeeper |
zkServer.sh stop | 关闭zookeeper |
zkServer.sh restart | 重启zookeeper |
zkServer.sh status | 查看zookeeper服务状态 |
zkCli.sh [-server] [ip:port] | 启动zookeeper客户端 |
zk客户端命令
命令 | 功能 |
help | 显示所有操作命令 |
ls path | 查看当前znode子结点 |
ls2 path | 查看当前znode子结点并显示znode元数据 |
create [-s] [-e] path data |
创建znode,并添加数据 -e:临时节点 |
get path | 获得znode的值 |
set | 重新设置znode的值 |
stat | 查看znode状态 |
delete | 删除znode |
rmr | 递归删除znode |
quit | 退出客户端 |
znode操作
普通结点
[root@zk01 zookeeper]# zkCli.sh -server zk02:2181 [zk: zk02:2181(CONNECTED) 5] ls / [zookeeper] [zk: zk02:2181(CONNECTED) 6] create /a "abc" Created /a [zk: zk02:2181(CONNECTED) 7] get /a abc
临时结点
[zk: zk02:2181(CONNECTED) 8] create -e /b 'bcd' Created /b [zk: zk02:2181(CONNECTED) 9] ls / [a, b, zookeeper] 退出客户端 [zk: zk02:2181(CONNECTED) 10] quit WATCHER:: WatchedEvent state:Closed type:None path:null 2021-03-06 21:12:34,506 [myid:] - INFO [main:ZooKeeper@1422] - Session: 0x200002280f00000 closed 2021-03-06 21:12:34,506 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down for session: 0x200002280f00000 重新连接客户端查看结点 [root@zk01 zookeeper]# zkCli.sh -server zk02:2181 [zk: zk02:2181(CONNECTED) 0] ls / [a, zookeeper]
序列结点
[zk: zk02:2181(CONNECTED) 0] ls / [a, zookeeper] [zk: zk02:2181(CONNECTED) 1] create -s /c 'abc' Created /c0000000003 [zk: zk02:2181(CONNECTED) 2] ls / [a, c0000000003, zookeeper] [zk: zk02:2181(CONNECTED) 3] create -s /c 'abc' Created /c0000000004 [zk: zk02:2181(CONNECTED) 8] ls / [a, c0000000003, c0000000004, zookeeper] [zk: zk02:2181(CONNECTED) 10] get /c0000000004 abc
查看zookeeper的事务日志
在ZooKeeper集群启动后,当第一个客户端连接到某个服务器节点时,会创建一个会话,这个会话也是事务,于是创建第一个事务日志,一般名为log.100000001
,这里的100000001是这次会话的事务id(zxid)。之后的事务都将写入到这个文件中,直到拍下一个快照。
[root@zk02 zookeeper]# ll zk*/* -rw-r--r-- 1 root root 2 Mar 6 22:51 zkSnapshot/myid -rw-r--r-- 1 root root 4 Mar 6 22:52 zkSnapshot/zookeeper_server.pid -rw-r--r-- 1 root root 2 Mar 6 22:50 zkTxData/myid zkSnapshot/version-2: total 12 -rw-r--r-- 1 root root 1 Mar 6 22:52 acceptedEpoch -rw-r--r-- 1 root root 1 Mar 6 22:52 currentEpoch -rw-r--r-- 1 root root 541 Mar 6 22:52 snapshot.0
客户端连接zookeeper后查看
[root@zk02 zookeeper]# ll zk*/* -rw-r--r-- 1 root root 2 Mar 6 22:51 zkSnapshot/myid -rw-r--r-- 1 root root 4 Mar 6 22:52 zkSnapshot/zookeeper_server.pid -rw-r--r-- 1 root root 2 Mar 6 22:50 zkTxData/myid zkSnapshot/version-2: total 12 -rw-r--r-- 1 root root 1 Mar 6 22:52 acceptedEpoch -rw-r--r-- 1 root root 1 Mar 6 22:52 currentEpoch -rw-r--r-- 1 root root 541 Mar 6 22:52 snapshot.0 zkTxData/version-2: total 8 -rw-r--r-- 1 root root 67108880 Mar 6 23:07 log.100000001
如果是事务ZXID5触发的拍快照,那么快照名就是snapshot.ZXID5,拍完后,下一个事务的ID就是ZXID6,于是新的事务日志名为log.ZXID6。
[root@zk03 zookeeper]# java -cp /opt/module/zookeeper/lib/zookeeper-3.5.5.jar:/opt/module/zookeeper/lib/slf4j-api-1.7.25.jar:/opt/module/zookeeper/lib/zookeeper-jute-3.5.5.jar org.apache.zookeeper.server.LogFormatter /opt/module/zookeeper/zkTxData/version-2/log.100000001 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. ZooKeeper Transactional Log File with dbid 0 txnlog format version 2 3/6/21 11:07:22 PM EST session 0x200008a6df70000 cxid 0x0 zxid 0x100000001 createSession 30000 3/6/21 11:09:41 PM EST session 0x200008a6df70000 cxid 0x2 zxid 0x100000002 create '/a,,v{s{31,s{'world,'anyone}}},F,1 3/6/21 11:09:48 PM EST session 0x2
00008a6df70000 cxid 0x3 zxid 0x100000003 create '/b,,v{s{31,s{'world,'anyone}}},F,2 3/6/21 11:10:04 PM EST session 0x200008a6df70000 cxid 0x4 zxid 0x100000004 closeSession null 3/6/21 11:10:13 PM EST session 0x200008a6df70001 cxid 0x0 zxid 0x100000005 createSession 30000 EOF reached after 5 txns.