首页 > 代码库 > ZooKeeper介绍与环境搭建

ZooKeeper介绍与环境搭建

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

 

ZooKeeper介绍与环境搭建