安装Zookeeper
https://blog.csdn.net/greenplum_xiaofan/article/details/99020516#31_Zookeeper_40
1) 树形结构: /
2) ZK中的每个节点:znode,唯一的路径标识
a) 每个znode都有自己的版本号
b) znode信息发生变化,那么version+1
c) znode数据量不要太大,几K就差不多了,都是存一些配置信息
d) 权限
e) Watcher
3) znode有两种类型
a)临时:当前session有效,不能有子节点的。
b)永久:不依赖于session
4) znode的四种形式
PERSISTENT
PERSISTENT_SEQUENTIAL 带顺序编号的
EPHEMERAL
EPHEMERAL_SEQUENTIA
ZK节点数:奇数个(>=3)
选举:leader+flower
ZK集群的写操作,leader负责,会把通知所有节点写入操作
只有收到半数以上节点的成功反馈,才算成功
启动ZK
[hadoop@vm01 bin]$ ./zkServer.sh start
查看ZK状态,单节点就是standalone,集群是leader,flower
[hadoop@vm01 bin]$ ./zkServer.sh status
打开ZK客户端
[hadoop@vm01 bin]$ ./zkCli.sh
[zk: localhost:2181(CONNECTED) 11] quit
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper, kafka]
ls2 可以理解为 ls+stat
[zk: localhost:2181(CONNECTED) 1] ls2 /
[zookeeper, kafka] #下面是ZK的状态信息
cZxid = 0x0
ctime = Wed Dec 31 16:00:00 PST 1969
mZxid = 0x0
mtime = Wed Dec 31 16:00:00 PST 1969
pZxid = 0x2
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2
显示当前状态信息,
[zk: localhost:2181(CONNECTED) 2] stat /
cZxid = 0x0
ctime = Wed Dec 31 16:00:00 PST 1969
mZxid = 0x0
mtime = Wed Dec 31 16:00:00 PST 1969
pZxid = 0x2
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 2
获取
[zk: localhost:2181(CONNECTED) 3] get /zookeeper
cZxid = 0x0 #zk当前的id
ctime = Wed Dec 31 16:00:00 PST 1969
mZxid = 0x0 #修改后的id
mtime = Wed Dec 31 16:00:00 PST 1969
pZxid = 0x0 #最后跟新的子节点id
cversion = -1 #子节点版本号
dataVersion = 0 #数据版本
aclVersion = 0 #权限版本
ephemeralOwner = 0x0 #是否是临时的
dataLength = 0 #数据长度
numChildren = 1 #有几个孩子
create
[-s] #带不带编号
[-e] #临时
[zk: localhost:2181(CONNECTED) 4] create /ruoze ruoze-data
Created /ruoze
[zk: localhost:2181(CONNECTED) 5] ls /
[ruoze, zookeeper, kafka]
[zk: localhost:2181(CONNECTED) 6] get /ruoze
ruoze-data #数据
cZxid = 0xe7
ctime = Thu Aug 15 06:16:21 PDT 2019
mZxid = 0xe7
mtime = Thu Aug 15 06:16:21 PDT 2019
pZxid = 0xe7
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 0
[zk: localhost:2181(CONNECTED) 7] create -e /ruoze/xiaoruoze xiaoruoze
Created /ruoze/xiaoruoze
[zk: localhost:2181(CONNECTED) 8] get /ruoze
ruoze-data
cZxid = 0xe7
ctime = Thu Aug 15 06:16:21 PDT 2019
mZxid = 0xe7
mtime = Thu Aug 15 06:16:21 PDT 2019
pZxid = 0xe8
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 1 #有一个孩子了
[zk: localhost:2181(CONNECTED) 9] get /ruoze/xiaoruoze
xiaoruoze
cZxid = 0xe8
ctime = Thu Aug 15 06:18:09 PDT 2019
mZxid = 0xe8
mtime = Thu Aug 15 06:18:09 PDT 2019
pZxid = 0xe8
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x16c88e4a0770001 #表示是临时节点
dataLength = 9
numChildren = 0
[zk: localhost:2181(CONNECTED) 10] quit
[hadoop@vm01 bin]$ ./zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /ruoze
[] #发现没有子节点了,这就是临时的特点,退出就没了
加编号,顺序
分布式锁的一个实现,会借助这个
[zk: localhost:2181(CONNECTED) 1] create -s /ruoze/seq seq
Created /ruoze/seq0000000001
[zk: localhost:2181(CONNECTED) 2] create -s /ruoze/seq seq
Created /ruoze/seq0000000002
[zk: localhost:2181(CONNECTED) 3] create -s /ruoze/seq seq
Created /ruoze/seq0000000003
[zk: localhost:2181(CONNECTED) 4] create -s /ruoze/seq seq
Created /ruoze/seq0000000004
[zk: localhost:2181(CONNECTED) 5] create -s /ruoze/seq seq
Created /ruoze/seq0000000005
[zk: localhost:2181(CONNECTED) 6]
[zk: localhost:2181(CONNECTED) 6] ls /ruoze
[seq0000000005, seq0000000003, seq0000000004, seq0000000001, seq0000000002]
修改数据,查看版本信息
[zk: localhost:2181(CONNECTED) 7] get /ruoze
ruoze-data
cZxid = 0xe7
ctime = Thu Aug 15 06:16:21 PDT 2019
mZxid = 0xe7
mtime = Thu Aug 15 06:16:21 PDT 2019
pZxid = 0xef
cversion = 7
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 10
numChildren = 5
[zk: localhost:2181(CONNECTED) 8] set /ruoze www.ruozedata.com
cZxid = 0xe7
ctime = Thu Aug 15 06:16:21 PDT 2019
mZxid = 0xf0
mtime = Thu Aug 15 06:24:30 PDT 2019
pZxid = 0xef
cversion = 7
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 17
numChildren = 5
[zk: localhost:2181(CONNECTED) 9] get /ruoze
www.ruozedata.com
cZxid = 0xe7
ctime = Thu Aug 15 06:16:21 PDT 2019
mZxid = 0xf0
mtime = Thu Aug 15 06:24:30 PDT 2019
pZxid = 0xef
cversion = 7
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 17
numChildren = 5
删除
[zk: localhost:2181(CONNECTED) 11] delete /ruoze/seq0000000001
[zk: localhost:2181(CONNECTED) 12] ls /ruoze
[seq0000000005, seq0000000003, seq0000000004, seq0000000002]
修改seq2的数据,然后删除,带版本的,0的版本是删除不掉的,
因为修改之后版本+1,必须用最新的版本号才能删除
set,delete都可以带上版本号,建议带上版本号,相当于加了一个乐观锁
[zk: localhost:2181(CONNECTED) 14] set /ruoze/seq0000000002 seq2
[zk: localhost:2181(CONNECTED) 16] delete /ruoze/seq0000000002 0
version No is not valid : /ruoze/seq0000000002
[zk: localhost:2181(CONNECTED) 17] delete /ruoze/seq0000000002 1
[hadoop@vm01 bin]$ echo stat |nc localhost 2181
Zookeeper version: 3.4.5-cdh5.7.0--1, built on 03/23/2016 18:31 GMT
Clients:
/0:0:0:0:0:0:0:1:50179[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 0/0/11
Received: 229
Sent: 228
Connections: 1 #有多少连接数
Outstanding: 0
Zxid: 0xf7
Mode: standalone
Node count: 146 #节点数量
[hadoop@vm01 bin]$ echo ruok |nc localhost 2181
imok
[hadoop@vm01 bin]$ echo dump |nc localhost 2181
SessionTracker dump:
Session Sets (0):
ephemeral nodes dump:
Sessions with Ephemerals (0): #临时节点有多少个
可以看到ZK的配置
[hadoop@vm01 bin]$ echo conf |nc localhost 2181
clientPort=2181
dataDir=/home/hadoop/app/zookeeper-3.4.5-cdh5.7.0/data/version-2
dataLogDir=/home/hadoop/app/zookeeper-3.4.5-cdh5.7.0/dataLog/version-2
tickTime=2000
maxClientCnxns=60
minSessionTimeout=4000
maxSessionTimeout=40000
serverId=0
[hadoop@vm01 bin]$ echo cons |nc localhost 2181
/0:0:0:0:0:0:0:1:50187[0](queued=0,recved=1,sent=0)
监控
[hadoop@vm01 bin]$ echo mntr |nc localhost 2181
zk_version 3.4.5-cdh5.7.0--1, built on 03/23/2016 18:31 GMT
zk_avg_latency 0
zk_max_latency 11
zk_min_latency 0
zk_packets_received 234
zk_packets_sent 233
zk_num_alive_connections 1
zk_outstanding_requests 0
zk_server_state standalone
zk_znode_count 146
zk_watch_count 0
zk_ephemerals_count 0
zk_approximate_data_size 11597
zk_open_file_descriptor_count 28
zk_max_file_descriptor_count 65536
查看有多少watcher
[zk: localhost:2181(CONNECTED) 1] get /ruoze watch #加一把锁
[hadoop@vm01 bin]$ echo wchs |nc localhost 2181
1 connections watching 1 paths
Total watches:1 #数量1个