单机单节点模式

zookeeper解压, 放到 /opt/zookeeper/下, 新建一个latest的软链

$ ln -s zookeeper-3.4. latest

将 conf/zoo-sample.cfg 重命名为 zoo.cfg, 内容使用默认的就可以

修改 conf/log4j.properties, 这边的日志路径是无效的, 不用动, 注释掉console类型日志, 启用rollingfile类型日志

# Define some default values that can be overridden by system properties
zookeeper.root.logger=INFO, CONSOLE
zookeeper.console.threshold=INFO
zookeeper.log.dir=.
zookeeper.log.file=zookeeper.log
zookeeper.log.threshold=DEBUG
zookeeper.tracelog.dir=.
zookeeper.tracelog.file=zookeeper_trace.log #
# ZooKeeper Logging Configuration
# # Format is "<default threshold> (, <appender>)+ # DEFAULT: console appender only
#log4j.rootLogger=${zookeeper.root.logger} # Example with rolling log file
log4j.rootLogger=DEBUG, ROLLINGFILE # Example with rolling log file and tracing
#log4j.rootLogger=TRACE, CONSOLE, ROLLINGFILE, TRACEFILE

更改日志的存放目录: 和zoo.cfg同目录创建文件 zookeeper-env.sh , 写入

ZOO_LOG_DIR=/opt/zookeeper/server1/log

单机多节点模式

zookeeper解压, 放到 /opt/zookeeper/ 下, 同目录再放一个 server1目录, 下面建data和log两个目录用于存放数据和日志

zoo.cfg

[milton@localhost conf]$ more zoo.cfg
# The number of milliseconds of each tick
tickTime=
# The number of ticks that the initial
# synchronization phase can take
initLimit=
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/opt/zookeeper/server1/data
# the port at which the clients will connect
clientPort=
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=
#
# 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=
# Purge task interval in hours
# Set to "" to disable auto purge feature
#autopurge.purgeInterval=
server.=127.0.0.1::
server.=127.0.0.1::
server.=127.0.0.1::
  • initLimit: zookeeper集群中的包含多台server, 其中一台为leader, 集群中其余的server为follower. initLimit参数配置初始化连接时, follower和leader之间的最长心跳时间. 此时该参数设置为5, 说明时间限制为5倍tickTime, 即5*2000=10000ms=10s.
  • syncLimit: 该参数配置leader和follower之间发送消息, 请求和应答的最大时间长度. 此时该参数设置为2, 说明时间限制为2倍tickTime, 即4000ms.
  • server.X=A:B:C 其中X是一个数字, 表示这是第几号server. A是该server所在的IP地址. B配置该server和集群中的leader交换消息所使用的端口. C配置选举leader时所使用的端口. 由于配置的是伪集群模式, 所以各个server的B, C参数必须不同.

参照zookeeper0/conf/zoo.cfg, 配置zookeeper1/conf/zoo.cfg, 和zookeeper2/conf/zoo.cfg文件. 只需更改dataDir, dataLogDir, clientPort参数即可.

在之前设置的dataDir中新建myid文件, 写入一个数字, 该数字表示这是第几号server. 该数字必须和zoo.cfg文件中的server.X中的X一一对应.
/Users/apple/zookeeper0/data/myid文件中写入0, /Users/apple/zookeeper1/data/myid文件中写入1, /Users/apple/zookeeper2/data/myid文件中写入2.

为了更改日志的存放目录, 和zoo.cfg同目录创建文件 zookeeper-env.sh , 写入

ZOO_LOG_DIR=/opt/zookeeper/server1/log

我用的版本是zookeeper-3.4.6, 如果不起作用, 请查看bin/zkServer.sh 里面具体的指定.

多机多节点集群模式

集群模式下, 各server部署在不同的机器上, 因此各server的conf/zoo.cfg文件可以完全一样.

    tickTime=
initLimit=
syncLimit=
dataDir=/home/zookeeper/data
dataLogDir=/home/zookeeper/logs
clientPort=
server.=10.1.39.43::
server.=10.1.39.47::
server.=10.1.39.48::

示例中部署了3台zookeeper server, 分别部署在10.1.39.43, 10.1.39.47, 10.1.39.48上. 需要注意的是, 各server的dataDir目录下的myid文件中的数字必须不同. 10.1.39.43 server的myid为43, 10.1.39.47 server的myid为47, 10.1.39.48 server的myid为48

zookeeper开启snapshot data自动清理

在版本3.4.0之后, zookeeper增加了这两个参数用于自动清理

 autopurge.snapRetainCount
(No Java system property)
New in 3.4.: When enabled, ZooKeeper auto purge feature retains the autopurge.snapRetainCount most recent snapshots and the corresponding transaction logs in the dataDir and dataLogDir respectively and deletes the rest. Defaults to . Minimum value is .

autopurge.purgeInterval
(No Java system property)
New in 3.4.: The time interval in hours for which the purge task has to be triggered. Set to a positive integer ( and above) to enable the auto purging. Defaults to .
05-28 12:56