Preview
Skip to content
CodingDiary
返回

dubbo前置知识:zookeeper集群搭建

编辑页面
导读

学 Dubbo 之前先把注册中心搭好。本文手把手教你在 3 台 Linux 虚拟机上搭建 Zookeeper 集群:从下载解压到配置 zoo.cfg,从创建 myid 文件到配置环境变量,每一步都有详细命令。最后验证集群状态,一个 leader 两个 follower,高可用注册中心就绑了。Dubbo 入门的前置必修课。

拖了一天一天又一天,终于在时隔3月之后,又迎来了一篇新博客。真的是懒癌晚期了。o(╯□╰)o

dubbo 框架需要将服务统一注册到一个注册中心,才能被远程调用。这里注册中心可以使用 zookeeper 或者 redis

这里我们以 zookeeper 为例。zookeeper 可以单机版也就是 standalone模式运行,也可以使用集群方式运行。为了实现高可用,本例采用集群方式部署,需要保证至少半数以上的机器存活,所以至少为3台机器。话不多说,让我们开始 zookeeper 集群的搭建吧:

  1. 官网下载 zookeeper-3.4.10.tar.gz ,将软件包分别放在三台 Linux 虚拟机的 /software 目录下。

  2. 进入 software 目录下,解压软件包。

    [root@localhost ~]# cd /software
    [root@localhost software]# tar -zxvf zookeeper-3.4.10.tar.gz
    [root@localhost software]# ll
    total 34228
    drwxr-xr-x. 10 1001 1001     4096 Mar 23  2017 zookeeper-3.4.10
    -rw-r--r--.  1 root root 35042811 Nov  1 14:58 zookeeper-3.4.10.tar.gz
  3. 解压后的文件夹名字包含了版本号,我们将其删除。

    [root@localhost software]# mv zookeeper-3.4.10 zookeeper
    [root@localhost software]# ll
    total 34228
    drwxr-xr-x. 10 1001 1001     4096 Mar 23  2017 zookeeper
    -rw-r--r--.  1 root root 35042811 Nov  1 14:58 zookeeper-3.4.10.tar.gz
  4. 进入刚解压的 zookeeper 的目录中

    [root@bogon software]# cd zookeeper/
    [root@bogon zookeeper]# ll
    total 1580
    drwxr-xr-x.  2 1001 1001    4096 Mar 23  2017 bin
    -rw-rw-r--.  1 1001 1001   84725 Mar 23  2017 build.xml
    drwxr-xr-x.  2 1001 1001      74 Mar 23  2017 conf
    drwxr-xr-x. 10 1001 1001    4096 Mar 23  2017 contrib
    drwxr-xr-x.  2 1001 1001    4096 Mar 23  2017 dist-maven
    drwxr-xr-x.  6 1001 1001    4096 Mar 23  2017 docs
    -rw-rw-r--.  1 1001 1001    1709 Mar 23  2017 ivysettings.xml
    -rw-rw-r--.  1 1001 1001    5691 Mar 23  2017 ivy.xml
    drwxr-xr-x.  4 1001 1001    4096 Mar 23  2017 lib
    -rw-rw-r--.  1 1001 1001   11938 Mar 23  2017 LICENSE.txt
    -rw-rw-r--.  1 1001 1001    3132 Mar 23  2017 NOTICE.txt
    -rw-rw-r--.  1 1001 1001    1770 Mar 23  2017 README_packaging.txt
    -rw-rw-r--.  1 1001 1001    1585 Mar 23  2017 README.txt
    drwxr-xr-x.  5 1001 1001      44 Mar 23  2017 recipes
    drwxr-xr-x.  8 1001 1001    4096 Mar 23  2017 src
    -rw-rw-r--.  1 1001 1001 1456729 Mar 23  2017 zookeeper-3.4.10.jar
    -rw-rw-r--.  1 1001 1001     819 Mar 23  2017 zookeeper-3.4.10.jar.asc
    -rw-rw-r--.  1 1001 1001      33 Mar 23  2017 zookeeper-3.4.10.jar.md5
    -rw-rw-r--.  1 1001 1001      41 Mar 23  2017 zookeeper-3.4.10.jar.sha1

    解释一下,几个常见目录里的内容:

    • bin 目录里存放的是一些可执行文件,基本以 *.bat*.sh 结尾的文件。
    • conf 目录里存放的是项目(软件)运行时的一些配置文件。
    • contrib 目录存放的内容官方未提供的一些附加软件。
    • dist-maven 目录存放的是 zookeeper 使用 maven 编译之后的源码和 jar文件
    • docs 目录里存放的是一些文档
    • lib 目录里存放的是依赖库
    • recipes 目录下存放的是使用 zookeeper 实现的分布式锁、高并发、队列的一些方案
    • src 目录里存放的是源码文件
  5. conf 文件夹里对 zookeeper 进行配置,将示例配置文件复制并改名

    [root@bogon software]# cd zookeeper/conf/
    [root@bogon conf]# ll
    total 12
    -rw-rw-r--. 1 1001 1001  535 Mar 23  2017 configuration.xsl
    -rw-rw-r--. 1 1001 1001 2161 Mar 23  2017 log4j.properties
    -rw-rw-r--. 1 1001 1001  922 Mar 23  2017 zoo_sample.cfg
    [root@bogon conf]# cp zoo_sample.cfg zoo.cfg
    [root@bogon conf]# ll
    total 16
    -rw-rw-r--. 1 1001 1001  535 Mar 23  2017 configuration.xsl
    -rw-rw-r--. 1 1001 1001 2161 Mar 23  2017 log4j.properties
    -rw-r--r--. 1 root root  922 Nov  2 14:40 zoo.cfg
    -rw-rw-r--. 1 1001 1001  922 Mar 23  2017 zoo_sample.cfg
    [root@bogon conf]# vim zoo.cfg

    然后在 zoo.cfg 里加上一些配置

    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.
    
    ################ 修改的地方1 ################
    # dataDir=/tmp/zookeeper
    # 将原先的 dataDir 注释,并修改成自定义的路径
    dataDir=/software/zookeeper/data
    
    # 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
    
    ################ 修改的地方2 ################
    # 在文件的末尾加上
    server.0=172.16.15.129:2888:3888
    server.1=172.16.15.130:2888:3888
    server.2=172.16.15.131:2888:3888
  6. 新建配置文件中的 dataDir 配置的目录

    [root@bogon conf]# cd ../
    [root@bogon zookeeper]# mkdir data
    [root@bogon zookeeper]# ll
    total 1580
    drwxr-xr-x.  2 1001 1001    4096 Mar 23  2017 bin
    -rw-rw-r--.  1 1001 1001   84725 Mar 23  2017 build.xml
    drwxr-xr-x.  2 1001 1001      88 Nov  2 14:57 conf
    drwxr-xr-x. 10 1001 1001    4096 Mar 23  2017 contrib
    drwxr-xr-x.  2 root root       6 Nov  2 14:58 data
    drwxr-xr-x.  2 1001 1001    4096 Mar 23  2017 dist-maven
    drwxr-xr-x.  6 1001 1001    4096 Mar 23  2017 docs
    -rw-rw-r--.  1 1001 1001    1709 Mar 23  2017 ivysettings.xml
    -rw-rw-r--.  1 1001 1001    5691 Mar 23  2017 ivy.xml
    drwxr-xr-x.  4 1001 1001    4096 Mar 23  2017 lib
    -rw-rw-r--.  1 1001 1001   11938 Mar 23  2017 LICENSE.txt
    -rw-rw-r--.  1 1001 1001    3132 Mar 23  2017 NOTICE.txt
    -rw-rw-r--.  1 1001 1001    1770 Mar 23  2017 README_packaging.txt
    -rw-rw-r--.  1 1001 1001    1585 Mar 23  2017 README.txt
    drwxr-xr-x.  5 1001 1001      44 Mar 23  2017 recipes
    drwxr-xr-x.  8 1001 1001    4096 Mar 23  2017 src
    -rw-rw-r--.  1 1001 1001 1456729 Mar 23  2017 zookeeper-3.4.10.jar
    -rw-rw-r--.  1 1001 1001     819 Mar 23  2017 zookeeper-3.4.10.jar.asc
    -rw-rw-r--.  1 1001 1001      33 Mar 23  2017 zookeeper-3.4.10.jar.md5
    -rw-rw-r--.  1 1001 1001      41 Mar 23  2017 zookeeper-3.4.10.jar.sha1

    data 目录中新建一个 myid 文件,这个文件中的内容存放配置文件中对应 IP 的 serverId

    [root@bogon zookeeper]# cd data
    [root@bogon data]# vim myid
    [root@bogon data]# cat myid
    0
  7. zookeeper 的目录配置在 Linux 系统的环境变量中

    [root@bogon data]# vim /etc/profile

    在文件末尾添加

    export ZOOKEEPER_HOME=/software/zookeeper
    export PATH=$ZOOKEEPER_HOME/bin:$PATH

    然后让环境变量生效

    [root@bogon data]# source /etc/profile
  8. 将另外两台虚拟机也按照 2-7 的步骤配置,注意第6步的 myid 文件里存放的内容,应该与 zoo.cfg 配置文件末尾的 server.n=ip:2888:3888n 对应

  9. 配置完环境变量之后,我们就可以不需要到 bin 目录下,直接在任意目录下执行 zookeeper 中 bin 目录下的文件了。

    • zkServer.sh status :查看 zookeeper 的状态
    • zkServer.sh start :启动 zookeeper
    • zkServer.sh stop :关闭 zookeeper
  10. 分别查看各个虚拟机的启动情况,以及启动后的状态。

虚拟机1:(follower)

[root@bogon software]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/zookeeper/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
[root@bogon software]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /software/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@bogon software]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/zookeeper/bin/../conf/zoo.cfg
Mode: follower

虚拟机2:(leader)

[root@bogon ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/zookeeper/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
[root@bogon ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /software/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@bogon ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/zookeeper/bin/../conf/zoo.cfg
Mode: leader

虚拟机3:(follower)

[root@bogon ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/zookeeper/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
[root@bogon ~]# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /software/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@bogon ~]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/zookeeper/bin/../conf/zoo.cfg
Mode: follower
  1. 至此,zookeeper 集群的简单搭建已经正式结束了。当然还有一些个性化(调优)的配置参数,请自行参照 zookeeper官方文档zoo.cfg 中配置。

编辑页面
分享到:

上一篇
dubbo前置知识:dubbo-admin 的安装
下一篇
mysql启动报错问题