已创建由三个 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 服务器中的一个将成为 ,Leader
其他的将成为Followers
。如果Leader
ZooKeeper 服务器已关闭,Leader
则选举将重新开始。目的是检查如果服务器已关闭,另一个 ZooKeeper 服务器是否会Leader
成为Leader
。
答案1
可以使用包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
答案2
或者可以使用以下命令:
bin/zkServer.sh status
它将在输出中打印模式:
ZooKeeper JMX enabled by default
Using config: /home/kafka/zookeeper/bin/../conf/zoo.cfg
Mode: follower
答案3
echo "srvr" | nc localhost 2181 | grep "Mode"
root@zoo2:/apache-zookeeper-3.8.0-bin# echo "srvr" | nc localhost 2181 | grep "Mode" Mode: follower
使用
bin/zkServer.sh status
:root@zoo2:/apache-zookeeper-3.8.0-bin# bin/zkServer.sh status ZooKeeper JMX enabled by default Using config: /conf/zoo.cfg Client port found: 2181. Client address: localhost. Client SSL: false. Mode: follower
如果你设置了
ZOO_4LW_COMMANDS_WHITELIST: "*"
,你可以使用echo "stat" | nc localhost 2181
root@zoo2:/apache-zookeeper-3.8.0-bin# echo "stat" | nc localhost 2181 Zookeeper version: 3.8.0-5a02a05eddb59aee6ac762f7ea82e92a68eb9c0f, built on 2022-02-25 08:49 UTC Clients: /127.0.0.1:47200[0](queued=0,recved=1,sent=0) Latency min/avg/max: 0/0.0/0 Received: 5 Sent: 4 Connections: 1 Outstanding: 0 Zxid: 0x0 Mode: follower Node count: 5
使用
http://localhost:8080/commands/srvr
root@zoo2:/apache-zookeeper-3.8.0-bin# wget --quiet --output-document=/dev/stdout http://localhost:8080/commands/srvr | grep "server_state" "server_state" : "follower",
或者
root@zoo2:/apache-zookeeper-3.8.0-bin# curl --silent http://localhost:8080/commands/stat | grep "server_state"
3.5.0 中的新功能:AdminServer 是一个嵌入式 Jetty 服务器,它为四个字母的单词命令提供 HTTP 接口。默认情况下,服务器在端口 8080 上启动,并通过转到 URL“/commands/[命令名称]”发出命令,例如 http://localhost:8080/commands/stat。命令响应以 JSON 形式返回。与原始协议不同,命令不限于四个字母的名称,命令可以有多个名称;例如,“stmk”也可以称为“set_trace_mask”。要查看所有可用命令的列表,请将浏览器指向 URL /commands(例如,http://localhost:8080/commands)。有关如何更改端口和 URL,请参阅 AdminServer 配置选项。
使用
http://localhost:8080/commands/leader
root@zoo2:/apache-zookeeper-3.8.0-bin# curl --silent http://localhost:8080/commands/leader { "is_leader" : false, "leader_id" : 3, "leader_ip" : "zoo3", "command" : "leader", "error" : null }