当前位置: 代码迷 >> 综合 >> Zookeeper--集群搭建(亲测可用)
  详细解决方案

Zookeeper--集群搭建(亲测可用)

热度:58   发布时间:2023-09-18 20:54:36.0

一、单机版

1、下载

首先cd到/usr/locla目录下,然后wegt下载:

   1)root@centos-7# cd /usr/local

   2)wget https://archive.apache.org/dist/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz

2、解压

tar -zxvf zookeeper-3.4.9.tar.gz

3、进入到/zookeeper-3.4.9/conf复制zoo_sample.cfg为zoo.cfg修改

root@centos-7  zookeeper-3.4.9# cd conf

root@centos-7  conf# cp  zoo_sample.cfg  zoo.cfg

root@centos-7  conf# vim  zoo.cfg

Zookeeper--集群搭建(亲测可用)

4、Zookeeper集群中,每一个节点(即每台单机zookeeper服务配置)都需创建data目录、logs目录以及myid文件

说明:myid文件需创建在data目录下,其值为后续zoo.cfg中配置的第几号服务器。笔者在linux机器上的myid文件目录为/usr/local/zookeeper-3.4.9/data/myid,其值为1;在另外2台机器上myid文件的值依次分别为2、3)

Zookeeper--集群搭建(亲测可用)

5、用vim打开/etc/目录下的配置文件profile

vim /etc/profile

然后在其尾部追加如下内容

export ZOOKEEPER_HOME=/usr/local/services/zookeeper/zookeeper-3.4.9/

export PATH=$ZOOKEEPER_HOME/bin:$PATH

export PATH

Zookeeper--集群搭建(亲测可用)

6、使/etc/ 目录下的profile文件即时生效

root@centos-7 # source /etc/profile

7、启动zookeeper

[root@n1 zookeeper-3.4.9]# cd  bin/

root@centos-7 # zkServer.sh start

8、状态查看zkServer.sh status

Zookeeper--集群搭建(亲测可用)

9、停止服务

关闭zookeeper服务:zkServer.sh stop

Zookeeper--集群搭建(亲测可用)

10、重启服务

重启zookeeper服务:zkServer.sh restart

Zookeeper--集群搭建(亲测可用)

二、集群版

搭建集群版前,先看单机版(至少三台服务器或虚拟机)

1、修改hosts配置(可根据自己需要修改): 每台zookeeper的linux都要一样

    x.x.x.1  s1

    x.x.x.2  s2

    x.x.x.3  s3

Zookeeper--集群搭建(亲测可用)

2、修改zoo.cfg

Zookeeper--集群搭建(亲测可用)

3、集群版要先检查zookeeper防火墙是否关闭,然后关闭

#查看防火墙状态 firewall-cmd --state

#关闭防火墙 systemctl stop firewalld.service

Zookeeper--集群搭建(亲测可用)

4、逐个启动,启动完用zkServer.sh status 命令查看,你会发现所有的都成功了

Zookeeper--集群搭建(亲测可用)

Zookeeper--集群搭建(亲测可用)

Zookeeper--集群搭建(亲测可用)

5、可以通过客户端,连接到ZooKeeper集群上。对于客户端来说,ZooKeeper是一个整体(ensemble),你可以在任何一个结点上建立到服务集群的连接,例如:

[root@ms bin]# zkCli.sh -server 192.168.1.139:2181

Connecting to 192.168.1.139:2181

2019-10-16 17:23:13,823 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT

2019-10-16 17:23:13,830 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=<NA>

2019-10-16 17:23:13,830 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_65

2019-10-16 17:23:13,835 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation

2019-10-16 17:23:13,835 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-3.b17.el7.x86_64/jre

