人,总免不了有心结,限制着自己,难以前行。对于ZooKeeper的理解,以及实践也拖了很久,今天用零散时间学习一下,补点干货。
???
一、简述
Zookeeper是Google的Chubby一个开源的实现,是高有效和可靠的协同工作系统,Zookeeper能够用来选举Leader(基于Fast Paxos
协议),配置信息维护等,在一个分布式的环境中,需要一个Master实例或存储一些配置信息,确保文件写入的一致性等。
ZooKeeper是一个分布式,开放源码的分布式应用程序协调服务,包含一个简单的原语集,是Hadoop和Hbase的重要组件,提供Java和C的接口。
ZooKeeper代码
版本中,提供了分布式独享锁、选举、
队列的接口。其中分布锁和队列有Java和C两个版本,选举只有Java版本。
二、配置服务
我直接在ubuntu服务器上开搞了,跳过下载、安装等基本操作,直奔主题。
ZooKeeper建议使用hostname,而非ip。这需要对主机的
/etc/hostname和
/etc/hosts做host绑定。
ZooKeeper以Fast Paxos算法为基础,用于完成leader的选举工作,所以至少需要3个节点。
为此,我准备了以下3个节点:
192.168.158.135 ubuntu-zookeeper-01
192.168.158.151 ubuntu-zookeeper-02
192.168.158.152 ubuntu-zookeeper-03
? ? ? 首先,为各个主机配置自己的hostname:
?
class="shell">sudo vim /etc/hostname
? ? ?例如,在192.168.158.135机器上配置/etc/hostname为:
ubuntu-zookeeper-01
? ? ?然后,为各个主机配置host绑定:
sudo vim /etc/hosts
? ? 例如:
? ??
? ? 在各主机上做好host绑定。
达到如下效果,说明配置ok
zlex@ubuntu-zookeeper-01:~$ ping ubuntu-zookeeper-02
PING ubuntu-zookeeper-02 (192.168.158.151) 56(84) bytes of data.
64 bytes from ubuntu-zookeeper-02 (192.168.158.151): icmp_seq=1 ttl=64 time=5.64 ms
64 bytes from ubuntu-zookeeper-02 (192.168.158.151): icmp_seq=2 ttl=64 time=0.643 ms
64 bytes from ubuntu-zookeeper-02 (192.168.158.151): icmp_seq=3 ttl=64 time=0.821 ms
64 bytes from ubuntu-zookeeper-02 (192.168.158.151): icmp_seq=4 ttl=64 time=0.873 ms
^C
--- ubuntu-zookeeper-02 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3006ms
rtt min/avg/max/mdev = 0.643/1.994/5.641/2.107 ms
?
? ? 接下来配置zookeeper的配置,让它快速跑起来。
? ? 把刚才准备好的hostname配置在这个文件中:
? ??
sudo vim /etc/zookeeper/conf/zoo.cfg
? ? ?
? ?
?
? ? 接着,使server.1~server.3 与各主机的?/etc/zookeeper/conf/myid 相对应:
sudo vim /etc/zookeeper/conf/myid
? ? 例如,ubuntu-zookeeper-01与server.1相对应,其myid的值就是1
$ cat /etc/zookeeper/conf/myid
1
?
? ? 现在,我们就可以启动服务了:
sudo /usr/share/zookeeper/bin/zkServer.sh start
? ? 具体可用的命令有:
{start|start-foreground|stop|restart|status|upgrade|print-cmd}
? ? 最后,观察启动日志:? ??
tail -f /var/log/zookeeper/zookeeper.log
? ? 例如:
zlex@ubuntu-zookeeper-03:~$ tail -f /var/log/zookeeper/zookeeper.log
2014-11-13 22:22:45,970 - INFO [QuorumPeer[myid=3]/0:0:0:0:0:0:0:0:2181:Leader@345] - LEADING - LEADER ELECTION TOOK - 299
2014-11-13 22:22:45,975 - INFO [QuorumPeer[myid=3]/0:0:0:0:0:0:0:0:2181:FileSnap@83] - Reading snapshot /var/lib/zookeeper/version-2/snapshot.200000000
2014-11-13 22:22:45,979 - INFO [QuorumPeer[myid=3]/0:0:0:0:0:0:0:0:2181:FileTxnSnapLog@240] - Snapshotting: 0x200000000 to /var/lib/zookeeper/version-2/snapshot.200000000
2014-11-13 22:22:46,966 - INFO [LearnerHandler-/192.168.158.151:48245:LearnerHandler@263] - Follower sid: 2 : info : org.apache.zookeeper.server.quorum.QuorumPeer$QuorumServer@19e0994
2014-11-13 22:22:46,975 - INFO [LearnerHandler-/192.168.158.151:48245:LearnerHandler@318] - Synchronizing with Follower sid: 2 maxCommittedLog=0x0 minCommittedLog=0x0 peerLastZxid=0x100000000
2014-11-13 22:22:46,975 - INFO [LearnerHandler-/192.168.158.151:48245:LearnerHandler@395] - Sending SNAP
2014-11-13 22:22:46,976 - INFO [LearnerHandler-/192.168.158.151:48245:LearnerHandler@419] - Sending snapshot last zxid of peer is 0x100000000 zxid of leader is 0x300000000sent zxid of db as 0x200000000
2014-11-13 22:22:47,011 - WARN [LearnerHandler-/192.168.158.151:48245:Leader@574] - Commiting zxid 0x300000000 from /192.168.158.152:2888 not first!
2014-11-13 22:22:47,011 - WARN [LearnerHandler-/192.168.158.151:48245:Leader@576] - First is 0x0
2014-11-13 22:22:47,012 - INFO [LearnerHandler-/192.168.158.151:48245:Leader@598] - Have quorum of supporters; starting up and setting last processed zxid: 0x300000000
?
? ? 服务都启动了~
? ? 暂时就到这里,接下来补充服务验证。
- 大小: 6.5 KB
- 大小: 17.4 KB