1. 程式人生 > >zookeeper初體驗-搭建偽分散式

zookeeper初體驗-搭建偽分散式

1.建立目錄

實際搭建的時候需要確認下面幾件事情:

1.檢查jdk環境,我這裡是1.8,如果是openjdk須先解除安裝,安裝jdk1.8

2.叢集的時間是否一致,同步時間

3.檢測防火牆是否關閉,可以開啟需要的埠,或者學習的時候可以直接關閉防火牆,

       service iptables stop

4.檢測主機有沒有做ip對映

因為沒有那麼多的主機,所以體驗了一把zookeeper的偽分散式,第一步就是要建立目錄;

建立好的目錄結構如下:

我在/usr/local下建立了一個zookeeper目錄,並在其下分別建立了data和logs資料夾為後期做準備 

那個zookeeper-3.4.10這個資料夾是zookeeper的安裝包的資料夾,等會兒再上傳

建立好後就是下載zookeeper安裝包,連結地址https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/,下載3.4.10版本

接下來就是將壓縮檔案移動到到server01這個資料夾下,然後解壓

接著就是進入zookeeper-3.4.10這個資料夾下的conf資料夾,然後發現有一個zoo_sample.cfg,我們在同目錄下複製檔案

cp zoo_sample.cfg zoo.cfg

這裡要複製偽zoo.cfg是因為zookeeper預設載入的是這個檔名

然後我們開啟這個檔案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=/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

然後我們將他修改為這樣的

 

# 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/server01/data
dataLogDir=/usr/local/zookeeper/server01/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=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773
server.4=127.0.0.1:8884:7774
server.5=127.0.0.1:8885:7775

  • tickTime:這個時間是作為 Zookeeper 伺服器之間或客戶端與伺服器之間維持心跳的時間間隔,也就是每個 tickTime 時間就會發送一個心跳。
  • dataDir:顧名思義就是 Zookeeper 儲存資料的目錄,預設情況下,Zookeeper 將寫資料的日誌檔案也儲存在這個目錄裡。
  • clientPort:這個埠就是客戶端連線 Zookeeper 伺服器的埠,Zookeeper 會監聽這個埠,接受客戶端的訪問請求。
  • initLimit:這個配置項是用來配置 Zookeeper 接受客戶端(這裡所說的客戶端不是使用者連線 Zookeeper 伺服器的客戶端,而是 Zookeeper 伺服器叢集中連線到 Leader 的 Follower 伺服器)初始化連線時最長能忍受多少個心跳時間間隔數。當已經超過 5個心跳的時間(也就是 tickTime)長度後 Zookeeper 伺服器還沒有收到客戶端的返回資訊,那麼表明這個客戶端連線失敗。總的時間長度就是 5*2000=10 秒
  • syncLimit:這個配置項標識 Leader 與 Follower 之間傳送訊息,請求和應答時間長度,最長不能超過多少個 tickTime 的時間長度,總的時間長度就是 2*2000=4 秒
  • server.A=B:C:D:其中 A 是一個數字,表示這個是第幾號伺服器;B 是這個伺服器的 ip 地址;C 表示的是這個伺服器與叢集中的 Leader 伺服器交換資訊的埠;D 表示的是萬一叢集中的 Leader 伺服器掛了,需要一個埠來重新進行選舉,選出一個新的 Leader,而這個埠就是用來執行選舉時伺服器相互通訊的埠。如果是偽叢集的配置方式,由於 B 都是一樣,所以不同的 Zookeeper 例項通訊埠號不能一樣,所以要給它們分配不同的埠號。

這裡需要注意的是三個地方

 

配置好後直接將整個檔案zookeeper3.4.10這個資料夾分別複製到server02,server03.....資料夾下,然後分別修改這個配置檔案,然後需要注意的三個地方需要注意一下,

clientPort=2181

 這個配置,分別需要配置偽2181,2182,2183,2184,2185,這些配置好後,還需要將每個server一個id

echo 1 > /usr/local/zookeeper/server01/data/myid
echo 2 > /usr/local/zookeeper/server02/data/myid
echo 3 > /usr/local/zookeeper/server03/data/myid
echo 4 > /usr/local/zookeeper/server04/data/myid
echo 5 > /usr/local/zookeeper/server05/data/myid

到這裡基本上配置就配置好了

然後依次進入zookeeper3.4.10/bin資料夾下執行

./zkServer.sh start

 如果你看到了如下資訊

ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/server01/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

說明啟動成功,然後都啟動完成之後,可以執行

 

這樣一個偽分散式就搭建完成了!

不足之處,歡迎指出