2019-10-16 17:23:13,835 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper-3.4.9/bin/../build/classes:/usr/local/zookeeper-3.4.9/bin/../build/lib/*.jar:/usr/local/zookeeper-3.4.9/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper-3.4.9/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper-3.4.9/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper-3.4.9/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper-3.4.9/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper-3.4.9/bin/../zookeeper-3.4.9.jar:/usr/local/zookeeper-3.4.9/bin/../src/java/lib/*.jar:/usr/local/zookeeper-3.4.9/bin/../conf:

2019-10-16 17:23:13,835 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib

2019-10-16 17:23:13,835 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp

2019-10-16 17:23:13,836 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>

2019-10-16 17:23:13,836 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux

2019-10-16 17:23:13,836 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64

2019-10-16 17:23:13,836 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-327.el7.x86_64

2019-10-16 17:23:13,836 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root

2019-10-16 17:23:13,836 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root

2019-10-16 17:23:13,837 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper-3.4.9/bin

2019-10-16 17:23:13,840 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=192.168.1.139:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@7aec35a

Welcome to ZooKeeper!

2019-10-16 17:23:13,961 [myid:] - INFO  [main-SendThread(192.168.1.139:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 192.168.1.139/192.168.1.139:2181. Will not attempt to authenticate using SASL (unknown error)

JLine support is enabled

2019-10-16 17:23:14,222 [myid:] - INFO  [main-SendThread(192.168.1.139:2181):ClientCnxn$SendThread@876] - Socket connection established to 192.168.1.139/192.168.1.139:2181, initiating session

[zk: 192.168.1.139:2181(CONNECTING) 0] 2019-10-16 17:23:14,345 [myid:] - INFO  [main-SendThread(192.168.1.139:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 192.168.1.139/192.168.1.139:2181, sessionid = 0x16dd3c849d20000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

[root@ms bin]# zkCli.sh -server 192.168.1.136:2181

Connecting to 192.168.1.136:2181

2019-10-16 17:25:20,580 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT

2019-10-16 17:25:20,583 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=<NA>

2019-10-16 17:25:20,583 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_65

2019-10-16 17:25:20,586 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation

2019-10-16 17:25:20,586 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-3.b17.el7.x86_64/jre

2019-10-16 17:25:20,586 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper-3.4.9/bin/../build/classes:/usr/local/zookeeper-3.4.9/bin/../build/lib/*.jar:/usr/local/zookeeper-3.4.9/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper-3.4.9/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper-3.4.9/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper-3.4.9/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper-3.4.9/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper-3.4.9/bin/../zookeeper-3.4.9.jar:/usr/local/zookeeper-3.4.9/bin/../src/java/lib/*.jar:/usr/local/zookeeper-3.4.9/bin/../conf:

2019-10-16 17:25:20,586 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib

2019-10-16 17:25:20,586 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp

2019-10-16 17:25:20,586 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>

2019-10-16 17:25:20,586 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux

2019-10-16 17:25:20,586 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64

2019-10-16 17:25:20,586 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-327.el7.x86_64

2019-10-16 17:25:20,586 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root

2019-10-16 17:25:20,586 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root

2019-10-16 17:25:20,587 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper-3.4.9/bin

2019-10-16 17:25:20,588 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=192.168.1.136:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@7aec35a

2019-10-16 17:25:20,617 [myid:] - INFO  [main-SendThread(192.168.1.136:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 192.168.1.136/192.168.1.136:2181. Will not attempt to authenticate using SASL (unknown error)

Welcome to ZooKeeper!

JLine support is enabled

2019-10-16 17:25:20,713 [myid:] - INFO  [main-SendThread(192.168.1.136:2181):ClientCnxn$SendThread@876] - Socket connection established to 192.168.1.136/192.168.1.136:2181, initiating session

[zk: 192.168.1.136:2181(CONNECTING) 0] 2019-10-16 17:25:20,783 [myid:] - INFO  [main-SendThread(192.168.1.136:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 192.168.1.136/192.168.1.136:2181, sessionid = 0x26dd3c849990000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

 

参考文章:

https://blog.csdn.net/cndmss/article/details/80220273

https://blog.csdn.net/u010199866/article/details/81907956

(Dubbo+zk 测试请移步下一篇:https://blog.csdn.net/qq_34395857/article/details/102683910)

  相关解决方案