Apache ZooKeeper 是一個開放原始碼的分佈式應用程式協調元件,是 Hadoop 和 Hbase 的重要元件。它是一個爲分佈式應用提供一致性服務的軟體,提供的功能包括:設定維護、域名服務、分佈式同步、組服務等。
在微服務專案開發中 ZooKeeper 主要的角色是當做服務註冊中心存在,我們將編寫好的服務註冊至 ZooKeeper 即可。
環境準備
ZooKeeper 在 Java 中執行,版本 1.8 或更高(JDK 8 LTS,JDK 11 LTS,JDK 12 - Java 9 和 10 不支援)
下載
ZooKeeper 下載地址:
https://zookeeper.apache.org/releases.html#download
https://archive.apache.org/dist/zookeeper/
安裝
將檔案上傳至 Linux 伺服器。
單機版
建立目錄/解壓
建立 zookeeper 目錄。
mkdir -p /usr/local/zookeeper
將檔案解壓至該目錄。
tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz -C /usr/local/zookeeper/
建立數據目錄、日誌目錄。
```java
mkdir -p /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/data
mkdir -p /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/log
修改組態檔
# 進入組態檔目錄
cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/conf/
# ZooKeeper 啓動預設載入名爲 zoo.cfg 的組態檔,複製一份命名爲 zoo.cfg
cp zoo_sample.cfg zoo.cfg
# 修改組態檔
vi zoo.cfg
主要修改數據目錄dataDir、日誌目錄dataLogDir兩處即可,修改結果如下:
# 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/apache-zookeeper-3.6.1-bin/data
dataLogDir=/usr/local/zookeeper/apache-zookeeper-3.6.1-bin/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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
啓動/關閉
cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/
bin/zkServer.sh start
---------------------------------------------------------------------------------
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
關閉:
cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/
bin/zkServer.sh stop
---------------------------------------------------------------------------------
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
叢集版
再準備兩臺機器,和剛纔單機的機器加一起構成一個叢集環境(如果電腦跑不動就改爲一臺機器跑三個進程的方式)。
建立目錄/解壓
建立 zookeeper 目錄。
mkdir -p /usr/local/zookeeper
將檔案解壓至該目錄。
tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz -C /usr/local/zookeeper/
建立數據目錄、日誌目錄。
mkdir -p /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/data
mkdir -p /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/log
myid 檔案
在 data 目錄下建立 myid 檔案,檔案中就只寫個 1 即可,其他兩個機器分別寫 2 和 3。
cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/data/
vi myid
修改組態檔
# 進入組態檔目錄
cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/conf/
# zookeeper 啓動預設載入名爲 zoo.cfg 的組態檔,所以複製一份命名爲 zoo.cfg
cp zoo_sample.cfg zoo.cfg
# 修改組態檔
vi zoo.cfg
主要修改:
數據目錄dataDir
日誌目錄dataLogDir
埠clientPort(如果是一臺機器的僞叢集,需要修改 2181 埠,比如:2181、2182、2183)
叢集設定(如果是一臺機器的僞叢集,需要修改 2888 和 3888 的埠,比如:2888、2889、2890 和 3888、3889、3890)
# 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/apache-zookeeper-3.6.1-bin/data
dataLogDir=/usr/local/zookeeper/apache-zookeeper-3.6.1-bin/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
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
# 叢集設定
# server.1 中的 1 是 myid 檔案中的內容,2888 用於叢集內部通訊,3888 用於選擇 leader
server.1=192.168.10.101:2888:3888
server.2=192.168.10.102:2888:3888
server.3=192.168.10.103:2888:3888
啓動/關閉
啓動
cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/
bin/zkServer.sh start
#################################################################################
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
關閉
cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/
bin/zkServer.sh stop
#################################################################################
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
叢集狀態檢視
cd /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/
bin/zkServer.sh status
################################ 192.168.10.101 ################################
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
################################ 192.168.10.102 ################################
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
################################ 192.168.10.103 ################################
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.6.1-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: follower
看到以上資訊說明 ZooKeeper 叢集環境已搭建成功,接下來就可以通過 RPC 框架對接 ZooKeeper,將 ZooKeeper 作爲我們的註冊中心來使用。想要更加深入學習Java微服務架構,點選獲取 java微服務架構、spring全家桶視訊教學。