软件下载
Zookeeper版本一般是越新越好。注意是带bin的文件
传送门
上传
本人使用的filezilla.exe将文件上传到虚拟机。我上传的地方是/soft文件下
解压缩
tar -zxvf apache-zookeeper-3.5.8.tar.gz
创建文件夹
我是在apache.zookeeper-3.5.8的文件下创建一个文件夹Data。命令可以随意取,这个文件夹是Zookeeper存储数据的目录
mkdir Data
配置
进入到apache.zookeeper-3.5.8/conf
下,将其中的zoo_sample.cfg
修改成zoo.cfg
# 进入到conf的文件夹
cd conf
# 修改文件名
mv zoo_sample.cfg 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=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=/soft/zookeeper3.5.8/Data
# 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
server.1=master:2888:3888
server.2=dataone:2888:3888
server.3=datatwo:2888:3888
修改文件名之后,其他的内容基本上都有,一般没有的是:
server.1=master:2888:3888
server.2=dataone:2888:3888
server.3=datatwo:2888:3888
这部分是根据自己实际情况进行配置,格式如下:
server.Id=host:port1:port2
其中
- id:可以自己取,之后在修改myid文件时会使用到这个id,大小在[1,244]
- host:可以填自己的主机名,要有主机与ip地址的映射
- port1:Leader端口,即该服务器作为Leader时供Follower连接的端口
- port2:选举端口,即选取Leader服务器时提供其他Follower连接的端口
还需要修改的一处是:
dataDir=/tmp/zookeeper/Data
因为我上面创建的是Data文件,所以这个使用的目录就是Data,根据自己创建的实际情况处理
相关配置属性介绍(可以先安装完,再回来看):
- tickTime:基本事件单元,用来指示一个心跳的时长。以毫秒为单位。
- initLimit:集群中的Follower服务器初始化连接leader服务器时能等待的最大心跳树(连接超时时长)。默认为10,即如果经过10个心跳之后,Follower服务器仍没有接收到Leader服务器的返回信息,则连接失败。在我的修改文件中,该参数值是默认值,参数tickTime为2000毫秒,则连接超时时长为5*2000=10秒
- syncLimit:集群中Follower服务器和Leader服务器之间发送消息以及请求/应答时所能等待的最多心跳数。
- server.id=host:port1:port2:标识不同的Zookeeper服务器
- dataDir:Zookeeper存储数据的目录
- clientPort:客户端连接Zookeeper服务器的端口。Zookeeper会监听这个端口,接收客户端的请求。
创建文件myid
在前面创建的文件夹下创建一个myid
的文件。此处,我就是在Data文件夹下创建一个myid
的文件。注意文件名必须一致,在这个文件中填入配置文件中的ID。比如:在我的master服务上,此处填入1.
分发文件
scp -r zookeeper3.5.8/ datatwo:/soft/
修改myid
登录到每个服务器,修改Zookeeper中myid文件。与zoo.cfg
文件中配置一致
启动检验
进入到Zookeeper文件夹中,运行以下命令:
bin/zkServer.sh start
出现下图中提示说明成功了
查看状态
bin/zkServer.sh status
关闭集群
bin/zkServer.sh stop
注意
Zookeeper的集群启动需要每台装有Zookeeper的服务器上都执行一次启动的命令,才能够将整个集群启动起来