环境要求

software version download
centos 7.2 省略
jdk 1.8 http://www.oracle.com/technet...
zookeeper 3.4.9 https://mirrors.tuna.tsinghua...

下载解压

按照上述提供的地址下载zookeeper后,使用命令tar -zxvf zookeeper-3.4.9.tar.gz进行解压后,可得到下图的文件目录结构

clipboard.png

  • bin目录

zk的可执行脚本目录,包括zk服务进程,zk客户端,等脚本。其中,.sh是Linux环境下的脚本,.cmd是Windows环境下的脚本。

  • conf目录

配置文件目录。zoo_sample.cfg为样例配置文件,需要修改为自己的名称,一般为zoo.cfg。log4j.properties为日志配置文件。

  • contrib目录

一些用于操作zk的工具包。

  • recipes目录

zk某些用法的代码示例

配置说明

复制 zoo_sample.cfg 文件的并命名为为 zoo.cfg

cp 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=/data/ms/zookeeper-3.4.9/data
dataLogDir=/data/ms/zookeeper-3.4.9/logs
 # 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=zk149:2888:3888
server.2=zk150:2888:3888
server.3=zk151:2888:3888

配置项说明

配置项 说明
initLimit ZooKeeper集群模式下包含多个zk进程,其中一个进程为leader,余下的进程为follower。 当follower最初与leader建立连接时,它们之间会传输相当多的数据,尤其是follower的数据落后leader很多。initLimit配置follower与leader之间建立连接后进行同步的最长时间。
syncLimit 配置follower和leader之间发送消息,请求和应答的最大时间长度。
tickTime tickTime则是上述两个超时配置的基本单位,例如对于initLimit,其配置值为5,说明其超时时间为 2000ms * 5 = 10秒。
server.id=host:port1:port2 其中id为一个数字,表示zk进程的id,这个id也是dataDir目录下myid文件的内容。host是该zk进程所在的IP地址,port1表示follower和leader交换消息所使用的端口,port2表示选举leader所使用的端口。
dataDir 其配置的含义跟单机模式下的含义类似,不同的是集群模式下还有一个myid文件。myid文件的内容只有一行,且内容只能为1 - 255之间的数字,这个数字亦即上面介绍server.id中的id,表示zk进程的id。
dataLogDir zookeeper日志存储路径

配置环境变量

vim /etc/profile
并在其尾部追加如下内容:

ZOOKEEPER_HOME=/data/ms/zookeeper-3.4.9
JAVA_HOME=/data/java/jdk1.8.0_111
JRE_HOME=/data/java/jdk1.8.0_111/jre
CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$ZOOKEEPER_HOME/bin:$PATH

刷新环境变量

 source /etc/profile

关闭系统防火墙或配置防火墙规则

  • 直接关闭防火墙

systemctl stop firewalld.service

  • 禁止firewall开机启动

禁用防火墙命令 systemctl disable firewalld.service
停止防火墙 systemctl stop firewalld.service

zookeeper命令

启动 zookeeper 服务

zkServer.sh start
如打印如下信息则表明启动成功:
 ZooKeeper JMX enabled by default
 Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
 Starting zookeeper ... STARTED

查看zookeeper 服务状态

zkServer.sh status

关闭zookeeper服务

zkServer.sh stop
如打印如下信息则表明成功关闭:
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED

重启zookeeper服务

zkServer.sh restart
 如打印如下信息则表明重启成功:
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.9/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

zookeeper自启动服务

新建zookeeper脚本

[root@zookeeper ~]# cd /etc/rc.d/init.d/  
[root@zookeeper init.d]# pwd  
/etc/rc.d/init.d  
[root@zookeeper init.d]# vim zookeeper 

#!/bin/bash  
#chkconfig:2345 20 90  
#description:zookeeper  
#processname:zookeeper  
export JAVA_HOME=/data/java/jdk1.8.0_111
export ZOOKEEPER_HOME=/data/ms/zookeeper-3.4.9
case $1 in
        start) su root $ZOOKEEPER_HOME/bin/zkServer.sh start;;
        stop) su root $ZOOKEEPER_HOME/bin/zkServer.sh stop;;
        status) su root $ZOOKEEPER_HOME/bin/zkServer.sh status;;
        restart) su $ZOOKEEPER_HOME/bin/zkServer.sh restart;;
        *) echo "require start|stop|status|restart" ;;
esac

给zookeeper脚本授可执行权限

chmod +x zookeeper 

添加开机自启动服务

chkconfig --add zookeeper   

查看自启动状态

chkconfig --list
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole         0:off    1:off    2:off    3:off    4:off    5:off    6:off
network            0:off    1:off    2:on    3:on    4:on    5:on    6:off
vmware-tools       0:off    1:off    2:on    3:on    4:on    5:on    6:off
zookeeper          0:off    1:off    2:on    3:on    4:on    5:on    6:off

春夏秋冬过
161 声望13 粉丝

专注于微服务和大数据架构的学习与研究