下載就不說(shuō)了,官網(wǎng)直接下載對(duì)應(yīng)的版本解壓
這邊演示的是阿里云服務(wù)器,不是虛擬機(jī)
我的目錄結(jié)構(gòu)是這樣的
drwxr-xr-x 2 502 games 4096 4月 28 16:07 bin
drwxr-xr-x 2 502 games 4096 8月 6 18:25 conf
drwxr-xr-x 3 root root 4096 5月 6 15:45 dataDir (新建,存放zk數(shù)據(jù))
drwxr-xr-x 3 root root 4096 4月 22 09:47 dataLogDir (新建,存放數(shù)據(jù)日志)
drwxr-xr-x 5 502 games 4096 2月 10 19:29 docs
drwxr-xr-x 2 root root 4096 4月 21 17:30 lib
-rw-r--r-- 1 502 games 11358 9月 13 2018 LICENSE.txt
drwxr-xr-x 2 root root 4096 4月 22 09:47 logs (新建,存放日志文件)
-rw-r--r-- 1 502 games 432 2月 10 18:00 NOTICE.txt
-rw-r--r-- 1 502 games 1560 2月 7 21:54 README.md
-rw-r--r-- 1 502 games 1347 2月 7 21:54 README_packaging.txt
- 然后幾個(gè)地方需要注意修改的
- 在根目錄下,創(chuàng)建一個(gè)dataLogDir(需要在conf配置中指定位置)目錄用于存放數(shù)據(jù),
創(chuàng)建一個(gè)logs目錄用于存放日志文件
創(chuàng)建dataLogDir 保存數(shù)據(jù)日志
mkdir dataDir
mkdir logs
mkdir dataLogDir
-----------------
drwxr-xr-x 3 root root 4096 5月 6 15:45 dataDir
drwxr-xr-x 3 root root 4096 4月 22 09:47 dataLogDir
drwxr-xr-x 3 root root 4096 4月 22 09:47 logs
- 在dataDir目錄下創(chuàng)建一個(gè)myid文件用于記錄該zookeeper在集群中的id(保證唯一就行)
在其他服務(wù)器一次執(zhí)行同樣的步驟,比如A服務(wù)器myid指定為1,B服務(wù)器myid指定為2....
vim dataDir/myid
## 內(nèi)容
1
#集群模式下配置一個(gè)文件myid奠宜,這個(gè)文件在daraDir目錄下,
#這個(gè)文件里面就有配置的值压真,Zookeeper啟動(dòng)時(shí)讀取此文件
#拿到里面的數(shù)據(jù)與zoo.cfg里面的配置信息比較從而判斷到底是哪個(gè)server。
- 進(jìn)入config目錄---是這樣的
-rw-r--r-- 1 502 games 535 5月 4 2018 configuration.xsl
-rw-r--r-- 1 502 games 2712 2月 7 21:54 log4j.properties
-rw-r--r-- 1 502 games 922 2月 7 21:54 zoo_sample.cfg
- 復(fù)制一份zoo_sample.cfg 官方也是建議復(fù)制一份,不要在事例配置文件修改
# 復(fù)制一份示例配置文件
cp ./zoo_sample.cfg conf/zoo.cfg
# 編輯 zoo.cfg
vim conf/zoo.cfg
- 進(jìn)行編輯,可以先用默認(rèn)配置,在末尾加入以下配置,
在其他服務(wù)器加上同樣的配置,但是myid需要更改保證每臺(tái)服務(wù)器唯一就行,和Ip對(duì)應(yīng)
server后面的數(shù)字唯一標(biāo)識(shí)是哪臺(tái)服務(wù)器,便于尋找服務(wù)
ip對(duì)應(yīng)服務(wù)器ip,后面的數(shù)字簡(jiǎn)單點(diǎn)解釋:2888是集群內(nèi)部通信的端口和3888是選舉端口
server.1=ip:2888:3888
server.2=ip:2888:3888
server.3=ip:2888:3888
下面是conf中的配置
# The number of milliseconds of each tick
#這個(gè)時(shí)間是作為Zookeeper服務(wù)器之間或者客戶端與服務(wù)器之間維持心跳的時(shí)間間隔岳悟,也就是每個(gè)tickTime時(shí)間就會(huì)發(fā)送一下心跳泼差。
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
#這個(gè)配置項(xiàng)是用來(lái)配置zookeeper接受客戶端的(這里所說(shuō)的客戶端不是用戶連接Zookeeper服務(wù)器的
#客戶端而是Zookeeper服務(wù)器集群中連接到Leader的Follower服務(wù)器)初始化連接時(shí)最長(zhǎng)能忍受多少個(gè)心跳時(shí)間間隔數(shù)。
#當(dāng)已經(jīng)超過(guò)10個(gè)心跳的時(shí)間(也就是tickTime)長(zhǎng)度后Zookeeper服務(wù)器還沒有接到客戶端的返回信息
#那么表明這個(gè)客戶端連接失敗滔灶『鸱剩總時(shí)間長(zhǎng)度就是10*2000=20秒。
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
#這個(gè)配置項(xiàng)標(biāo)示Leader與Follower之間發(fā)送消息缀皱、請(qǐng)求和應(yīng)答時(shí)間長(zhǎng)度,
#最長(zhǎng)不能超過(guò)多少個(gè)tickTime的時(shí)間長(zhǎng)度涝影,總的時(shí)間長(zhǎng)度就是5*2000=10秒争占。
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#Zookeeper保存數(shù)據(jù)的目錄序目,默認(rèn)情況下臂痕,Zookeeper將數(shù)據(jù)的日志文件也保存在這里猿涨。
#這個(gè)改成自己知道的目錄
dataDir=/usr/dev-environment/zk/dataDir
#短文本 這個(gè)路徑需要新建的
dataLogDir=/usr/dev-environment/zk/dataLogDir
# the port at which the clients will connect
#這個(gè)端口就是客戶端連接Zookeeper服務(wù)器的端口。Zookeeper會(huì)監(jiān)聽這個(gè)端口澡绩,接受客戶端的訪問請(qǐng)求。
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#autopurge.purgeInterval=1
#四字命令白名單
4lw.commands.whitelist=*
# server.A=ip:2888:3888
#集群需要配置的ip,'xxxxxip'改成真實(shí)的服務(wù)器Ip,后面的
#其中A是一個(gè)數(shù)字肥卡,標(biāo)示這是第幾號(hào)服務(wù)器;
#ip是這個(gè)服務(wù)器的ip地址揪胃;
#2888標(biāo)示的是這個(gè)服務(wù)器與集群中的Leader服務(wù)器交換信息的端口(可以自定義)氛琢;
#3888標(biāo)示的是萬(wàn)一集群中Leader服務(wù)器掛了,需要一個(gè)端口來(lái)進(jìn)行選舉(可以自定義)阳似,
#選出一個(gè)新的Leader,而這個(gè)端口就是用來(lái)執(zhí)行選舉時(shí)服務(wù)器相互通信的端口撮奏。
#如果是偽集群的配置方式,由于B都是一樣藐石,所以不同的Zookeeper實(shí)例通信端口號(hào)不能一樣定拟,所以要給他們分配不同的端口號(hào)。
#我是這樣配置的
server.1=ip:2888:3888
server.2=ip:2888:3888
server.3=ip:2888:3888
#支持外網(wǎng)訪問互通(內(nèi)網(wǎng)打通不用管)
quorumListenOnAllIPs=true
- 進(jìn)入bin目錄分別在不同服務(wù)器啟動(dòng)服務(wù)
[root@cheng bin]# zkServer.sh restart
ZooKeeper JMX enabled by default
Using config: /usr/dev-environment/zk/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /usr/dev-environment/zk/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /usr/dev-environment/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
zkServer.sh start 啟動(dòng)zk服務(wù)
zkServer.sh stop 停止zk服務(wù)
zkServer.sh restart 重啟zk服務(wù)
zkServer.sh status 查看zk服務(wù)狀態(tài)
通過(guò)status查看狀態(tài)可以看到選舉狀態(tài)
[root@VM_0_6_centos bin]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/dev-environment/zk/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower(啟動(dòng)成功,選舉為從節(jié)點(diǎn))
另一臺(tái)服務(wù)器
[root@cheng bin]# zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/dev-environment/zk/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader (啟動(dòng)成功,選舉為主節(jié)點(diǎn))