一、软件简介
ZooKeeper实现服务器集群的节点数据的共享、Leader仲裁选举。常用做数据发布与订阅、负载均衡、命名服务(Naming Service)、分布式通知/协调、集群管理与Master选举及分布式锁/队列等。
二、安装脚本
1. 下载zookeeper.tar.gz后,解压下载包
>tar -zxvf zookeeper-3.4.6.tar.gz
2. 配置相关配置文件
>cd zookeeper-3.4.6@b@>cd conf@b@>mv zoo_sample.cfg zoo.cfg@b@>vi zoo.cfg
打开配置文件,参考如下图配置
对于B项集群配置补充说明 -其中N表示服务器编号,YYY表示服务器的IP地址,A为LF通信端口,表示该服务器与集群中的leader交换的信息的端口。B为选举端口,和新选举leader服务器通信的端口(当leader挂掉时,会用B端口和新选举leader进行通信)。集群中每个服务器的A、B端口一般都是一样
注:配置集群模式,需根据不同server.0服务器到dataDir路径下配置对于myid,否则启动不了,具体配置步骤教程参考"解决zookeeper集群模式下无法启动报Error contacting service. It is probably not running..."文章
3. 创建数据及日志存储路径文件夹
>cd /@b@>mkdir data@b@>cd data@b@>mkdir zookeeper@b@>mkdir log
4. 配置环境变量编辑/etc/profile
>vi /etc/profile
>source /etc/profile
5. 启动zookeeper服务(停止>sh zkServer.sh stop)
>sh zkServer.sh start
6.测试服务,具体如下
[root@localhost bin]# telnet localhost 2181@b@Trying 127.0.0.1...@b@Connected to localhost.localdomain (127.0.0.1).@b@Escape character is '^]'.@b@Connection closed by foreign host.
[root@localhost bin]# sh zkServer.sh status@b@JMX enabled by default@b@Using config: /apps/zookeeper-3.4.6/bin/../conf/zoo.cfg@b@Mode: standalone
[root@localhost bin]# sh zkCli.sh -server 127.0.0.1:2181@b@Connecting to 127.0.0.1:2181@b@2016-09-24 17:02:38,607 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT@b@...@b@Welcome to ZooKeeper!@b@JLine support is enabled@b@2016-09-24 17:02:38,843 [myid:] - INFO [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x1575b644d900000, negotiated timeout = 60000@b@@b@WATCHER::@b@@b@WatchedEvent state:SyncConnected type:None path:null@b@[zk: 127.0.0.1:2181(CONNECTED) 0] ls /@b@[zookeeper]@b@[zk: 127.0.0.1:2181(CONNECTED) 1] create /test hello@b@Created /test@b@[zk: 127.0.0.1:2181(CONNECTED) 4] set /test hello@b@cZxid = 0x2@b@ctime = Sat Sep 24 17:03:15 CST 2016@b@mZxid = 0x3@b@mtime = Sat Sep 24 17:04:16 CST 2016@b@pZxid = 0x2@b@cversion = 0@b@dataVersion = 1@b@aclVersion = 0@b@ephemeralOwner = 0x0@b@dataLength = 5@b@numChildren = 0@b@[zk: 127.0.0.1:2181(CONNECTED) 5] get /test@b@hello@b@cZxid = 0x2@b@ctime = Sat Sep 24 17:03:15 CST 2016@b@mZxid = 0x3@b@mtime = Sat Sep 24 17:04:16 CST 2016@b@pZxid = 0x2@b@cversion = 0@b@dataVersion = 1@b@aclVersion = 0@b@ephemeralOwner = 0x0@b@dataLength = 5@b@numChildren = 0@b@[zk: 127.0.0.1:2181(CONNECTED) 6]
7.配置自启动或重启脚本
①在/root/shell目录下创建zookill.sh脚本,内容如下
[root@localhost bin]# mkdir /root/shell@b@[root@localhost bin]# cd /root/shell@b@[root@localhost shell]# touch zookill.sh@b@[root@localhost shell]# vi zookill.sh@b@#!/bin/bash@b@KEY=$1@b@@b@if [ -z $KEY ]@b@then@b@ echo "No process key words specified!"@b@ echo "Usage: $0 process_key_words"@b@ exit 1@b@fi@b@@b@PID='ps -ef|grep $KEY|grep -v $0|grep -v grep|awk '{print $2}''@b@kill -9 $PID@b@@b@ @b@sleep 5@b@sh /usr/local/$1/bin/zkServer.sh start
②将上面,vi下面的部分保存(:wq),然后在/root目录下创建z1_restart.sh脚本,如下
[root@localhost shell]# cd ../@b@[root@localhost root] # touch z1_restart.sh@b@[root@localhost root] # vi z1_restart.sh@b@sh /root/shell/zookill.sh zookeeper-3.4.6
③将上面,vi下面的部分保存(:wq),如后如下执行zookeeper服务重启,如下
[root@localhost bin]# sh ~/z1_restart.sh @b@JMX enabled by default@b@Using config: /usr/local/zookeeper-3.4.6/bin/../conf/zoo.cfg@b@Starting zookeeper ... STARTED@b@[root@localhost bin]# telnet localhost 2181@b@Trying 127.0.0.1...@b@Connected to localhost.localdomain (127.0.0.1).@b@Escape character is '^]'.
8.设置开机启动,具体配置脚本步骤如下
[root@centos6 kafka]# cd /etc/rc.d/init.d/@b@[root@centos6 init.d]# pwd@b@/etc/rc.d/init.d@b@[root@centos6 init.d]# touch zookeeper@b@[root@centos6 init.d]# chmod +x zookeeper @b@[root@centos6 init.d]# vim zookeeper @b@@b@#!/bin/bash@b@#chkconfig:2345 10 90@b@#description:service zookeeper@b@export JAVA_HOME=/home/app/jdk7@b@export ZOO_LOG_DIR=/apps/zookeeper-3.4.6/logs/@b@ZOOKEEPER_HOME=/apps/zookeeper-3.4.6/@b@case "$1" in@b@ start) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh start;;@b@ start-foreground) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh start-foreground;;@b@ stop) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh stop;;@b@ status) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh status;;@b@ restart) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh restart;;@b@ upgrade)su root ${ZOOKEEPER_HOME}/bin/zkServer.sh upgrade;;@b@ print-cmd)su root ${ZOOKEEPER_HOME}/bin/zkServer.sh print-cmd;;@b@ *) echo "requirestart|start-foreground|stop|status|restart|print-cmd";;@b@esac@b@@b@~@b@"zookeeper" 20L, 797C 已写入 @b@[root@centos6 init.d]# chkconfig --add zookeeper @b@[root@centos6 init.d]# chkconfig zookeeper on@b@[root@centos6 init.d]# chkconfig --list zookeeper @b@zookeeper 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭@b@[root@centos6 init.d]# service zookeeper stop@b@JMX enabled by default@b@Using config: /apps/zookeeper-3.4.6/bin/../conf/zoo.cfg@b@Stopping zookeeper ... STOPPED@b@[root@centos6 init.d]# service zookeeper start@b@JMX enabled by default@b@Using config: /apps/zookeeper-3.4.6/bin/../conf/zoo.cfg@b@Starting zookeeper ... STARTED@b@[root@centos6 init.d]#