ZooKeeper是一個分布式的,開放源碼的分布式應用程序協調服務,是Google的Chubby一個開源的實現,是Hadoop和Hbase的重要組件。它是一個為分布式應用提供一致性服務的軟件,提供的功能包括:配置維護、域名服務、分布式同步、組服務等。由於ZooKeeper的開源特性,后來我們的開發者在分布式鎖的基礎上,摸索了出了其他的使用方法:配置維護、組服務、分布式消息隊列、分布式通知/協調等。
ZooKeeper是可以集群復制的,通過Zab(Zookeeper Atomic Broadcast)協議保持數據一致性,協議主要包括兩個階段:leader selection和Atomic broadcast。集群中有一個leader,通過broadcast更新其余的follower。當leader崩潰或者失去多數follower,則leader會面臨重選。
1. 進入到/usr目錄,wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
[root@Server1 Downloads]# cd /usr [root@Server1 usr]# ls bin etc games include lib lib64 libexec local sbin share src tmp [root@Server1 usr]# wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz --2017-05-24 23:37:12-- http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz Resolving mirror.bit.edu.cn (mirror.bit.edu.cn)... 202.204.80.77, 2001:da8:204:2001:250:56ff:fea1:22 Connecting to mirror.bit.edu.cn (mirror.bit.edu.cn)|202.204.80.77|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 35042811 (33M) [application/octet-stream] Saving to: ‘zookeeper-3.4.10.tar.gz’ 31% [====================> ] 11,160,899 494KB/s eta 42s
2.解壓縮下載的文件,並重命名為zookeeper文件夾
[root@Server1 usr]# tar -xvf zookeeper-3.4.10.tar.gz [root@Server1 usr]# mv zookeeper-3.4.10 zookeeper
3.修改環境變量,vi /etc/profile,在文件尾部追加如下
export ZOOKEEPER_HOME=/usr/zookeeper
export PATH=$PATH:$ZOOKEEPER_HOME/bin
4.創建zookeeper配置文件
[root@Server1 zookeeper]# cd conf/ [root@Server1 conf]# ls configuration.xsl log4j.properties zoo_sample.cfg [root@Server1 conf]# cp zoo_sample.cfg zoo.cfg
5.配置zookeeper的數據目錄和日志目錄,把這兩個目錄存放在/tmp/zookeeper下面
[root@Server1 ~]# cd /tmp [root@Server1 tmp]# ls hogsuspend systemd-private-3ff79a0765994743931501169ad962cf-cups.service-uQcMzj systemd-private-3ff79a0765994743931501169ad962cf-vmtoolsd.service-aovBEQ [root@Server1 tmp]# mkdir zookeeper [root@Server1 tmp]# cd zookeeper/ [root@Server1 zookeeper]# mkdir log [root@Server1 zookeeper]# mkdir data [root@Server1 zookeeper]# ls data log
6.配置zoo.cfg文件如下
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/tmp/zookeeper/data dataLogDir=/tmp/zookeeper/log # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1
7.啟動zookeeper,進入到/usr/zookeeper/bin目錄
[root@Server1 bin]# pwd /usr/zookeeper/bin [root@Server1 bin]# ls README.txt zkCleanup.sh zkCli.cmd zkCli.sh zkEnv.cmd zkEnv.sh zkServer.cmd zkServer.sh [root@Server1 bin]# ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /usr/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED