深入淺出Zookeeper集羣搭建

部署環境:Centos7.5
在這裏插入圖片描述
zookeeper下載地址:https://zookeeper.apache.org/releases.html#download
選擇3.4.14版本:http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/

在這裏插入圖片描述
開始下載安裝:

cd /usr/local/

wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz

tar -zxvf  zookeeper-3.4.14.tar.gz

cd zookeeper-3.4.14/

在這裏插入圖片描述
接下來我們修改一下配置文件:

cd conf/

cp zoo_sample.cfg zoo.cfg

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.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
# 數據文件夾
dataDir=/usr/local/zookeeper-3.4.14/data
# 日誌文件夾
dataLogDir=/usr/local/zookeeper-3.4.14/logs
# 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=172.20.42.10:2888:3888  
server.2=172.20.41.96:2888:3888
server.3=172.20.42.247:2888:3888

tickTime:這個時間是作爲 Zookeeper 服務器之間或客戶端與服務器之間維持心跳的時間間隔,也就是每個 tickTime 時間就會發送一個心跳。

initLimit:這個配置項是用來配置 Zookeeper 接受客戶端(這裏所說的客戶端不是用戶連接 Zookeeper 服務器的客戶端,而是 Zookeeper 服務器集羣中連接到 Leader 的 Follower 服務器)初始化連接時最長能忍受多少個心跳時間間隔數。當已經超過 10個心跳的時間(也就是 tickTime)長度後 Zookeeper 服務器還沒有收到客戶端的返回信息,那麼表明這個客戶端連接失敗。總的時間長度就是 10*2000=20 秒

syncLimit:這個配置項標識 Leader 與 Follower 之間發送消息,請求和應答時間長度,最長不能超過多少個 tickTime 的時間長度,總的時間長度就是 5*2000=10秒

dataDir:顧名思義就是 Zookeeper 保存數據的目錄,默認情況下,Zookeeper 將寫數據的日誌文件也保存在這個目錄裏。

clientPort:這個端口就是客戶端連接 Zookeeper 服務器的端口,Zookeeper 會監聽這個端口,接受客戶端的訪問請求。

server.A=B:C:D:其中 A 是一個數字,表示這個是第幾號服務器;B 是這個服務器的 ip 地址;C 表示的是這個服務器與集羣中的 Leader 服務器交換信息的端口;D 表示的是萬一集羣中的 Leader 服務器掛了,需要一個端口來重新進行選舉,選出一個新的 Leader,而這個端口就是用來執行選舉時服務器相互通信的端口。如果是僞集羣的配置方式,由於 B 都是一樣,所以不同的 Zookeeper 實例通信端口號不能一樣,所以要給它們分配不同的端口號。

保存退出,去創建數據目錄和日誌目錄

cd /usr/local/zookeeper-3.4.14

mkdir logs
mkdir data

cd data/
vi myid
#根據ip地址,在每個目錄中創建文件myid 文件,寫入當前實例的server id,即1.2.3
1

在這裏插入圖片描述
啓動三個zookeeper實例

cd /usr/local/zookeeper-3.4.14/bin

./zkServer.sh start

./zkServer.sh status 查看狀態

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
至此,我們對zookeeper就算有了一個入門的瞭解,當然zookeeper遠比我們這裏描述的功能多,比如用zookeeper實現集羣管理,分佈式鎖,分佈式隊列,zookeeper集羣leader選舉等等…

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章