ZooKeeper是一个分布式的、开源的分布式应用程序协调服务,可以在分布式环境中实现应用配置管理、统一命名服务、状态同步服务等功能。 ZooKeeper是一种为分布式应用所设计的高可用、高性能的开源协调服务它提供了一项基本服务:分布式锁服务。 由于ZooKeeper开源的特性,在其分布式锁实现的基础上,又被摸索出了其它的功用,譬如:配置维护、组服务、分布式消息队列等等。 ZooKeeper维护了一个类似文件系统的数据结构,其内部每个子目录都被 称作znode(目录节点),与文件系统一样,我们可以自由的增删改查znode。 ZooKeeper集群适合搭建在奇数 台机器上。只要集群中半数以上主机处于存活,那么服务就是可用的。 ZooKeeper在配置文件中并没有指定 master和 slave,但是,ZooKeeper 在工作时,只有一个节点为 leader,其余节点为 follower,leader 是通过内部的选举机制临时产生的。
ZooKeeper特点
ZooKeeper有三种安装方式:单机模式 & 伪集群模式 & 集群模式
下载地址:http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.6.2/
# 解压
tar -zxvf apache-zookeeper-3.6.2.tar.gz
# 进入安装目录
cd apache-zookeeper-3.6.2
cd conf/
# 复制配置文件
cp zoo_sample.cfg zoo.cfg
# 修改配置
# dataDir=/tmp/zookeeper/data
# dataLogDir=/tmp/zookeeper/log
vim zoo.cfg
# 创建tmp目录
mkdir -p /tmp/zookeeper/data
mkdir -p /tmp/zookeeper/log
echo 0 >/tmp/zookeeper/data/myid
cd /bin
./zhServer.sh
启动失败查看安装目录下的 logs 中的日志文件
yi:logs xxx$ cat zookeeper-xxx-server-yi.out
错误: 找不到或无法加载主类 org.apache.zookeeper.server.quorum.QuorumPeerMain
在官网上有两个tar.gz文件,从目前的最新版本3.5.5开始,带有bin名称的包才是我们想要的下载可以直接使用的里面有编译后的二进制的包,而之前的普通的tar.gz的包里面是只是源码的包无法直接使用。
下载带有 bin 的文件,解压后,复制对应的lib
文件
cp -r ../apache-zookeeper-3.6.2-bin/lib/ ./lib
yi:bin xxx$ ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Users/xxx/Ashe/plugin/zookeeper/apache-zookeeper-3.6.2/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
配置文件
# 数据快照目录
dataDir=/tmp/zookeeper/data
# 事务日志目录
dataLogDir=/tmp/zookeeper/log
# 连接端口
clientPort=2181
# 投票选举新 leader 的初始化时间
initLimit=10
# Leader 和 Follower 之间最大响应单位,当超过 syncLimit*tickTime,Leader 认为 Follower 挂掉,从服务器列表中删除 Follower
syncLimit=5
# Zookeeper 服务器心跳时间,单位毫秒
tickTime=2000
查看运行状态
yi:bin xxx$ ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/xxx/Ashe/plugin/zookeeper/apache-zookeeper-3.6.2/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone
yi:bin xxx$
启动客户端
yi:bin xxx$ ./zkCli.sh -server localhost:2181
Connecting to localhost:2181
2021-01-08 13:47:02,336 [myid:] - INFO [main:Environment@98] - Client environment:zookeeper.version=3.6.2--803c7f1a12f85978cb049af5e4ef23bd8b688715, built on 09/04/2020 12:44 GMT
//...
2021-01-08 13:47:02,425 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@999] - Socket connection established, initiating session, client: /127.0.0.1:52814, server: localhost/127.0.0.1:2181
2021-01-08 13:47:02,440 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1433] - Session establishment complete on server localhost/127.0.0.1:2181, session id = 0x10002b9ca150000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
创建节点
# 命令格式
create [-s] [-e] [-c] [-t ttl] path [data] [acl]
# 创建一个 /zk-test 节点,内容是 2021
[zk: localhost:2181(CONNECTED) 1] create /zk-test 2021
Created /zk-test
# 创建子节点
[zk: localhost:2181(CONNECTED) 5] create /zk-test/child-test 01-06
Created /zk-test/child-test
查看节点内容
[zk: localhost:2181(CONNECTED) 2] get /zk-test
2021
查看子节点
[zk: localhost:2181(CONNECTED) 4] ls /
[zk-test, zookeeper]
[zk: localhost:2181(CONNECTED) 7] ls /zk-test
[child-test]
更新节点内容
[zk: localhost:2181(CONNECTED) 9] set /zk-test/child-test 01-07
[zk: localhost:2181(CONNECTED) 10] get /zk-test/child-test
01-07
删除节点
[zk: localhost:2181(CONNECTED) 11] delete /zk-test/child-test
[zk: localhost:2181(CONNECTED) 12] get /zk-test/child-test
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /zk-test/child-test
[zk: localhost:2181(CONNECTED) 13]
准确说来,本次由于懒得开启虚拟机,搭建的是伪集群模式。实际部署的话,使用真实 IP、目录放到对应服务器即可。
# 创建数据快照目录
mkdir -p /tmp/zookeeper/{data_1,data_2,data_3}
# 创建myid文件,并填入ID值
echo 1 > /tmp/zookeeper/data_1/myid
echo 2 > /tmp/zookeeper/data_2/myid
echo 3 > /tmp/zookeeper/data_3/myid
# 创建事务日志目录,官方建立尽量给事务日志作单独的磁盘或挂载点,这会极大的提高zk性能
mkdir -p /tmp/zookeeper/{log_1,log_2,log_3}
节点1
tickTime=2000
initLimit=10
syncLimit=5.
dataDir=/tmp/zookeeper/data_1
dataLogDir=/tmp/zookeeper/log_1
clientPort=2182
server.1=localhost:2288:3288
server.2=localhost:2289:3289
server.3=localhost:2290:3290
节点2
tickTime=2000
initLimit=10
syncLimit=5.
dataDir=/tmp/zookeeper/data_2
dataLogDir=/tmp/zookeeper/log_2
clientPort=2183
server.1=localhost:2288:3288
server.2=localhost:2289:3289
server.3=localhost:2290:3290
节点3
tickTime=2000
initLimit=10
syncLimit=5.
dataDir=/tmp/zookeeper/data_3
dataLogDir=/tmp/zookeeper/log_3
clientPort=2184
server.1=localhost:2288:3288
server.2=localhost:2289:3289
server.3=localhost:2290:3290
yi:conf xxx$ ./../bin/zkServer.sh start zoo_01.cfg
ZooKeeper JMX enabled by default
Using config: /Users/xxx/Ashe/plugin/zookeeper/apache-zookeeper-3.6.2/bin/../conf/zoo_01.cfg
Starting zookeeper ... STARTED
yi:conf xxx$ ./../bin/zkServer.sh start zoo_02.cfg
ZooKeeper JMX enabled by default
Using config: /Users/xxx/Ashe/plugin/zookeeper/apache-zookeeper-3.6.2/bin/../conf/zoo_02.cfg
Starting zookeeper ...
yi:conf xxx$ ./../bin/zkServer.sh start zoo_03.cfg
ZooKeeper JMX enabled by default
Using config: /Users/xxx/Ashe/plugin/zookeeper/apache-zookeeper-3.6.2/bin/../conf/zoo_03.cfg
Starting zookeeper ... STARTED
查看状态:
yi:conf xxx$ jps
401
19138 QuorumPeerMain
17323 QuorumPeerMain
19117 QuorumPeerMain
19229 Jps
19085 QuorumPeerMain
查看各个节点的角色:
yi:conf xxx$ ./../bin/zkServer.sh status zoo_01.cfg
ZooKeeper JMX enabled by default
Using config: /Users/xxx/Ashe/plugin/zookeeper/apache-zookeeper-3.6.2/bin/../conf/zoo_01.cfg
Client port found: 2182. Client address: localhost. Client SSL: false.
Mode: follower
yi:conf xxx$ ./../bin/zkServer.sh status zoo_02.cfg
ZooKeeper JMX enabled by default
Using config: /Users/xxx/Ashe/plugin/zookeeper/apache-zookeeper-3.6.2/bin/../conf/zoo_02.cfg
Client port found: 2183. Client address: localhost. Client SSL: false.
Mode: leader
yi:conf xxx$ ./../bin/zkServer.sh status zoo_03.cfg
ZooKeeper JMX enabled by default
Using config: /Users/xxx/Ashe/plugin/zookeeper/apache-zookeeper-3.6.2/bin/../conf/zoo_03.cfg
Client port found: 2184. Client address: localhost. Client SSL: false.
Mode: follower
linux安装zookeeper及使用
https://www.cnblogs.com/expiator/p/9853378.html)Linux命令大全
https://man.linuxde.net/cp)Zookeeper 集群安装配置,超详细,速度收藏!
https://cloud.tencent.com/developer/article/1458839)ZooKeeper集群环境安装与配置
https://blog.csdn.net/21aspnet/article/details/18990891)zookeeper(单机、伪集群、集群)部署
https://blog.51cto.com/14227204/2484928)Zookeeper框架设计及源码解读七(跟随者观察者消息处理器)
https://donaldhan.github.io/zookeeper/2021/01/07/zookeeper-framwork-design-message-processor-follower.html