ZooKeeper集群内部各个节点(leader和follower)间使用端口2888进行数据同步。
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.先查看三个节点上的原始数据
[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上创建/更新/删除节点
[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上查看同步后的结果
[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之后,仍可以正确的对外提供服务。
网友评论