Windows安裝和使用zookeeper

2021-06-29 16:49:50 字數 4368 閱讀 8294

之前整理過一篇文章《zookeeper 分布式鎖服務》,本文介紹的 zookeeper 是以 3.4.5 這個穩定版本為基礎,最新的版本可以通過官網來獲取,zookeeper 的安裝非常簡單,下面將從單機模式和集群模式兩個方面介紹 zookeeper 的windows安裝和配置.

單機模式

單機安裝非常簡單,只要獲取到 zookeeper 的壓縮包並解壓到某個目錄如:c:\zookeeper-3.4.5\下,zookeeper 的啟動指令碼在 bin 目錄下,windows 下的啟動指令碼是 zkserver.cmd。

在你執行啟動指令碼之前,還有幾個基本的配置項需要配置一下,zookeeper 的配置檔案在 conf 目錄下,這個目錄下有 zoo_sample.cfg 和 log4j.properties,你需要做的就是將 zoo_sample.cfg 改名為 zoo.cfg,因為 zookeeper 在啟動時會找這個檔案作為預設配置檔案。下面詳細介紹一下,這個配置檔案中各個配置項的意義。

# 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=c:\\zookeeper-3.4.5\\data

datalogdir=c:\\zookeeper-3.4.5\\log

# the port at which the clients will connect

clientport=2181

## be sure to read the maintenance section of the

# administrator guide before turning on autopurge.

## ## 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

當這些配置項配置好後,你現在就可以啟動 zookeeper 了,啟動後要檢查 zookeeper 是否已經在服務,可以通過 netstat – ano 命令檢視是否有你配置的 clientport 埠號在監聽服務。

集群模式

zookeeper 不僅可以單機提供服務,同時也支援多機組成集群來提供服務。實際上 zookeeper 還支援另外一種偽集群的方式,也就是可以在一台物理機上執行多個 zookeeper 例項,下面將介紹集群模式的安裝和配置。

zookeeper 的集群模式的安裝和配置也不是很複雜,所要做的就是增加幾個配置項。集群模式除了上面的三個配置項還要增加下面幾個配置項:

initlimit=5 

synclimit=2 

server.1=192.168.211.1:2888:3888 

server.2=192.168.211.2:2888:3888

資料模型

zookeeper 會維護乙個具有層次關係的資料結構,它非常類似於乙個標準的檔案系統,如圖 1 所示:

zookeeper 這種資料結構有如下這些特點:

如何使用

zookeeper 作為乙個分布式的服務框架,主要用來解決分布式集群中應用系統的一致性問題,它能提供基於類似於檔案系統的目錄節點樹方式的資料儲存,但是 zookeeper 並不是用來專門儲存資料的,它的作用主要是用來維護和監控你儲存的資料的狀態變化。通過監控這些資料狀態的變化,從而可以達到基於資料的集群管理.

通過c#**使用zookeeper

zookeeper的使用主要是通過建立其nuget zookeepernet包下的zookeeper例項,並且呼叫其介面方法進行的,主要的操作就是對znode的增刪改操作,監聽znode的變化以及處理。

using system;

using system.collections.generic;

using system.linq;

using system.text;

using zookeepernet;

namespace zookeeperdemo}}

}

using system;

using system.collections.generic;

using system.linq;

using system.text;

using zookeepernet;

namespace zookeeperdemo}}

}

**建立連線:

1.獲取服務主機列表

2.設定超時時間

3.註冊客戶端事件

4.以執行緒安全的方式建立請求連線(啟動客戶端請求佇列,迴圈佇列基於socket通訊、根據請求型別執行不同的請求動作)

請求流程:

構造請求頭、構造request,reponse、構造響應頭、構造packet物件,packet物件準備好後,把整個物件放入乙個outgoingqueue

packet被放入outgoingqueue中,等待sendthread把packet對應的內容傳送給server。server處理分3步在doio方法中readlength readconnectresult readresponse,直到readresponse方法中確定packet請求結束。

響應流程:

建立節點:create

demo:zk.create(dir, severname.getbytes(), ids.open_acl_unsafe, createmode.persistent);

其中createmode分為4類persistent、persistentsequential、ephemeral、ephemeralsequential

persistent 建立持久化節點,對應機器關閉連線後節點/資料不會消失

persistent_sequential 如果path是以』/』結尾則以這個path作為父節點,建立乙個子節點,其子節點名字是乙個按先後順序排列的數值;否則建立乙個名字是『/』後面字元加上先後順序排列的數值字串的節點,同樣建立持久節點

ephemeral 建立瞬時節點,zookeeper在感知連線機器宕機後會清除它建立的瞬時節點

ephemeral_sequential 穿件瞬時順序節點,和persistent_sequential一樣,區別在於它是瞬時的

刪除節點 delete

demo :zk.delete(dir, -1);

前乙個引數代表節點名稱(一般用作路徑),後乙個是版本號 -1表示全匹配

檢視節點 exists

demo : zk.exists(dir, new mywatch2());

獲取資料 getdata

demo :zk.getdata(dir, new mywatch2(), stat);

獲取乙個節點的資料,可注入watcher 

設定資料 setdata

demo : zk.setdata(dir, new byte[1], 1);

獲取下級節點集合 getchildren

demo :zk.getchildren(dir, true);

儲存znodes類似檔案和目錄。但它不是乙個典型的檔案系統,zookeeper資料儲存在記憶體中,這意味著zookeeper可以實現高吞吐量和低延遲。

watcher

zookeeper有兩種watches,一種是data watches,另一種是child watches。其中,getdata()和exists()以及create()等會新增data watches,getchildren()會新增child watches。而delete()涉及到刪除資料和子節點,會同時觸發data watches和child watches。

Windows安裝和使用zookeeper

之前整理過一篇文章 zookeeper 分布式鎖服務 本文介紹的 zookeeper 是以 3.4.5 這個穩定版本為基礎,最新的版本可以通過官網 來獲取,zookeeper 的安裝非常簡單,下面將從單機模式和集群模式兩個方面介紹 zookeeper 的windows安裝和配置.單機模式 單機安裝非...

Windows安裝和使用zookeeper

之前整理過一篇文章 zookeeper 分布式鎖服務 本文介紹的 zookeeper 是以 3.4.5 這個穩定版本為基礎,最新的版本可以通過官網來獲取,zookeeper 的安裝非常簡單,下面將從單機模式和集群模式兩個方面介紹 zookeeper 的windows安裝和配置.單機模式 單機安裝非常...

Windows安裝和使用zookeeper

第二步 執行zookeeper啟動指令碼之前,還有幾個基本的配置項需要配置一下,在zookeeper 的配置檔案在 conf 目錄下 1,將 zoo sample.cfg 改名為 zoo.cfg,因為 zookeeper 在啟動時會找這個檔案作為預設配置檔案。2,配置zoo.cfg的幾個必要引數,如...