Zookeeper 单机&集群搭建
Zookeeper 单机搭建
Zookeeper 下载
进入到 https://zookeeper.apache.org/releases.html#download
选择对应的版本:
点击进入下载页面
Zookeeper 安装
解压
tar -zxvf zookeeper-3.4.14.tar.gz
复制配置文件,进行修改
cp zoo_sample.cfg zoo.cfg
vim zoo.cfg
# 修改如下的配置
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/your zookeeper dir/zookeeper-3.4.14/data
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
- dataDir
- 存放ZK数据的目录
- clientPort
- ZK的监听端口
启动Zookeeper
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
- ZK的监听端口
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
---
<a name="hPQ5d"></a>
## Zookeeper 集群搭建
<a name="D5vHG"></a>
### 拷贝多份配置文件
```shell
ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ cp conf/zoo.cfg conf/zoo02.cfg
ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ cp conf/zoo.cfg conf/zoo03.cfg
修改配置文件
# 修改如下的配置
dataDir=/home/ifan/software/java/zookeeper-3.4.14/data02
clientPort=2182
# 添加如下的配置
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
在各自的data目录下,创建myid文件并写上一个数字
touch myid
修改完成之后,尝试启动集群
ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ bin/zkServer.sh start conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: conf/zoo.cfg
Starting zookeeper ... STARTED
ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ bin/zkServer.sh start conf/zoo02.cfg
ZooKeeper JMX enabled by default
Using config: conf/zoo02.cfg
Starting zookeeper ... STARTED
ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ bin/zkServer.sh start conf/zoo03.cfg
ZooKeeper JMX enabled by default
Using config: conf/zoo03.cfg
Starting zookeeper ... STARTED
查看集群启动情况
使用zkServer脚本查看集群情况
ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ bin/zkServer.sh status conf/zoo03.cfg
ZooKeeper JMX enabled by default
Using config: conf/zoo03.cfg
Mode: follower
ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ bin/zkServer.sh status conf/zoo02.cfg
ZooKeeper JMX enabled by default
Using config: conf/zoo02.cfg
Mode: follower
ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ bin/zkServer.sh status conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: conf/zoo.cfg
Mode: leader
使用jps查看进程
ifan@ifan-PC:~/software/java/zookeeper-3.4.14$ jps
25640 QuorumPeerMain
25417 QuorumPeerMain
25498 QuorumPeerMain
26029 Jps
zkCli测试
开启两个不同的命令行,连接不同的端口
在节点一中进行创建数据,可以看到,节点二中也能看见,即集群搭建成功。