Zookeeper基于JDK开发出来的
运行环境至少需要JRE
快速安装JDK:
yum install -y java-1.8.0-openjdk-devel.x86_64
# ZK镜像仓库
https://downloads.apache.org/zookeeper/
# 下载 *.bin.tar.gz包
wget https://downloads.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
# 解压包
tar -zxvf apache-zookeeper-3.7.0-bin.tar.gz
# 将解压出来的目录复制到 /usr/local/下 (可选)
cp -r /root/apache-zookeeper-3.7.0-bin /usr/local/
# 将ZK备份的配置文件复制一份重命名为zoo.cfg
cp /usr/local/apache-zookeeper-3.7.0-bin/conf/zoo_sample.cfg /usr/local/apache-zookeeper-3.7.0-bin/conf/zoo.cfg
# 启动ZK,停止ZK
/usr/local/apache-zookeeper-3.7.0-bin/bin/zkServer.sh start /usr/local/apache-zookeeper-3.7.0-bin/bin/zkServer.sh stop
# 查看ZK状态
/usr/local/apache-zookeeper-3.7.0-bin/bin/zkServer.sh status
# 启动ZK失败,查看日志信息
cat /usr/local/apache-zookeeper-3.7.0-bin/logs/zookeeper-root-server-localhost.localdomain.out
一般情况是8080端口占用,杀死该占用进程就好了
netstat -lntp | grep 8080 kill -9 8080端口的进程号
服务器基本操作命令:
# 启动ZK,停止ZK 重启ZK /usr/local/apache-zookeeper-3.7.0-bin/bin/zkServer.sh start /usr/local/apache-zookeeper-3.7.0-bin/bin/zkServer.sh stop /usr/local/apache-zookeeper-3.7.0-bin/bin/zkServer.sh restart # 查看ZK状态 /usr/local/apache-zookeeper-3.7.0-bin/bin/zkServer.sh status
顺便设置开机自启动:
试了一下,并不会开启。。。
vim /etc/rc.local /usr/local/apache-zookeeper-3.7.0-bin/bin/zkServer.sh start
登陆 与 退出 ZK
# 登陆ZK /usr/local/apache-zookeeper-3.7.0-bin/bin/zkCli.sh -server localhost:2181 # 退出ZK quit
展示根节点信息:
[zk: localhost:2181(CONNECTED) 1] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 2]
ZK节点信息:
[zk: localhost:2181(CONNECTED) 2] ls /zookeeper [config, quota] [zk: localhost:2181(CONNECTED) 3]
创建节点(可连子节点)和数据
[zk: localhost:2181(CONNECTED) 3] create /sample aaa Created /sample [zk: localhost:2181(CONNECTED) 4] create /sample2 Created /sample2 [zk: localhost:2181(CONNECTED) 5]
# 创建临时节点
create -e /tempNode
# 创建持久化顺序节点
create -s /persistSortNode
# 创建临时顺序节点
create -s /tempSortNode
对已经存在的节点设置数据
[zk: localhost:2181(CONNECTED) 5] set /sample bbb [zk: localhost:2181(CONNECTED) 6]
获取节点上的数据,无数据则返回NULL
[zk: localhost:2181(CONNECTED) 7] get /sample bbb
删除节点(该节点包含后代节点则不能删除)
[zk: localhost:2181(CONNECTED) 20] delete /sample [zk: localhost:2181(CONNECTED) 21]
删除节点(包括后代节点一并删除)
[zk: localhost:2181(CONNECTED) 20] deleteall /sample [zk: localhost:2181(CONNECTED) 21]
创建顺序的持久化节点:
会自动在后面补上连号
[zk: localhost:2181(CONNECTED) 9] create -s /sequenNode Created /sequenNode0000000002 [zk: localhost:2181(CONNECTED) 10] create -s /sequenNode Created /sequenNode0000000003 [zk: localhost:2181(CONNECTED) 11] create -s /sequenNode Created /sequenNode0000000004 [zk: localhost:2181(CONNECTED) 12] create -s /sequenNode Created /sequenNode0000000005 [zk: localhost:2181(CONNECTED) 13] create -s /sequenNode Created /sequenNode0000000006 [zk: localhost:2181(CONNECTED) 14] create -s /sequenNode Created /sequenNode0000000007 [zk: localhost:2181(CONNECTED) 15] create -s /sequenNode Created /sequenNode0000000008 [zk: localhost:2181(CONNECTED) 16] create -s /sequenNode Created /sequenNode0000000009
展示节点的详细信息:
[zk: localhost:2181(CONNECTED) 22] ls -s / [sample2, sequenNode0000000002, sequenNode0000000003, sequenNode0000000004, sequenNode0000000005, sequenNode0000000006, sequenNode0000000007, sequenNode0000000008, sequenNode0000000009, zookeeper] cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0xe cversion = 10 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 0 numChildren = 10 [zk: localhost:2181(CONNECTED) 23]