美文网首页
【zookeeper学习笔记二】安装搭建

【zookeeper学习笔记二】安装搭建

作者: 三变粥 | 来源:发表于2017-09-07 21:10 被阅读0次

    zookeeper的搭建方式

    zookeeper有三种安装方式:

    1. 单机模式:zookeeper只运行在一台服务器上,适用于测试环境
    2. 伪集群模式:在一台物理机上运行多个zookeeper实例
    3. 集群模式:zookeeper运行于一个集群上,适用于生产环境,这个计算机集群被称为一个"集合体"(ensemble)

    单机模式搭建

    下载zookeeper

    官网:http://mirror.bit.edu.cn/apache/zookeeper/

    解压到安装目录

    更改配置文件

    在conf目录下,新建zoo.cfg

    tickTime=2000
    dataDir=/usr/local/zk/data
    dataLogDir=/usr/local/zk/dataLog
    clientPort=2181
    

    配置环境变量

    vi .bash_profile
    

    添加后面两行的内容

    export M2_HOME=/Users/sanbian/Documents/soft/apache-maven-3.3.9/
    export PATH=$PATH:$M2_HOME/bin
    
    export ZOOKEEPER_HOME=/usr/local/zk
    export PATH=$PATH:$ZOOKEEPER_HOME/bin
    

    启动zookeeper

    在zookeeper的bin目录下,管理员启动

    sudo sh zkServer.sh start
    

    响应的,关闭:

    sudo sh zkServer.sh stop
    

    伪集群模式搭建

    要在一台机器上的部署3个server,我们使用的是:每个配置文档模拟一台机器,实现单台机器上运行多个zookeeper实例。

    必须保证的是:每个配置文档的端口号、dataDir都不能冲突!!!

    搭建3个server

    按照一下路径建立3个server:

    sanbiandeMacBook-Pro:zookeeper sanbian$ tree -d -L 2
    .
    ├── server1
    │   ├── data
    │   ├── dataLog
    │   └── zk
    ├── server2
    │   ├── data
    │   ├── dataLog
    │   └── zk
    └── server3
        ├── data
        ├── dataLog
        └── zk
    
    12 directories
    

    配置文档 zoo.cfg

    dir相对配置,clientPort也响应配置为2181,2182,2183

    # 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=/Users/sanbian/Documents/soft/zookeeper/server3/data
    
    dataLogDir=/Users/sanbian/Documents/soft/zookeeper/server3/dataLog
    # the port at which the clients will connect
    clientPort=2183
    # 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
    
    server.1=127.0.0.1:8881:7771
    server.2=127.0.0.1:8882:7772
    server.3=127.0.0.1:8883:7773
    

    各个配置项的含义

    tickTime=2000

    zookeeper服务器之间或客户端与服务器之间维持心跳的时间间隔,每隔tickTime时间就会发送一个心跳。

    initLimit

    配置zookeeper接受客户端(zookeeper服务器集群中连接到Leader的Follower服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已超过initLimit*tickTime长度后,zookeeperr服务器若还没有收到客户端的返回信息,则表明客户端连接失败。

    syncLimit

    配置Leader与Follower之间发送消息,请求和应答时间长度,最长不能超过syncLimit*tickTime的时间长度

    dataDir

    zookeeper保存数据的目录,默认情况下,写数据的日志也保存在这个目录

    dataLogDir

    若没有的话,则用dataDir.

    zookeeper的持久化都存储在这个目录里。

    dataLogDir放的是顺序日志(WAL),而dataDir放的是内存数据结构的snapshot,便于快速回复和达到性能最大化。

    clientPort

    zookeeper服务器监听的端口,以接受客户端的访问请求。

    server.A=B:C:D
    1. A:是一个数字,表示这是第几号服务器
    2. B:这个服务器的ip地址。
    3. C:这个服务器与集群中的Leader服务器交换信息的端口
    4. D:万一集群中的Leader服务伪集群器挂了,需要一个端口来重新进行选举新的Leader,此端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于B都是一样的,所以不同zookeeper实例通信端口号不能一样,所以要给它们分配不同的端口号。

    在data下面新建myid文档,对应到server.n

    sanbiandeMacBook-Pro:zookeeper sanbian$ cd server1/data
    sanbiandeMacBook-Pro:data sanbian$ echo "1" myid
    

    启动服务

    sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    sanbiandeMacBook-Pro:zookeeper sanbian$ server2/zk/bin/zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server2/zk/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    sanbiandeMacBook-Pro:zookeeper sanbian$ server1/zk/bin/zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server1/zk/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    

    查看启动状态

    sanbiandeMacBook-Pro:zookeeper sanbian$ server1/zk/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server1/zk/bin/../conf/zoo.cfg
    Mode: follower
    sanbiandeMacBook-Pro:zookeeper sanbian$ server2/zk/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server2/zk/bin/../conf/zoo.cfg
    Mode: follower
    sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
    Mode: leader
    sanbiandeMacBook-Pro:zookeeper sanbian$ 
    

    关闭一个server,看其他server状态

    sanbiandeMacBook-Pro:zookeeper sanbian$ server1/zk/bin/zkServer.sh stop
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server1/zk/bin/../conf/zoo.cfg
    Stopping zookeeper ... STOPPED
    sanbiandeMacBook-Pro:zookeeper sanbian$ server1/zk/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server1/zk/bin/../conf/zoo.cfg
    Error contacting service. It is probably not running.
    sanbiandeMacBook-Pro:zookeeper sanbian$ server2/zk/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server2/zk/bin/../conf/zoo.cfg
    Mode: leader
    sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
    Mode: follower
    

    可以看到,关闭掉Server1后,server2自动成为了leader

    关闭2个server,启动一个server

    sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
    Error contacting service. It is probably not running.
    

    产生异常信息的原因是由于Zookeeper 服务的每个实例都拥有全局配置信息,他们在启动的时候会随时随地的进行Leader选举操作。此时,第一个启动的Zookeeper需要和另外两个 Zookeeper实例进行通信。但是,另外两个Zookeeper实例还没有启动起来,因此就产生了这的异样信息。

    如果将配置文件zoo.cfg里的关联其他server的配置项去掉,再重新启动查看状态

    sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
    Mode: standalone
    

    这样就变成了单机模式(独立模式)。

    所以在启动三个服务时,先启动一个服务也会出现这样的异常,等到第二个启动成功后,便正常了。

    sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
    Error contacting service. It is probably not running.
    sanbiandeMacBook-Pro:zookeeper sanbian$ server2/zk/bin/zkServer.sh start
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server2/zk/bin/../conf/zoo.cfg
    Starting zookeeper ... STARTED
    sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
    Mode: leader
    sanbiandeMacBook-Pro:zookeeper sanbian$ server2/zk/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server2/zk/bin/../conf/zoo.cfg
    Mode: follower
    

    这个时候再关掉一个Server

    sanbiandeMacBook-Pro:zookeeper sanbian$ server3/zk/bin/zkServer.sh stop
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server3/zk/bin/../conf/zoo.cfg
    Stopping zookeeper ... STOPPED
    sanbiandeMacBook-Pro:zookeeper sanbian$ server2/zk/bin/zkServer.sh status
    ZooKeeper JMX enabled by default
    Using config: /Users/sanbian/Documents/soft/zookeeper/server2/zk/bin/../conf/zoo.cfg
    Error contacting service. It is probably not running.
    

    在选举leader时,找不到可以通信的Server,就又异常了。

    如果先关闭掉follower,也是一样的状态。

    遇到的坑

    Error contacting service. It is probably not running.

    网上查了很多办法,都没有解决。后来一句话点醒了我,看日志呀!!!日志在哪里呢???zookeeper.out

    sanbiandeMacBook-Pro:zookeeper sanbian$ vi server2/zk/bin/zookeeper.out 
    
    2017-09-06 16:49:41,767 [myid:] - INFO  [main:QuorumPeerConfig@134] - Reading configuration from: ../conf/zoo3.cfg
    2017-09-06 16:49:41,777 [myid:] - INFO  [main:QuorumPeer$QuorumServer@167] - Resolved hostname: localhost to address: localhost/127.0.0.1
    2017-09-06 16:49:41,778 [myid:] - INFO  [main:QuorumPeer$QuorumServer@167] - Resolved hostname: localhost to address: localhost/127.0.0.1
    2017-09-06 16:49:41,778 [myid:] - INFO  [main:QuorumPeer$QuorumServer@167] - Resolved hostname: localhost to address: localhost/127.0.0.1
    2017-09-06 16:49:41,779 [myid:] - ERROR [main:QuorumPeerMain@85] - Invalid config, exiting abnormally
    org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing ../conf/zoo3.cfg
            at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:154)
            at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:101)
            at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:78)
    Caused by: java.lang.IllegalArgumentException: initLimit is not set
            at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parseProperties(QuorumPeerConfig.java:355)
            at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:150)
            ... 2 more
    Invalid config, exiting abnormally
    ~                                   
    

    很明显了吧。我之前的zoo.cfg是自己写进去的,少了initLimit

    tickTime=2000
    dataDir=/Users/sanbian/Documents/soft/zookeeper/server3/data
    dataLogDir=/Users/sanbian/Documents/soft/zookeeper/server3/dataLog
    clientPort=2183
    server.1=127.0.0.1:8881:7771
    server.2=127.0.0.1:8882:7772
    server.3=127.0.0.1:8883:7773
    

    按照zoo_sample.cfg更改:

    # 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=/tmp/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学习笔记二】安装搭建

          本文链接:https://www.haomeiwen.com/subject/qgrwjxtx.html