二.使用
Zookeeper一般是用于Linux系统
1.安装
1-1 解压
下载对应版本的Zookeeper
将其解压到一个非中文无空格的目录中
tar -zxf zookeeper-3.4.13.tar.gz
1-2 配置
在Zookeeper中,默认读取的核心配置文件为:
zoo.cfg
但是,初始情况下不存在该文件
在conf下只有一个叫做
zoo_sample.cfg
的文件因此,我们需要复制该文件,将复制后的文件名字改为
zoo.cfg
即可打开
zoo.cfg
,进行配置,增加数据存放目录的配置
# 进入配置文件所在目录
cd zookeeper-3.4.13/conf
# 复制配置文件
cp zoo_sample.cfg zoo.cfg
# 打开zoo.cfg文件
vi zoo.cfg
# 修改数据目录配置,指定存放的目录处于上级目录下的data目录中
dataDir=../data
# 返回上一级目录,创建data目录
cd ..
mkdir data
1-3 服务器操作
进入对应的bin目录中,执行相关的命令
- 启动服务器
./zkServer.sh start
- 停止服务器
./zkServer.sh stop
- 查看服务器状态
./zkServer.sh status
- 重启服务器
./zkServer.sh restart
cd bin
# 启动 | 停止 | 查看状态 | 重启
./zkServer.sh start | stop | status | restart
1-4 客户端操作
客户端连接时,服务器必须处于启动状态
进入对应的bin目录中,执行相关的命令
- 默认连接
- 使用默认的配置进行连接,zookeeper默认端口2181
./zkCli.sh
- 指定连接
- 指定需要连接的Zookeeper服务器地址
./zkCli.sh -server 服务器ip:端口
- 退出
- quit
- Ctrl+C快捷键
# 方式一: 使用默认的配置进行连接,zookeeper默认端口2181
./zkCli.sh
# 方式二 :指定需要连接的Zookeeper服务器地址
./zkCli.sh -server 服务器ip:端口
2.配置文件
- tickTime
- 默认值2000,单位毫秒
- 客户端与服务器通信的心跳时间
- 客户端与服务器会每隔一段时间进行一次心跳
- 客户端与服务器之间必须维持心跳的的正常
- 如果心跳不正常,说明客户端与服务器之间的连接出现了问题
- 在Zookeeper中,所有与时间相关的配置都是以心跳时间作为基础单元,进行配置
- initLimit
- 初始通信时间,默认值为10,单位心跳数
- 用于Zookeeper集群
- 集群中follower服务器与leader服务器之间的请求和响应最多能够容忍的心跳数
- syncLimie
- 同步通信时间,默认值为5,单位心跳数
- 集群中follower服务器与leader服务器之间进行通信
- 如果超过了指定的心跳数后还未收到响应
- 默认该follower已崩
- dataDir
- 存储数据的目录
- 该目录中可以存放一些与服务器相关的信息
- 例如:集群环境中的myid
- clientPort
- 客户端连接的端口号,默认为2181
- maxClientCnxns
- 单个客户端的最大连接限制,默认60,可不配
- 不配置表示无限制
- autopurge.snapRetainCount
- 保留的文件的数量,默认3个,可不配
- autopurge.purgeInterval
- 自动清理快照文件与事务日志的频率,默认为1,单位小时,可不配置
- 不配置表示不自动清理
# 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=../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