Linux

可以發出什麼命令來檢查 ZooKeeper 伺服器是領導者還是追隨者?

  • February 26, 2020

由三個 ZooKeeper 伺服器組成的 ZooKeeper Quorum 已創建。

所有三個 ZooKeeper 伺服器上的zoo.cfg位置如下所示:

maxClientCnxns=50
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
dataDir=/var/lib/zookeeper
# the port at which the clients will connect
clientPort=2181

server.1=<ip-address-1>:2888:3888
server.2=<ip-address-2>:2888:3888
server.3=<ip-address-3>:2888:3888

分析

很明顯,三個 ZooKeeper 伺服器中的一個將變為 theLeader和other Followers。如果LeaderZooKeeper 伺服器已經關閉,Leader選舉將重新開始。目的是檢查另一個 ZooKeeper 伺服器是否將成為Leader伺服器Leader是否已關閉。

nc可以使用包中包含的命令檢查 ZooKeeper 伺服器是領導者還是跟隨者netcat

echo stat | nc localhost 2181 | grep Mode
echo srvr | nc localhost 2181 | grep Mode #(From 3.3.0 onwards)

如果 ZooKeeper 伺服器是領導者,則命令將返回:Mode: leader否則:Mode: follower

或者,可以使用以下內容:

bin/zkServer.sh status

它將在輸出中列印模式:

ZooKeeper JMX enabled by default
Using config: /home/kafka/zookeeper/bin/../conf/zoo.cfg
Mode: follower

引用自:https://serverfault.com/questions/601409