Zookeeper 单机&集群搭建


Zookeeper 单机搭建

Zookeeper 下载

进入到 https://zookeeper.apache.org/releases.html#download
选择对应的版本:
image.png
点击进入下载页面
image.png

Zookeeper 安装

采用 3.4.14 版本

解压

  1. tar -zxvf zookeeper-3.4.14.tar.gz

复制配置文件,进行修改

  1. cp zoo_sample.cfg zoo.cfg
  2. vim zoo.cfg
  3. # 修改如下的配置
  4. tickTime=2000
  5. initLimit=10
  6. syncLimit=5
  7. dataDir=/your zookeeper dir/zookeeper-3.4.14/data
  8. clientPort=2181

Zookeeper 配置文件详解:

  • tickTime
    • 通讯心跳数,Zookeeper服务器心跳时间,单位毫秒
    • Zookeeper使用的基本时间,服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个tickTime时间就会发送一个心跳,时间单位为毫秒。
    • 它用于心跳机制,并且设置最小的session超时时间为两倍心跳时间(session的最小超时时间是2*tickTime)
  • initLimit
    • LF初始通信时限
    • 集群中的follower跟随者服务器(F)与leader领导者服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量),用它来限定集群中的Zookeeper服务器连接到Leader的时限。
    • 投票选举新leader的初始时间
    • Follwer在启动过程中,会从Leader同步所有最新数据,然后确定自己能够对外服务的起始状态。
    • Leader允许F在initLimit时间内完成这个工作
  • syncLimit
    • syncLimit:LF同步通讯实现
    • 集群中Leader与Follower之间的最大响应时间单位,假如响应时间超过syncLimit * tickTime,
    • Leader认为Follower死掉,从服务器列表中删除Follower。
    • 在运行过程中,leader负责ZK集群中所有机器进行通讯,例如通过一些心跳检测j机制,来检测机器的存活状态。

    • 如果L发出心跳包在syncLimit之后,还没有从F那收到响应,那么就认为这个F已经不在线了。
  • dataDir
    • 存放ZK数据的目录
  • clientPort
    • ZK的监听端口

      启动Zookeeper

      1. bin/zkServer.sh start conf/zoo.cfg

      连接Server

      ```shell ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ bin/zkCli.sh Connecting to localhost:2181 2020-08-12 11:00:42,394 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.14-4c25d480e66aadd371de8bd2fd8da255ac140bcf, built on 03/06/2019 16:18 GMT 2020-08-12 11:00:42,397 [myid:] - INFO [main:Environment@100] - Client environment:host.name=ifan-PC 2020-08-12 11:00:42,398 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_201 2020-08-12 11:00:42,399 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation 2020-08-12 11:00:42,400 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/home/ifan/software/java/jdk1.8/jre 2020-08-12 11:00:42,400 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/home/ifan/software/java/zookeeper-3.4.14/bin/../zookeeper-server/target/classes:/home/ifan/software/java/zookeeper-3.4.14/bin/../build/classes:/home/ifan/software/java/zookeeper-3.4.14/bin/../zookeeper-server/target/lib/.jar:/home/ifan/software/java/zookeeper-3.4.14/bin/../build/lib/.jar:/home/ifan/software/java/zookeeper-3.4.14/bin/../lib/slf4j-log4j12-1.7.25.jar:/home/ifan/software/java/zookeeper-3.4.14/bin/../lib/slf4j-api-1.7.25.jar:/home/ifan/software/java/zookeeper-3.4.14/bin/../lib/netty-3.10.6.Final.jar:/home/ifan/software/java/zookeeper-3.4.14/bin/../lib/log4j-1.2.17.jar:/home/ifan/software/java/zookeeper-3.4.14/bin/../lib/jline-0.9.94.jar:/home/ifan/software/java/zookeeper-3.4.14/bin/../lib/audience-annotations-0.5.0.jar:/home/ifan/software/java/zookeeper-3.4.14/bin/../zookeeper-3.4.14.jar:/home/ifan/software/java/zookeeper-3.4.14/bin/../zookeeper-server/src/main/resources/lib/*.jar:/home/ifan/software/java/zookeeper-3.4.14/bin/../conf: 2020-08-12 11:00:42,400 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 2020-08-12 11:00:42,400 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp 2020-08-12 11:00:42,400 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler= 2020-08-12 11:00:42,400 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux 2020-08-12 11:00:42,400 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64 2020-08-12 11:00:42,400 [myid:] - INFO [main:Environment@100] - Client environment:os.version=4.15.0-29deepin-generic 2020-08-12 11:00:42,400 [myid:] - INFO [main:Environment@100] - Client environment:user.name=ifan 2020-08-12 11:00:42,400 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/home/ifan 2020-08-12 11:00:42,400 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/ifan/software/java/zookeeper-3.4.14 2020-08-12 11:00:42,401 [myid:] - INFO [main:ZooKeeper@442] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@446cdf90 Welcome to ZooKeeper! 2020-08-12 11:00:42,420 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1025] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) JLine support is enabled 2020-08-12 11:00:42,468 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@879] - Socket connection established to localhost/127.0.0.1:2181, initiating session 2020-08-12 11:00:42,481 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x100006b4ca30000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 1] create /aaa aaa Created /aaa [zk: localhost:2181(CONNECTED) 2] get /aaa aaa cZxid = 0x1a ctime = Wed Aug 12 11:01:21 CST 2020 mZxid = 0x1a mtime = Wed Aug 12 11:01:21 CST 2020 pZxid = 0x1a cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 0

  1. ---
  2. <a name="hPQ5d"></a>
  3. ## Zookeeper 集群搭建
  4. <a name="D5vHG"></a>
  5. ### 拷贝多份配置文件
  6. ```shell
  7. ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ cp conf/zoo.cfg conf/zoo02.cfg
  8. ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ cp conf/zoo.cfg conf/zoo03.cfg

