Contents
  1. 1. Abstract
  2. 2. 安装配置过程
  3. 3. 启动
  4. 4. 测试

Abstract

zookeeper的作用重点在于提供数据交换,抵抗物理宕机引起的服务不可用带来的影响,首先更重要的是代码的质量、健壮性。这里记录一下自己搭建集群的过程

安装配置过程

node-01机器

echo "192.168.1.11 lee1" >> /etc/hosts
echo "192.168.1.12 lee2" >> /etc/hosts
echo "dataDir=/home/lee/zookeeper/node-01/data" >> /usr/local/zookeeper/conf/zoo.cfg
echo "dataLogDir=/home/lee/zookeeper/node-01/log" >> /usr/local/zookeeper/conf/zoo.cfg
echo "server:1=lee1:12281:13381" >> /usr/local/zookeeper/conf/zoo.cfg
echo "server:2=lee2:22281:23381" >> /usr/local/zookeeper/conf/zoo.cfg
# 在dataDir下新建文件myid,内容为1
echo 1 >> /home/lee/zookeeper/node-01/data/myid

node-02机器

echo "192.168.1.11 lee1" >> /etc/hosts
echo "192.168.1.12 lee2" >> /etc/hosts
echo "dataDir=/home/lee/zookeeper/node-02/data" >> /usr/local/zookeeper/conf/zoo.cfg
echo "dataLogDir=/home/lee/zookeeper/node-02/log" >> /usr/local/zookeeper/conf/zoo.cfg
echo "server:1=lee1:12281:13381" >> /usr/local/zookeeper/conf/zoo.cfg
echo "server:2=lee2:22281:23381" >> /usr/local/zookeeper/conf/zoo.cfg
# 在dataDir下新建文件myid,内容为2
echo 2 >> /home/lee/zookeeper/node-01/data/myid

启动

在两台机器启动zookeeper

tail -f zookeeper.out
# 如果输出不正常,显示一直在重试所有端口的话,很可能表明ip类型与监听的ip类型不一致
# 检查监听端口及ip类型 
echo stat|nc localhost 2181
#  0.0.0.0为ipv4  八个0为ipv6
# 如果为ipv6
echo "export _JAVA_OPTIONS=\"-Djava.net.preferIPv4Stack=true\"" >> /etc/profile

测试

要点之一是正确解析到目标机器ip,所以如果通过hosts文件内容解析不出来的话,有必要server:1=后面直接使用内网IP+端口
正常的启动日志zookeeper.out如下

Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
 [myid:] - INFO  [main:QuorumPeerConfig@103] - Reading configuration from: /usr/local/zookeeper/bin/../conf/zoo.cfg
 [myid:] - INFO  [main:QuorumPeer$QuorumServer@149] - Resolved hostname: lvb01 to address: lvb01/192.168.203.131
 [myid:] - INFO  [main:QuorumPeer$QuorumServer@149] - Resolved hostname: 192.168.203.136 to address: /192.168.203.136
# zookeeper集群要求挂机超过一半才判定集群失败,所以尽量设置奇数台机器
 [myid:] - WARN  [main:QuorumPeerConfig@284] - No server failure will be tolerated. You need at least 3 servers.
 [myid:] - INFO  [main:QuorumPeerConfig@331] - Defaulting to majority quorums
 [myid:2] - INFO  [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
 [myid:2] - INFO  [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
 [myid:2] - INFO  [main:DatadirCleanupManager@101] - Purge task is not scheduled.
 [myid:2] - INFO  [main:QuorumPeerMain@127] - Starting quorum peer
 [myid:2] - INFO  [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181
 [myid:2] - INFO  [main:QuorumPeer@1019] - tickTime set to 2000
 [myid:2] - INFO  [main:QuorumPeer@1039] - minSessionTimeout set to -1
 [myid:2] - INFO  [main:QuorumPeer@1050] - maxSessionTimeout set to -1
 [myid:2] - INFO  [main:QuorumPeer@1065] - initLimit set to 10
 [myid:2] - INFO  [main:FileSnap@83] - Reading snapshot /home/app/zookeeper/node-02/data/version-2/snapshot.801
 [myid:2] - INFO  [ListenerThread:QuorumCnxManager$Listener@534] - My election bind port: /192.168.203.136:63381
 [myid:2] - INFO  [QuorumPeer[myid=2]/0.0.0.0:2181:QuorumPeer@774] - LOOKING
 [myid:2] - INFO  [QuorumPeer[myid=2]/0.0.0.0:2181:FastLeaderElection@818] - New election. My id =  2, proposed zxid=0x300000204
 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection@600] - Notification: 1 (message format version), 1 (n.leader), 0x300000204 (n.zxid), 0x19 (n.round), LOOKING (n.state), 1 (n.sid), 0x3 (n.peerEpoch) LOOKING (my state)
 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection@600] - Notification: 1 (message format version), 2 (n.leader), 0x300000204 (n.zxid), 0x1 (n.round), LOOKING (n.state), 2 (n.sid), 0x3 (n.peerEpoch) LOOKING (my state)
 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection@600] - Notification: 1 (message format version), 1 (n.leader), 0x300000204 (n.zxid), 0x19 (n.round), LOOKING (n.state), 1 (n.sid), 0x3 (n.peerEpoch) LOOKING (my state)
 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection@600] - Notification: 1 (message format version), 2 (n.leader), 0x300000204 (n.zxid), 0x19 (n.round), LOOKING (n.state), 2 (n.sid), 0x3 (n.peerEpoch) LOOKING (my state)
 [myid:2] - INFO  [WorkerReceiver[myid=2]:FastLeaderElection@600] - Notification: 1 (message format version), 2 (n.leader), 0x300000204 (n.zxid), 0x19 (n.round), LOOKING (n.state), 1 (n.sid), 0x3 (n.peerEpoch) LOOKING (my state)
 [myid:2] - INFO  [QuorumPeer[myid=2]/0.0.0.0:2181:QuorumPeer@856] - LEADING
 [myid:2] - INFO  [QuorumPeer[myid=2]/0.0.0.0:2181:Leader@59] - TCP NoDelay set to: true
 [myid:2] - INFO  [QuorumPeer[myid=2]/0.0.0.0:2181:Environment@100] - Server environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
 [myid:2] - INFO  [QuorumPeer[myid=2]/0.0.0.0:2181:Environment@100] - Server environment:host.name=localhost
Contents
  1. 1. Abstract
  2. 2. 安装配置过程
  3. 3. 启动
  4. 4. 测试