win10環境下搭建zookeeper偽叢集

前言:zookeeper部署分單機部署,單機偽叢集部署,叢集部署。因為資源有限又不想單機部署,於是決定採用單機偽叢集部署。由於zookeeper超過半數伺服器正常即可提供服務的特性,採取部署3個server的偽叢集,下文暫稱server1、server2和server3。

一、下載zookeeper

https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/

這裡筆者下載的是zookeeper-3.3.6

二、配置zookeeper

1.解壓到三個目錄

我們想要在單機上搭建3個server的偽叢集,需要將下載好的zookeeper壓縮包解壓到三個目錄下。筆者的目錄如下:
server1 : F:\paths\zookeeper\server1\zookeeper-3.3.6
server2 : F:\paths\zookeeper\server2\zookeeper-3.3.6
server3 : F:\paths\zookeeper\server3\zookeeper-3.3.6

2.建立配置檔案(cfg檔案)

解壓之後,分別進入conf目錄,可以看到zoo_sample.cfg,log4j.properties和configuration.xsl三個檔案。

在該目錄下建立一個zoo.cfg檔案(也可以直接使用zoo_sample.cfg),配置如下:

# The number of milliseconds of each tick
tickTime=2000(2000ms,心跳間隔,也是zookeeper的時間單位)
# The number of ticks that the initial synchronization phase can take
initLimit=10(10*tickTime = 20s,定義初始化的時間限制)
# The number of ticks that can pass between sending a request and getting an acknowledgement
syncLimit=5 (5*tickTime = 10s,定義同步的時間限制)
# the directory where the snapshot is stored.
dataDir=F:\paths\zookeeper\server1\zookeeper-3.3.6\data (自定義資料快照的存放目錄,data目錄需要自行建立)
dataLogDir=F:\paths\zookeeper\server1\zookeeper-3.3.6\log (自定義日誌存放目錄,log目錄需要自行建立)
# the port at which the clients will connect
clientPort=2181 (zookeeper服務埠)
server.1=localhost:2887:3887 (偽叢集zookeeper的server1標識)
server.2=localhost:2888:3888 (偽叢集zookeeper的server2標識)
server.3=localhost:2889:3889 (偽叢集zookeeper的server3標識)

以上就是zookeeper偽叢集中server1的配置檔案。同理在其他兩個解壓路徑的conf目錄下建立server2和server3的配置檔案zoo.cfg。引數區別僅在於dataDir、dataLogDir和clientPort

server2的zoo.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.
dataDir=F:\paths\zookeeper\server2\zookeeper-3.3.6\data
dataLogDir=F:\paths\zookeeper\server2\zookeeper-3.3.6\log
# the port at which the clients will connect
clientPort=2182
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

server3的zoo.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.
dataDir=F:\paths\zookeeper\server3\zookeeper-3.3.6\data
dataLogDir=F:\paths\zookeeper\server3\zookeeper-3.3.6\log
# the port at which the clients will connect
clientPort=2183
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

3.建立myid檔案

在上個步驟中,我們在dataDir中指定了快照存放目錄,切換到各目錄下,分別建立一個檔名為myid的檔案(沒有字尾名)。檔案內容為一個整型數。

在server1的data目錄下的myid檔案,其內容為1。

在server2的data目錄下的myid檔案,其內容為2。

在server3的data目錄下的myid檔案,其內容為3。

三、啟動zookeeper

分別切換到三個解壓路徑下的bin目錄,在cmd上輸入zkServer.cmd啟動服務,可以同時用三個cmd視窗分別啟動三個server,筆者的啟動順序是server1 -> server2 -> server3。啟動的過程中是會報錯的,資訊如下:

這是zookeeper的Leader選舉演算法的異常資訊,當節點沒有啟動完畢的時候,Leader無法正常進行工作,這種錯誤資訊是可以忽略的,等其他節點啟動之後就正常了。

四、驗證zookeeper服務是否啟動

在cmd中輸入jps,可以檢視到相應的java程序。