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

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

位于所有三台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服务器之一将成为Leader和其他Followers 。 如果Leader ZooKeeper服务器已经closures, Leader选举将再次开始。 目的是检查如果Leader Leader服务器已经closures,另一个ZooKeeper服务器是否将成为Leader Leader


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

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

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

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