可以发出什么命令来检查ZooKeeper服务器是Leader还是Follower?

030*_*030 25 linux zookeeper

已创建由三个 ZooKeeper 服务器组成的 ZooKeeper Quorum。

zoo.cfg所有三个ZooKeeper的服务器看起来位于如下:

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
Run Code Online (Sandbox Code Playgroud)

分析

很明显,三个 ZooKeeper 服务器之一将成为 ,Leader而其他服务器将成为Followers。如果LeaderZooKeeper 服务器已经关闭,Leader选举将重新开始。目的是检查另一个 ZooKeeper 服务器是否会成为服务器已关闭LeaderLeader服务器。

小智 54

可以使用包中nc包含的命令检查 ZooKeeper 服务器是领导者还是追随者netcat

echo stat | nc localhost 2181 | grep Mode
echo srvr | nc localhost 2181 | grep Mode #(From 3.3.0 onwards)
Run Code Online (Sandbox Code Playgroud)

如果 ZooKeeper 服务器是领导者,则该命令将返回:Mode: leader否则:Mode: follower

  • 和`独立` (2认同)
  • @sumit 这可能需要自己的 SO 问题,但一种方法是简单地读取 zoo.cfg 文件。 (2认同)

小智 5

或者,可以使用以下方法:

bin/zkServer.sh status
Run Code Online (Sandbox Code Playgroud)

它将在输出中打印模式:

ZooKeeper JMX enabled by default
Using config: /home/kafka/zookeeper/bin/../conf/zoo.cfg
Mode: follower
Run Code Online (Sandbox Code Playgroud)