1. 程式人生 > 其它 >02 | zookeeper 配置和叢集搭建

02 | zookeeper 配置和叢集搭建

zookeeper-3.7.0 配置與部署官方文件

基礎配置說明

Zookeeper 提供了一個簡單的配置檔案示例:zoo_simple.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=/tmp/zookeeper
# 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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

zookeeper 啟動的時候預設會去載入 conf 目錄下的 zoo.cfg 這個配置檔案

zookeeper 可以指定配置檔案啟動,就是啟動命令後面接配置檔案路徑,eg:zkServer.sh start ../conf/zoo_sample.cfg

引數說明

配置項 說明
tickTime 預設值 2000,單位 ms,必要配置
心跳檢測時間間隔
也是 zookeeper 中的基本時間單位,關於時間配置該是該配置的倍數
initLime 預設值 5,代表5次心跳,也就是 5 * 2000 ms
初始通訊時限,叢集中的 follower 節點與 leader 節點之間初始連線時限
syncLimit
預設值 2
同步時限,follower 節點超過該時限未與 leader 通訊,則認為該節點下線
dataDir 預設值 /tmp/zookeeper,必要配置
儲存資料的目錄,預設情況下也是事務日誌的儲存目錄
zookeeper 的資料以類目錄樹的結構儲存,每間隔一段時間生成快照(shapshot)
clientPort 預設值 2181,必要配置
監聽客戶端連線的埠

zookeeper 叢集搭建

zookeeper 有三種搭建模式:

叢集的搭建注意 clientPort

dataDirdataLogDir的配置保持區分

dataLogDir:事務日誌的儲存目錄,預設在 dataDir 配置的目錄下。配置後可以有效避免資料快照與日誌記錄之間的競爭,所以建議配置

在上述配置檔案的基礎上新增叢集節點的相關配置:

# 格式:server.id=host:port1:port2
# 其中 id 為server id,對應myid
# host 為 ip 或主機名稱
# port1 為用於 followers 連線到 leader 的埠
# port2 為 leader 選舉時使用的埠
server.1=127.0.0.1:2287:3387
server.2=127.0.0.1:2288:3388
server.3=127.0.0.1:2289:3389

在對應 zookeeper 例項配置檔案中的 dataDir 目錄下建立 myid 檔案並記錄對應的 server id,以 server.1 為例:

echo "1" > /usr/local/zookeeper/data1/myid

完整的配置檔案示例如下:

tickTime=2000

initLimit=10

syncLimit=5

dataDir=/usr/local/zookeeper/data1
dataLogDit=/usr/local/zookeeper/log1

clientPort=2181

server.1=127.0.0.1:2287:3387
server.2=127.0.0.1:2288:3388
server.3=127.0.0.1:2289:3389

多個 zookeeper 例項的配置檔案一定要區分 clientPortdataDirdataLogDir

最後,逐一指定配置檔案啟動 zookeeper 例項即可

更細緻的配置引數可以參考官方文件