ENV
Docker version 19.03.5, build 633a0ea838
ZooKeeper 3.8.0
Java 14.0.2
code
scripts/
is the scripts to create zookeeper docker cluster.
- view the cluster nodes
from Container
[hostuser@host-machine]$ docker exec --interactive --tty zookeeper-cluster-3 bin/zkCli.sh -server :2181 config | grep ^server
server.1=zoo1:2888:3888:participant;0.0.0.0:2181
server.2=zoo2:2888:3888:participant;0.0.0.0:2181
server.3=zoo3:2888:3888:participant;0.0.0.0:2181
[hostuser@host-machine]$ docker exec zookeeper-cluster-1 bash -c 'echo "srvr" | nc localhost 2181' | grep "Mode"
Mode: follower
[hostuser@host-machine]$ docker exec zookeeper-cluster-2 bash -c 'echo "srvr" | nc localhost 2181' | grep "Mode"
Mode: follower
[hostuser@host-machine]$ docker exec zookeeper-cluster-3 bash -c 'echo "srvr" | nc localhost 2181' | grep "Mode"
Mode: leader
[hostuser@host-machine]$ docker exec zookeeper-cluster-3 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: leader
[hostuser@host-machine]$ docker exec zookeeper-cluster-2 bash -c 'echo "stat" | nc localhost 2181' | grep "Mode"
Mode: follower
root@zoo2:/apache-zookeeper-3.8.0-bin# wget --quiet --output-document=/dev/stdout http://localhost:8080/commands/stat | grep "server_state"
"server_state" : "follower",
This command requires configuration option ZOO_4LW_COMMANDS_WHITELIST: "*"
from host
[hostuser@host-machine]$ echo "stat" | nc localhost 32181
Zookeeper version: 3.8.0-5a02a05eddb59aee6ac762f7ea82e92a68eb9c0f, built on 2022-02-25 08:49 UTC
Clients:
/172.19.0.1:36848[0](queued=0,recved=1,sent=0)
Latency min/avg/max: 1/8.5/25
Received: 7
Sent: 6
Connections: 1
Outstanding: 0
Zxid: 0x10000000e
Mode: leader
This command requires configuration option ZOO_4LW_COMMANDS_WHITELIST: "*"
[hostuser@host-machine]$ curl --silent http://localhost:28080/commands/stat | grep "server_state"
"server_state" : "follower",
This command requires configuration option ZOO_4LW_COMMANDS_WHITELIST: "*"
- enter into a cluster node and create a znode
[hostuser@host-machine]$ docker exec --interactive --tty zookeeper-cluster-2 bash
root@zoo2:/apache-zookeeper-3.8.0-bin# zkCli.sh -server zoo3:2181
Connecting to zoo3:2181
...
Welcome to ZooKeeper!
JLine support is enabled
2022-05-21 16:38:14,563 [myid:zoo3:2181] - INFO [main-SendThread(zoo3:2181):o.a.z.ClientCnxn$SendThread@1171] - Opening socket connection to server zoo3/172.19.0.13:2181.
...
[zk: zoo3:2181(CONNECTED) 0] create /test hello
Created /test
[zk: zoo3:2181(CONNECTED) 0] ls /
[test, zookeeper]
[zk: zoo3:2181(CONNECTED) 1] get /test
hello
[zk: zoo3:2181(CONNECTED) 2] quit
...
root@zoo2:/apache-zookeeper-3.8.0-bin# zkCli.sh -server zoo1:2181
...
[zk: zoo1:2181(CONNECTED) 0] get /test
hello
[zk: zoo1:2181(CONNECTED) 1] ls /
[test, zookeeper]
src/
is the souce code of Java api example.
apiexmaple Ref