zookeeper
Is a strong consistent distributed database from multiple nodes together form a distributed cluster, hang up any node, the database can still work.
Stand-alone mode
Download the zookeeper
packed files and decompress
➜ ~ tar -xvzf apache-zookeeper-3.5.6-bin.tar.gz
Enter zookeeper
decompression directory, rename conf
the configuration files in a directory
➜ apache-zookeeper-3.5.6-bin mv conf/zoo_sample.cfg conf/zoo.cfg
Start zookeeper
using the start-foreground
boot to the front for easy viewing services output
➜ apache-zookeeper-3.5.6-bin bin/zkServer.sh start-foreground
Arbitration mode
In the zoo.cfg
editing on the basis of creation zoo_1.cfg
, zoo_2.cfg
andzoo_3.cfg
We need to configure additional information added. Colon-separated second and third part is the TCP
port number, respectively, for arbitration and communication cluster head election.
server.1=127.0.0.1:2222:2223
server.2=127.0.0.1:3333:3334
server.3=127.0.0.1:4444:4445
When starting a server, we need to know which server is started. zookeeper
By reading dataDir
called in myid
to get the server's file ID
information.
➜ zookeeper echo 1 > zoo_1/data/myid
➜ zookeeper echo 2 > zoo_2/data/myid
➜ zookeeper echo 3 > zoo_3/data/myid
Start the service from zoo_1
the start
➜ zoo_1 ~/apache-zookeeper-3.5.6-bin/bin/zkServer.sh start-foreground ./zoo_1.cfg
Because we only started three zookeeper
one, the entire server can not run.
2020-01-01 12:08:37,016 [myid:1] - INFO [QuorumPeer[myid=1](plain=/0:0:0:0:0:0:0:0:2181)(secure=disabled):QuorumPeer@1193] - LOOKING
2020-01-01 12:08:37,016 [myid:1] - INFO [QuorumPeer[myid=1](plain=/0:0:0:0:0:0:0:0:2181)(secure=disabled):FastLeaderElection@885] - New election. My id = 1, proposed zxid=0x0 2020-01-01 12:08:37,021 [myid:1] - WARN [WorkerSender[myid=1]:QuorumCnxManager@679] www.xinyueylzc.cn- Cannot open channel to 2 at election address /127.0.0.1:3334 java.net.ConnectException: Connection refused (Connection refused) ... 2020-01-01 12:08:37,031 [myid:1] - WARN [WorkerSender[myid=1]:QuorumCnxManager@679] - Cannot open channel to 3 at election address /127.0.0.1:4445 java.net.ConnectException: Connection refused (Connection refused) ...
Start a second server, which can constitute a quorum arbitration
➜ zoo_2 ~/apache-zookeeper-3.5.6-bin/bin/zkServer.sh start-foreground ./zoo_2.cfg
Two were selected as the server cluster head
2020-01-01 12:10:40,802 [myid:2] - INFO [QuorumPeer[myid=2](plain=/0:0:0:0:0:0:0:0:2182)(secure=disabled):Leader@464] - LEADING - LEADER ELECTION TOOK - 54 MS
2020-01-01 12:10:40,804 [myid:2] - INFO [QuorumPeer[myid=2](www.lafei6d.cn plain=/0:0:0:0:0:0:0:0:2182)(secure=disabled):FileTxnSnapLog@384] - Snapshotting: 0x0 to /tmp/zookeeper/zoo_2/data/version-2/snapshot.0 2020-01-01 12:10:40,812 [myid:2] - INFO [LearnerHandler-www.huizhonggjpt.cn /127.0.0.1:62308:LearnerHandler@406] - Follower sid: 1 : info : 127.0.0.1:2222:2223:participant 2020-01-01 12:10:40,816 [myid:2] - INFO [LearnerHandler-www.shentuylzc.cn /127.0.0.1:62308:ZKDatabase@295] - On disk txn sync enabled with snapshotSizeFactor 0.33
Access cluster
➜ bin ./zkCli.sh -server 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183
Examples publish and subscribe
A start zk_0
, create a temporary znode
node:
[zk: 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183(CONNECTED) 9] create www.feishenbo.cn-e /master "this is master" Created /master
Start another zk_1
, to znode
set up a monitoring point:
[zk: 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183(CONNECTED) 3] ls /master true 'ls path [watch]' has been deprecated. Please use 'ls [-w] path' instead. []
And then start another zk_2
, to znode
set up a monitoring point:
[zk: 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183(CONNECTED) 1] ls /master true 'ls path [watch]' has been deprecated.www.jujinyule.com Please use 'ls [-w] path' instead. []
In zk_0
deleted in master
, zk_1
and zk_2
at the same time receive a notification message is deleted
zk_0:
[zk: 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183(www.letianhuanchao.cn CONNECTED) 10] delete /master zk_1/zk_2: [zk: 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183(CONNECTED) 2] WATCHER:: WatchedEvent state:SyncConnected type:NodeDeleted path:/master