修改配置文件

  1. # 修改如下的配置
  2. dataDir=/home/ifan/software/java/zookeeper-3.4.14/data02
  3. clientPort=2182
  4. # 添加如下的配置
  5. server.1=127.0.0.1:2888:3888
  6. server.2=127.0.0.1:2889:3889
  7. server.3=127.0.0.1:2890:3890

在各自的data目录下,创建myid文件并写上一个数字

  1. touch myid

修改完成之后,尝试启动集群

  1. ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ bin/zkServer.sh start conf/zoo.cfg
  2. ZooKeeper JMX enabled by default
  3. Using config: conf/zoo.cfg
  4. Starting zookeeper ... STARTED
  5. ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ bin/zkServer.sh start conf/zoo02.cfg
  6. ZooKeeper JMX enabled by default
  7. Using config: conf/zoo02.cfg
  8. Starting zookeeper ... STARTED
  9. ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ bin/zkServer.sh start conf/zoo03.cfg
  10. ZooKeeper JMX enabled by default
  11. Using config: conf/zoo03.cfg
  12. Starting zookeeper ... STARTED

查看集群启动情况

使用zkServer脚本查看集群情况

  1. ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ bin/zkServer.sh status conf/zoo03.cfg
  2. ZooKeeper JMX enabled by default
  3. Using config: conf/zoo03.cfg
  4. Mode: follower
  5. ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ bin/zkServer.sh status conf/zoo02.cfg
  6. ZooKeeper JMX enabled by default
  7. Using config: conf/zoo02.cfg
  8. Mode: follower
  9. ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ bin/zkServer.sh status conf/zoo.cfg
  10. ZooKeeper JMX enabled by default
  11. Using config: conf/zoo.cfg
  12. Mode: leader

使用jps查看进程

  1. ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ jps
  2. 25640 QuorumPeerMain
  3. 25417 QuorumPeerMain
  4. 25498 QuorumPeerMain
  5. 26029 Jps

zkCli测试

开启两个不同的命令行,连接不同的端口
在节点一中进行创建数据,可以看到,节点二中也能看见,即集群搭建成功。
image.png