1. 程式人生 > 實用技巧 >微服務系列之ZooKeeper註冊中心03:zookeeper介紹與安裝

微服務系列之ZooKeeper註冊中心03:zookeeper介紹與安裝

一、Zookeeper 介紹

Apache ZooKeeper 是一個開放原始碼的分散式應用程式協調元件,是 Hadoop 和 Hbase 的重要元件。它是一個為分散式應用提供一致性服務的軟體,提供的功能包括:配置維護、域名服務、分散式同步、組服務等。

在微服務專案開發中 ZooKeeper 主要的角色是當做服務註冊中心存在,我們將編寫好的服務註冊至 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/

建立資料目錄、日誌目錄。

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即可,其他兩個機器分別寫23

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微服務架構spring全家桶視訊教程。