ZooKeeper是Apache的一个开源项目,在分布式服务中应用比较广泛。它主要用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步、集群管理、配置文件管理、同步锁、队列等。这里主要讲集群中ZooKeeper的部署。
1、准备工作
我们准备3台机器做ZooKeeper集群,分别在3台机器上创建ZooKeeper需要的目录。
数据存储目录:
mkdir -p /opt/data/zookeeper
日志目录:
mkdir -p /opt/logs/zookeeper
ZooKeeper 安装包下载:
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
JDK的安装见:Linux环境下安装JDK
2、ZooKeeper部署
首先解压:
tar -zxvf zookeeper-3.4.6.tar.gz
修改zoo.cfg文件的集群配置信息:
cd zookeeper-3.4.6/conf/
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
zoo.cfg配置如下:
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=1 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=2 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/opt/data/zookeeper # 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=4000 # The number of snapshots to retain in dataDir autopurge.snapRetainCount=100 # Purge task interval in hours # Set to "0" to disable auto purge feature autopurge.purgeInterval=24 #Service server.1=10.100.152.1:1888:2888 server.2=10.100.152.2:1888:2888 server.3=10.100.152.3:1888:2888
在log4j.properties中找到zookeeper.log.dir修改日志的存储目录:
vim log4j.properties
zookeeper.root.logger=INFO, CONSOLE zookeeper.console.threshold=INFO zookeeper.log.dir=/opt/logs/zookeeper zookeeper.log.file=zookeeper.log zookeeper.log.threshold=DEBUG zookeeper.tracelog.dir=. zookeeper.tracelog.file=zookeeper_trace.log
配置myid本机server服务的标示:
vim /opt/data/zookeeper/myid
输入1后保存退出,到此集群中的第一台机器就配置好了。
下面接着配置集群中的第二台和每三台机器:
首先通过scp把zookeeper目录拷贝到其它两台机器上,操作如下:
scp -rp zookeeper-3.4.6 [email protected]:/opt/app/
scp -rp zookeeper-3.4.6 [email protected]:/opt/app/
然后到10.100.152.2机器上配置myid本机server服务的标示:
vim /opt/data/zookeeper/myid
输入2后保存退出,到此集群中的第二台机器就配置好了。
再到10.100.152.3机器上配置myid本机server服务的标示:
vim /opt/data/zookeeper/myid
输入3后保存退出,到此集群中的第三台机器就配置好了。
3、启动ZooKeeper
启动:./zkServer.sh start
停止:./zkServer.sh stop
到此ZooKeeper的整个集群就部署完成。
注意:如果机器上有防火墙的话,记得配置防火墙端口。