Zookeeper-快速入门、服务搭建、集群搭建教程

发布时间 2023-12-22 15:29:23作者: 蕝戀

官网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