Zookeeper的來源
為什么叫動物園管理員呢毅桃,因為他是hadoop(大象)的一個子項目钧舌,用來管理hadoop和hive(蜜蜂)還有pig(豬)等項目的,所以就叫做了動物園管理員了。
搭建集群
- 首先下載zookeeper
下載地址
下載之后找到合適的位置解壓
2.創(chuàng)建zk數(shù)據(jù)文件
在一個合適的位置創(chuàng)建zoo_nodes(名稱隨意)文件夾光绕,然后再里面分別創(chuàng)建node1 node2 node3三個文件夾每個文件夾中海油data和log兩個文件夾
在data文件夾中創(chuàng)建myid文件(注意沒有后綴),文件內(nèi)容分別為1,2,3
- 修改配置文件
找到conf下面的zoo_sample.cfg文件復(fù)制3份畜份,命名為zoo1.cfg zoo2.cfg zoo3.cfg
# The number of milliseconds of each tick
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.
# 注意這里奇钞,在windows中必須要使用轉(zhuǎn)義字符!F怠>鞍!!
dataDir=D:\\zhouyang\\dev\\zoo_nodes\\node1\\data
dataLogDir=D:\\zhouyang\\dev\\zoo_nodes\\node1\\log
# 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
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890
tickTime這個時間是作為zookeeper服務(wù)器之間或客戶端與服務(wù)器之間維持心跳的時間間隔,也就是說每個tickTime時間就會發(fā)送一個心跳顶别。
initLimit這個配置項是用來配置zookeeper接受客戶端(這里所說的客戶端不是用戶連接zookeeper服務(wù)器的客戶端,而是zookeeper服務(wù)器集群中連接到leader的follower 服務(wù)器)初始化連接時最長能忍受多少個心跳時間間隔數(shù)谷徙。
當(dāng)已經(jīng)超過10個心跳的時間(也就是tickTime)長度后 zookeeper 服務(wù)器還沒有收到客戶端的返回信息,那么表明這個客戶端連接失敗⊙币铮總的時間長度就是 10*2000=20秒完慧。
syncLimit這個配置項標(biāo)識leader與follower之間發(fā)送消息,請求和應(yīng)答時間長度,最長不能超過多少個tickTime的時間長度,總的時間長度就是5*2000=10秒。
dataDir顧名思義就是zookeeper保存數(shù)據(jù)的目錄,默認(rèn)情況下zookeeper將寫數(shù)據(jù)的日志文件也保存在這個目錄里剩失;
clientPort這個端口就是客戶端連接Zookeeper服務(wù)器的端口,Zookeeper會監(jiān)聽這個端口接受客戶端的訪問請求屈尼;
server.A=B:C:D中的A是一個數(shù)字,表示這個是第幾號服務(wù)器,B是這個服務(wù)器的IP地址册着,C第一個端口用來集群成員的信息交換,表示這個服務(wù)器與集群中的leader服務(wù)器交換信息的端口,D是在leader掛掉時專門用來進行選舉leader所用的端口脾歧。
- 修改cmd文件并啟動
進入bin目錄甲捏,把zkServer.cmd負(fù)責(zé)3分,命名為zkServer1.cmd zkServer2.cmd zkServer3.cmd
然后分別修改,添加set ZOOCFG=..\conf\zoo1.cfg
setlocal
call "%~dp0zkEnv.cmd"
set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
set ZOOCFG=..\conf\zoo1.cfg
echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*
endlocal
然后分別執(zhí)行zkServer1.cmd zkServer2.cmd zkServer3.cmd
Linux系統(tǒng)下直接在zookeeper根目錄執(zhí)行以下命令就可以
./bin/zkServer.sh start zoo1.cfg
./bin/zkServer.sh start zoo2.cfg
./bin/zkServer.sh start zoo3.cfg
現(xiàn)在zookeeper單機偽集群就建立起來了