1.下载Zookeeper最新稳定版本
apache-zookeeper-3.6.1-bin.tar.gz
解压缩
# tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz
重命名
# mv apache-zookeeper-3.6.1-bin/ zookeeper-3.6.1
进入到conf目录,修改配置文件
将 zoo_sample.cfg 复制一份并命名为 zoo.cfg
# cp zoo_sample.cfg zoo.cfg
在zookeeper目录下创建data目录
修改配置文件中的dataDir如下:
# dataDir=/app/soft/zookeeper-3.6.1/data
2.修改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=/app/soft/zookeeper-3.6.1/data
# the port at which the clients will connect
clientPort=2181
3.zookeeper命令
进入到zookeeper目录下的bin文件夹中,使用以下命令:
启动zookeeper
./zkServer.sh start
检查zookeeper启动情况
./zkServer.sh status
使用客户端连接zookeeper
./zkCli.sh
查看zookeeper的进程
ps -ef | grep zookeeper
4.设置zookeeper开机自启动
可以将zookeeper作为一个服务,设置其开机自启,这样每次我们打开虚拟机就可以开启zookeeper。设置zookeeper开机自启需要以下几个步骤:
1.修改/etc/profile文件,添加ZK配置
# vim /etc/profile
#zookeeper
export ZK_HOME=/app/soft/zookeeper-3.6.1
PATH=$ZK_HOME/bin/:$PATH
export ZK_HOME PATH
# source /etc/profile
2.进入 /etc/init.d
目录,创建文件zookeeper,并添加脚本如下:
vim zookeeper
未配置ZK_PATH环境变量
#!/bin/bash
#chkconfig:2345 20 90
#description:zookeeper
#processname:zookeeper
ZK_PATH=/app/soft/zookeeper-3.6.1
case $1 in
start) sh $ZK_PATH/bin/zkServer.sh start;;
stop) sh $ZK_PATH/bin/zkServer.sh stop;;
status) sh $ZK_PATH/bin/zkServer.sh status;;
restart) sh $ZK_PATH/bin/zkServer.sh restart;;
*) echo "require start|stop|status|restart";;
esac
已配置ZK_PATH环境变量
#!/bin/bash
#chkconfig:2345 20 90
#description:zookeeper
#processname:zookeeper
case $1 in
start) sh zkServer.sh start;;
stop) sh zkServer.sh stop;;
status) sh zkServer.sh status;;
restart) sh zkServer.sh restart;;
*) echo "require start|stop|status|restart";;
esac
为脚本添加权限
# chmod +x zookeeper
3、保存脚本之后,执行以下指令将其注册为服务:
chkconfig --add zookeeper
4.测试其是否生效,这里采用先停服务,再使用命令启动,注意需要修改创建的zookeeper服务权限:
# 启动
[root@qa-11-2 init.d]# service zookeeper start
/bin/java
ZooKeeper JMX enabled by default
Using config: /app/soft/zookeeper-3.6.1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
# 停止
[root@qa-11-2 init.d]# service zookeeper stop
/bin/java
ZooKeeper JMX enabled by default
Using config: /app/soft/zookeeper-3.6.1/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
# 状态查看
[root@qa-11-2 init.d]# service zookeeper status
/bin/java
ZooKeeper JMX enabled by default
Using config: /app/soft/zookeeper-3.6.1/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Error contacting service. It is probably not running.
# 重启
[root@qa-11-2 init.d]# service zookeeper restart
/bin/java
ZooKeeper JMX enabled by default
Using config: /app/soft/zookeeper-3.6.1/bin/../conf/zoo.cfg
/bin/java
ZooKeeper JMX enabled by default
Using config: /app/soft/zookeeper-3.6.1/bin/../conf/zoo.cfg
Stopping zookeeper ... no zookeeper to stop (could not find file /app/soft/zookeeper-3.6.1/data/zookeeper_server.pid)
/bin/java
ZooKeeper JMX enabled by default
Using config: /app/soft/zookeeper-3.6.1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@qa-11-2 init.d]#
5.zookeeper基本操作命令
-
ls /
: 使用 ls 命令来查看当前 ZooKeeper 中所包含的内容 -
ls2 /
: 查看当前节点数据并能看到更新次数等数据 - 创建文件,并设置初始内容:
create /fy "feiyue"
创建一个新的znode节点"fy"以及与它关联的字符串"feiyue" - 获取文件内容:
get /fy
确认新创建的znode节点是否包含我们所创建的"feiyue"字符串 - 修改文件内容:
set /fy "feiyue100"
对 zk 所关联的字符串"feiyue"进行设置,修改为"feiyue100" - 删除文件:
delete /fy
将刚才创建的 znode 删除;rmr /fy
删除节点和他的子节点 - 退出客户端: quit
- 帮助命令: help