項(xiàng)目中想實(shí)現(xiàn)zookeeper集群,但是機(jī)器不足,所以選擇在一臺(tái)機(jī)器上部署兩個(gè)zookeeper做偽集群
下載zookeeper
下載地址戳這里,選擇需要的版本,我下載的是zookeeper-3.4.10.tar.gz
安裝zookeeper
準(zhǔn)備在單機(jī)上建幾個(gè)zookeeper服務(wù)器,就需要建幾個(gè)目錄,在每個(gè)目錄中解壓zookeeper壓縮包,我這邊是兩個(gè)
1、將zookeeper壓縮文件拷貝到對(duì)應(yīng)目錄下
cp /mnt/download/zookeeper-3.4.10.tar.gz /usr/local/
2、解壓兩次到當(dāng)前目錄,重命名
tar xzf zookeeper-3.4.10.tar.gz
mv zookeeper-3.4.10 zookeeper1
tar xzf zookeeper-3.4.10.tar.gz
mv zookeeper-3.4.10 zookeeper2
配置zookeeper
1、zookeeper1配置
cd /usr/local/zookeeper1
新建目錄data
mkdir data
新建目錄log
mkdir log
新建文件myid
cd data
vi myid
內(nèi)容就是一個(gè)數(shù)字,比如我目前是zookeeper1,那么就寫(xiě)一個(gè)1
1
新建文件zoo.cfg
cd /usr/local/zookeeper1/conf
vi zoo.cfg
# The number of milliseconds of each tick
#tickTime:CS通信心跳數(shù)
#zookeeper服務(wù)器之間或客戶端與服務(wù)器之間維持心跳的時(shí)間間隔,也就是每個(gè) tickTime 時(shí)間就會(huì)發(fā)送一個(gè)心跳,tickTime以毫秒為單位
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
#initLimit:LF初始通信時(shí)限
#集群中的follower服務(wù)器(F)與leader服務(wù)器(L)之間初始連接時(shí)能容忍的最多心跳數(shù)(tickTime的數(shù)量)
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
#syncLimit:LF同步通信時(shí)限
#集群中的follower服務(wù)器與leader服務(wù)器之間請(qǐng)求和應(yīng)答之間能容忍的最多心跳數(shù)(tickTime的數(shù)量)
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#dataDir:數(shù)據(jù)文件目錄
#zookeeper保存數(shù)據(jù)的目錄,默認(rèn)情況下,zookeeper將寫(xiě)數(shù)據(jù)的日志文件也保存在這個(gè)目錄里
dataDir=/usr/local/zookeeper1/data
#dataLogDir:日志文件目錄
#zookeeper保存日志文件的目錄
dataLogDir=/usr/local/zookeeper1/log
# the port at which the clients will connect
#clientPort:客戶端連接端口
#客戶端連接zookeeper 服務(wù)器的端口,zookeeper 會(huì)監(jiān)聽(tīng)這個(gè)端口,接受客戶端的訪問(wèn)請(qǐng)求
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
最后在文件末尾添加如下內(nèi)容
#服務(wù)器名稱與地址:集群信息(服務(wù)器編號(hào),服務(wù)器地址,LF通信端口,選舉端口)
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
這個(gè)配置項(xiàng)的書(shū)寫(xiě)格式比較特殊,規(guī)則
server.N=YYY:A:B
其中N表示服務(wù)器編號(hào),這個(gè)數(shù)字就是對(duì)應(yīng) data/myid中的數(shù)字,YYY表示服務(wù)器的IP地址,A為L(zhǎng)F通信端口,表示該服務(wù)器與集群中的leader交換的信息的端口。B為選舉端口,表示選舉新leader時(shí)服務(wù)器間相互通信的端口(當(dāng)leader掛掉時(shí),其余服務(wù)器會(huì)相互通信,選擇出新的leader)。一般來(lái)說(shuō),集群中每個(gè)服務(wù)器的A端口都是一樣,每個(gè)服務(wù)器的B端口也是一樣。但是當(dāng)所采用的為偽集群時(shí),IP地址都一樣,只能A端口和B端口不一樣,否則端口沖突
非偽集群的例子
server.1=233.34.9.145:2008:6008
server.2=233.34.9.146:2008:6008
server.3=233.34.9.147:2008:6008
偽集群的例子
server.1=127.0.0.1:2007:6007
server.2=127.0.0.1:2006:6006
server.3=127.0.0.1:2005:6005
2、zookeeper2配置
配置步驟與zookeeper1相同,但是data中的myid文件的內(nèi)容要改為2,zoo.cfg中的clientPort、dataDir和dataLogDir也要相應(yīng)修改
dataDir=/usr/local/zookeeper2/data
dataLogDir=/usr/local/zookeeper2/log
clientPort=2182
啟動(dòng)zookeeper
分別進(jìn)入兩個(gè)服務(wù)器的bin目錄下,啟動(dòng)服務(wù)
./zkServer.sh start
啟動(dòng)成功后可以查看兩個(gè)服務(wù)器的狀態(tài)
./zkServer.sh status
參考1:zookeeper入門(mén)(1)在單機(jī)上實(shí)現(xiàn)ZooKeeper偽機(jī)群/偽集群部署
參考2:zookeeper入門(mén)(2)解讀zookeeper的配置項(xiàng)
參考3:zookeeper單機(jī)偽集群配置