官网:https://zookeeper.apache.org/
zookeeper常用用途:
- 集群管理,zookeeper作为注册中心,管理服务提供方的ip地址端口号url信息,并在服务消费方请求需要时发送给服务消费方。
- 配置中心(不过一般用阿波罗apollo或者阿里的nacos来做)
- 多个app中的配置是从zookeeper中拉取配置,而不是一个个去手动修改。
- 消费端从服务端中关注某个znode,一旦节点发生数据变更,服务端会向消费端发送watcher事件进行通知,消费端接受事件后,主动到服务端获取最新的配置数据。
- 分布式锁
- 等待
安装
前提:安装zookeeper需要的jdk版本,这里装的是openjdk-8
apt install openjdk-8-jdk
复制示例配置文件zoo_simple.conf到confg/zoo.cfg
cp confg/zoo_simle.conf confg/zoo.cf
修改配置文件,修改如下:(主要修改数据的保存目录,其他看自己的需求修改)
# 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=/apps_data/zookeeper**
# the port at which the clients will connect
clientport=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
# 客户端连接的最大数量。
# 如果需要处理更多客户端,请增加此值
#maxclientcnxns=60
# 下面是自动清除相关,根据提示阅读链接使用。
# be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperadmin.html#sc_maintenance
#
# the number of snapshots to retain in datadir
#autopurge.snapretaincount=3
# purge task interval in hours
# set to "0" to disable auto purge feature
#autopurge.purgeinterval=1
通过自带的脚本启动zookeeper
# 查看帮助
root@swq-virtual-machine:/apps/zookeeper-3.4.13# bash bin/zkserver.sh --help
zookeeper jmx enabled by default
using config: /apps/zookeeper-3.4.13/bin/../conf/zoo.cfg
usage: bin/zkserver.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
# 启动服务
root@swq-virtual-machine:/apps/zookeeper-3.4.13# bash bin/zkserver.sh start
zookeeper jmx enabled by default
using config: /apps/zookeeper-3.4.13/bin/../conf/zoo.cfg
starting zookeeper ... started
# 查看2181端口是否监听成功
root@swq-virtual-machine:/apps/zookeeper-3.4.13# ss -tanl
state recv-q send-q local address:port peer address:port process
listen 0 4096 127.0.0.53%lo:53 0.0.0.0:*
listen 0 128 0.0.0.0:22 0.0.0.0:*
listen 0 128 127.0.0.1:631 0.0.0.0:*
listen 0 4096 127.0.0.1:2379 0.0.0.0:*
listen 0 4096 127.0.0.1:2380 0.0.0.0:*
listen 0 50 *:46069 *:*
listen 0 128 [::]:22 [::]:*
listen 0 128 [::1]:631 [::]:*
listen 0 50 *:2181 *:*
客户端
官方命令行客户端zkcli.sh
bash bin/zkcli.sh
[zk: localhost:2181(connected) 0] help
zookeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setacl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getacl path
close
connect host:port
[zk: localhost:2181(connected) 1]
图形化ui客户端-zooinspector
老古董,不过实用!没有啥界面之说…
图形化ui客户端-zkui
界面也挺好看的…不过也挺久没有更新了
https://github.com/deemopen/zkui
图形化ui客户端-prettyzoo
比较新,界面好看
https://github.com/vran-dev/prettyzoo
集群
集群理论
集群角色分类
- leader领导者(就是master主)
- flowller追随者(也就是slave从)
- observer观察者,不参与leader选举,也不参与【过半写成功】策略。只用于读数据,提高读性能。【一般不怎么用】
zookeeper集群中的所有机器通过leader选举来选定⼀台被称为leader的机器,leader服务器为客户端提供读和写服务,除leader外,其他机器包括follower和observer,follower和observer都能提供读服务,唯⼀的区别在于observer不参与leader选举过程,不参与写操作的过半写成功策略,因此observer可以在不影响写性能的情况下提升集群的性能。
集群读写的方式
读:任意节点都可以
写:只有leader节点可以,如果往非leader节点写,会自动转发给leader写入,然后再同步给其他非leader节点
集群搭建
实验环境
# 由于我没啥硬件资源,全部都放在同一台虚拟机上,然后用不同的端口模拟实现集群。
# 生产环境肯定是放在不同的物理机上的。
zk1 -> 192.168.6.130: 2181
zk2 -> 192.168.6.130: 2182
zk3 -> 192.168.6.130: 2183
修改配置文件中的端口和数据存储目录
# 由于是放同一个服务器上用不同的端口实现伪集群,因此这里只需要添加2个额外的配置文件即可。
# zk2 配置文件(zoo2.cfg)
# 这里需要修改,指定存储数据的目录
datadir=/apps_data/zookeeper2
# the port at which the clients will connect
clientport=2182
# zk3 服务(zoo3.cfg)
# 这里需要修改,指定存储数据的目录
datadir=/apps_data/zookeeper3
# the port at which the clients will connect
clientport=2183
停止之前的zk1服务
bash zkserver.sh stop
在每个zk服务器的【数据目录】下添加myid文件,并且内容必须时每个服务器都时唯一的id
# 我这里就用1、2、3来区分了
echo 1 > /apps_data/zookeeper/myid
echo 2 > /apps_data/zookeeper2/myid
echo 3 > /apps_data/zookeeper3/myid
再次编辑每个zk服务的配置文件,将需要组成集群的zk服务器ip端口等信息添加
# zk1、zk2、zk3的配置文件,添加以下内容
# 格式: server.服务器唯一id=服务器ip:数据同步端口:leader选举端口
# 服务器唯一id,是上面步骤中向【数据目录】写入myid文件中内容
server.1=192.168.6.130:2888:3888
server.2=192.168.6.130:2888:3888
server.3=192.168.6.130:2888:3888
# zk2和zk3配置文件添加的内容相同。
# 由于,我是同一个服务器上部署,所以端口就不一致了,生产环境可以直接用上面的配置
# 示例:
root@swq-virtual-machine:/apps/zookeeper-3.4.13# cat <<eof>> ./conf/zoo.cfg
# cluster config
server.1=192.168.6.130:2888:3888
server.2=192.168.6.130:2889:3889
server.3=192.168.6.130:2887:3887
eof
启动3台服务器:
root@swq-virtual-machine:/apps/zookeeper-3.4.13# bash bin/zkserver.sh start conf/zoo.cfg
zookeeper jmx enabled by default
using config: conf/zoo.cfg
starting zookeeper ... ^[[a^[[dstarted
root@swq-virtual-machine:/apps/zookeeper-3.4.13# bash bin/zkserver.sh start conf/zoo2.cfg
zookeeper jmx enabled by default
using config: conf/zoo2.cfg
starting zookeeper ... started
root@swq-virtual-machine:/apps/zookeeper-3.4.13# bash bin/zkserver.sh start conf/zoo3.cfg
zookeeper jmx enabled by default
using config: conf/zoo3.cfg
starting zookeeper ... started
用jps查看zookeeper是否启动(用于查看当前系统上的java虚拟机):
root@swq-virtual-machine:/apps/zookeeper-3.4.13# jps
29360 quorumpeermain
29443 jps
29332 quorumpeermain
29402 quorumpeermain
查看端口:
root@swq-virtual-machine:/apps/zookeeper-3.4.13# ss -tanl
state recv-q send-q local address:port peer address:port process
...省略其他的
listen 0 50 *:2181 *:*
listen 0 50 *:2182 *:*
listen 0 50 *:2183 *:*
listen 0 50 *:37447 *:*
listen 0 50 [::ffff:192.168.6.130]:2888 (被选为leader的服务才会监听【数据同步端口】) *:*
listen 0 50 *:38731 *:*
listen 0 50 [::ffff:192.168.6.130]:3887 *:*
listen 0 50 *:40687 *:*
listen 0 50 **[::ffff:192.168.6.130]:3888 ** *:*
listen 0 50 [::ffff:192.168.6.130]:3889 *:*
注意:(被选为leader的服务才会监听【数据同步端口】,因为其他follwer是通过该端口与leader同步数据的。)
查看zookeeper状态:
root@swq-virtual-machine:/apps/zookeeper-3.4.13# bash bin/zkserver.sh status conf/zoo3.cfg
zookeeper jmx enabled by default
using config: conf/zoo3.cfg
mode: follower
root@swq-virtual-machine:/apps/zookeeper-3.4.13# bash bin/zkserver.sh status conf/zoo2.cfg
zookeeper jmx enabled by default
using config: conf/zoo2.cfg
mode: follower
root@swq-virtual-machine:/apps/zookeeper-3.4.13# bash bin/zkserver.sh status conf/zoo.cfg
zookeeper jmx enabled by default
using config: conf/zoo.cfg
mode: leader
发表评论