本文记录zookeeper集群的搭建过程及简单测试。
配置
zk1
tickTime=2000 |
The entries of the form server.X list the servers that make up the ZooKeeper service. When the server starts up, it knows which server it is by looking for the file myid in the data directory. That file has the contains the server number, in ASCII.
Finally, note the two port numbers after each server name: " 2888" and "3888". Peers use the former port to connect to other peers. Such a connection is necessary so that peers can communicate, for example, to agree upon the order of updates. More specifically, a ZooKeeper server uses this port to connect followers to the leader. When a new leader arises, a follower opens a TCP connection to the leader using this port. Because the default leader election also uses TCP, we currently require another port for leader election. This is the second port in the server entry.
zk2
tickTime=2000 |
zk3
tickTime=2000 |
启动
zk1
echo 1 > data/myid
.../zookeeper-3.4.6_server-01$ bin/zkServer.sh start |
日志在./zookeeper.out
文件中。
|
zk2
echo 2 > data/myid |
zk3
echo 3 > data/myid |
检查 status
zk1
.../zookeeper-3.4.6_server-01$ bin/zkServer.sh status |
zk2
.../zookeeper-3.4.6_server-02$ bin/zkServer.sh status |
zk3
.../zookeeper-3.4.6_server-03$ bin/zkServer.sh status |
可知, zk2 被选为 leader。
宕机测试
把 zk2 kill掉后, 查看各个 zk server 的 status, 发现 zk3 变成 master :
zookeeper-3.4.6_server-03$ bin/zkServer.sh status |
用java客户端api连接三个服务器, 然后用zkCli.sh测试, java client 工作正常:
[zk: 127.0.0.1:2182(CONNECTED) 3] set /test niko2 |
然后关掉master zk3后, 集群将停止服务, 因为选举不能获得不能超过3/2=1
张票了, 无法得出leader :
<2015-12-29 22:42:57,889> <ClientCnxn:INFO> main-SendThread(127.0.0.1:2181) Unable to read additional data from server sessionid 0x3585e1413050000, likely server has closed socket, closing socket connection and attempting reconnect |
我们重新启动zk3, 集群将恢复服务:
<2015-12-29 22:53:34,232> <ClientCnxn:INFO> main-SendThread(127.0.0.1:2183) Opening socket connection to server 127.0.0.1/127.0.0.1:2183. Will not attempt to authenticate using SASL (unknown error) |
接下来将会了解基于docker搭建zookeeper集群
及其zookeeper集群的选举算法
, 另起博客介绍.
参考
https://zookeeper.apache.org/doc/trunk/zookeeperStarted.html#sc_RunningReplicatedZooKeeper