ZooKeeper集群內(nèi)部各個節(jié)點(diǎn)(leader和follower)間使用端口2888進(jìn)行數(shù)據(jù)同步恬惯。
1.啟動ZooKeeper集群
[root@bigdata131 ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@bigdata131 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
[root@bigdata132 ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@bigdata132 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader
[root@bigdata133 ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@bigdata133 ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /root/trainings/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
2.先查看三個節(jié)點(diǎn)上的原始數(shù)據(jù)
[zk: bigdata131:2181(CONNECTED) 0] ls /
[node0000000004, node11, zookeeper]
[zk: bigdata131:2181(CONNECTED) 1] get /node11
11
cZxid = 0x400000023
ctime = Thu Nov 29 08:26:10 CST 2018
mZxid = 0x400000023
mtime = Thu Nov 29 08:26:10 CST 2018
pZxid = 0x400000034
cversion = 4
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 4
[zk: bigdata132:2181(CONNECTED) 0] ls /
[node0000000004, node11, zookeeper]
[zk: bigdata132:2181(CONNECTED) 1] get /node11
11
cZxid = 0x400000023
ctime = Thu Nov 29 08:26:10 CST 2018
mZxid = 0x400000023
mtime = Thu Nov 29 08:26:10 CST 2018
pZxid = 0x400000034
cversion = 4
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 4
[zk: bigdata133:2181(CONNECTED) 0] ls /
[node0000000004, node11, zookeeper]
[zk: bigdata133:2181(CONNECTED) 1] get /node11
11
cZxid = 0x400000023
ctime = Thu Nov 29 08:26:10 CST 2018
mZxid = 0x400000023
mtime = Thu Nov 29 08:26:10 CST 2018
pZxid = 0x400000034
cversion = 4
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 4
3.在leader上創(chuàng)建/更新/刪除節(jié)點(diǎn)
[zk: bigdata131:2181(CONNECTED) 2] create /node22 22
Created /node22
[zk: bigdata131:2181(CONNECTED) 3] set /node11 1111
cZxid = 0x400000023
ctime = Thu Nov 29 08:26:10 CST 2018
mZxid = 0x600000009
mtime = Fri Nov 30 08:17:35 CST 2018
pZxid = 0x400000034
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 4
[zk: bigdata131:2181(CONNECTED) 4] delete /node0000000004
4.在follower上查看同步后的結(jié)果
[zk: bigdata132:2181(CONNECTED) 2] ls /
[node11, node22, zookeeper]
[zk: bigdata132:2181(CONNECTED) 3] get /node11
1111
cZxid = 0x400000023
ctime = Thu Nov 29 08:26:10 CST 2018
mZxid = 0x600000009
mtime = Fri Nov 30 08:17:35 CST 2018
pZxid = 0x400000034
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 4
[zk: bigdata132:2181(CONNECTED) 4] get /node22
22
cZxid = 0x600000008
ctime = Fri Nov 30 08:17:16 CST 2018
mZxid = 0x600000008
mtime = Fri Nov 30 08:17:16 CST 2018
pZxid = 0x600000008
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
[zk: bigdata133:2181(CONNECTED) 2] ls /
[node11, node22, zookeeper]
[zk: bigdata133:2181(CONNECTED) 3] get /node11
1111
cZxid = 0x400000023
ctime = Thu Nov 29 08:26:10 CST 2018
mZxid = 0x600000009
mtime = Fri Nov 30 08:17:35 CST 2018
pZxid = 0x400000034
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 4
[zk: bigdata133:2181(CONNECTED) 4] get /node22
22
cZxid = 0x600000008
ctime = Fri Nov 30 08:17:16 CST 2018
mZxid = 0x600000008
mtime = Fri Nov 30 08:17:16 CST 2018
pZxid = 0x600000008
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 0
可以看到:leader上所做的修改,立即被同步到了所有follower上。這里即使原來的leader掛了,那么通過選舉將某一個follower選為新的leader之后般眉,仍可以正確的對外提供服務(wù)菌湃。