ZooKeeper简介
官网:https://zookeeper.apache.org/doc/r3.5.9/zookeeperOver.html
ZooKeeper 是分布式应用程序的分布式开源协调服务。
分布式应用程序可以基于这些原语来实现更高级别的同步、配置维护以及组和命名服务。它被设计为易于编程,并使用以熟悉的文件系统目录树结构为样式的数据模型。
众所周知,协调服务很难做好。它们特别容易出现诸如竞争条件和死锁之类的错误。ZooKeeper 背后的动机是减轻分布式应用程序从头开始实现协调服务的责任。
zookeeper集群两种状态,可用状态、不可用状态。不可用状态可以快速选举出leader恢复成可用状态,官方压测200ms左右。  zookeeper是一个目录树结构,node节点可以存1M数据。节点包括:持久节点、临时节点、序列节点(持久、临时)。临时节点依托session,session结束,临时节点立即消失。  
安装、配置、启动
安装
$ wget https://downloads.apache.org/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz
$ tar -xf apache-zookeeper-3.5.9-bin.tar.gz
$ mkdir zk
配置
修改配置文件
# 心跳时间
tickTime=2000
# 初始延迟心跳数(时间 = tickTime * initLimit)
initLimit=10
# 请求得到响应的允许心跳数(时间 = tickTime * syncLimit)
syncLimit=5
# 存放快照的目录
dataDir=/home/zk
# 客户端连接zk的端口号
clientPort=2181
# 最大连接数
# maxClientCnxns=60
# 集群配置
# 服务器编号1、2、3、4 是用来选leader的
# 3888 在第一次启动或者leader挂掉后,用这个端口号建立socket连接进行通信,选举leader
# 2888 在有leader后,其它follower通过这个端口号建立socket连接,和leader进行通信,后续创建节点等操作是通过这个端口去通信的
server.1=192.168.174.62:2888:3888
server.2=192.168.174.63:2888:3888
server.3=192.168.174.64:2888:3888
server.4=192.168.174.65:2888:3888
配置文件远程拷贝到其它三个节点
$ scp zoo.cfg root@192.168.174.63:`pwd`
   
创建myid
$ cd /home/zk
$ vim myid

myid所在目录远程拷贝到其它三个节点
$ scp -r ./zk/ root@192.168.174.65:`pwd`

修改其它3个节点的myid
  
修改环境变量
$ vim /etc/profile
ZOOKEEPER_HOME=/home/apache-zookeeper-3.5.9-bin
JAVA_HOME=/home/jdk1.8.0_291
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
PATH=$PATH:$ZOOKEEPER_HOME/bin:$JAVA_HOME/bin
export ZOOKEEPER_HOME JAVA_HOME CLASS_PATH PATH
$ source /etc/profile
开放端口
$ firewall-cmd --zone=public --query-port=2181/tcp
$ firewall-cmd --zone=public --query-port=2888/tcp
$ firewall-cmd --zone=public --query-port=3888/tcp
$ firewall-cmd --zone=public --add-port=2181/tcp --permanent
$ firewall-cmd --zone=public --add-port=2888/tcp --permanent
$ firewall-cmd --zone=public --add-port=3888/tcp --permanent
$ firewall-cmd --reload
启动

$ zkServer.sh start-foreground
$ zkServer.sh start
$ zkServer.sh status
 
客户端命令
客户端连接ZooKeeper
$ zkCli.sh
$ zkServer.sh -server 192.168.174.62:2181
帮助命令
> help
ZooKeeper -server host:port cmd args
addauth scheme auth
close
config [-c] [-w] [-s]
connect host:port
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
delete [-v version] path
deleteall path
delquota [-n|-b] path
get [-s] [-w] path
getAcl [-s] path
history
listquota path
ls [-s] [-w] [-R] path
ls2 path [watch]
printwatches on|off
quit
reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
redo cmdno
removewatches path [-c|-d|-a] [-l]
rmr path
set [-s] [-v version] path data
setAcl [-s] [-v version] [-R] path acl
setquota -n|-b val path
stat [-w] path
sync path
关闭连接
> close

创建连接
> connect 192.168.174.62:2181

退出客户端
> quit

查看历史命令
> history

查询节点
> ls /
> ls -w /
> ls -s /
> ls -R /
> ls /zookeeper/config
> ls -w /zookeeper/config
> ls -s /zookeeper/config
> ls -R /zookeeper/config

查询节点详情
> ls -s /
> stat /
> stat -w /

查询节点值
> get /zookeeper/config
> get -w /zookeeper/config
> get -s /zookeeper/config
 节点信息解释
[zk: localhost:2181(CONNECTED) 49] get -s /test1
hello test1 u v5 to v6
cZxid = 0x200000053
ctime = Wed Jul 28 10:30:09 CST 2021
mZxid = 0x500000012
mtime = Wed Jul 28 11:34:52 CST 2021
pZxid = 0x200000053
cversion = 0
dataVersion = 6
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 22
numChildren = 0
[zk: localhost:2181(CONNECTED) 50]
创建节点
> create /test
> create /test2 "This is test2 node"
> create /test3 "This is test3 node" world:anyone:cdwra
持久节点 [-c] (默认)
> create -c /test3/hello "this is hello node"
 临时节点 [-e]
> create -e /test3/temp "This is temp node"
 序列化节点 [-s]
> create -s /test3/s
> create -s /test3/
> create -s -e /test3/temp

修改节点值
> set /test1 "hello test1"
> set -v 5 /test1 "hello test1 u v5 to v6"

删除节点
> delete /test3/s0000000013
> delete -v 0 /test3/s0000000014
> deleteall /test3

上一篇《11 Redis开发:spring.data.redis、连接、序列化、high/low api》 下一篇 《13 Zookeeper原理知识,paxos、zab、角色功能、API开发基础》
|