ZooKeeper安裝及配置(Windows系統下)

來源:互聯網
上載者:User

標籤:print   初始   develop   一個   dex   ibm   direct   tail   作用   

ZooKeeper的定義用一句話就能說清:分布式服務架構 Zookeeper -- 管理分布式環境中的資料。下面從安裝開始,對這個架構進行分析。

1.安裝

1. 官網下載壓縮包並解壓到D:\Program Files (x86)\zookeeper-3.4.12

2. 在D:\Program Files (x86)\zookeeper-3.4.12目錄下建立data和log檔案夾

3. 複製conf目錄下zoo_sample.cfg檔案到同目錄下,重新命名為zoo.cfg(Zookeeper 在啟動時會找這個檔案作為預設設定檔),修改其中的 dataDir 和 dataLogDir 上面建立目錄的路徑

4. 啟動測試一下

服務端啟動

 啟動之後,連接埠查看可以看到ZooKeeper通過2181連接埠啟動了一個java服務

啟動用戶端串連一下,成功

2. ZooKeeper設定檔

目前設定檔zoo.cfg中的內容如下

# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial # synchronization phase can takeinitLimit=10# The number of ticks that can pass between # sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just # example sakes.dataDir=D:\\Program Files (x86)\\zookeeper-3.4.12\\data  dataLogDir=D:\\Program Files (x86)\\zookeeper-3.4.12\\log # the port at which the clients will connectclientPort=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

單機模式下,主要配置項作用:

  • tickTime:這個時間是作為 Zookeeper 伺服器之間或用戶端與伺服器之間維持心跳的時間間隔,也就是每個 tickTime 時間就會發送一個心跳。
  • dataDir:顧名思義就是 Zookeeper 儲存資料的目錄,預設情況下,Zookeeper 將寫資料的記錄檔也儲存在這個目錄裡。
  • dataLogDir:顧名思義就是 Zookeeper 儲存記錄檔的目錄
  • clientPort:這個連接埠就是用戶端串連 Zookeeper 伺服器的連接埠,Zookeeper 會監聽這個連接埠,接受用戶端的訪問請求。
3. 叢集模式

ZooKeeper的安裝模式分為三種,分別為:單機模式(stand-alone)、叢集模式和叢集偽分布模式。上面示範的即單機模式;通過多台機器共同提供服務即為叢集模式;一台電腦的話其實還可以進行偽叢集模式,即在一台物理機上運行多個 Zookeeper 執行個體。

叢集模式是通過增加設定檔zoo.cfg中的配置項來設定,主要配置項如下:

initLimit=10 syncLimit=5 server.1=192.168.211.1:2888:3888 server.2=192.168.211.2:2888:3888
  • initLimit:這個配置項是用來配置 Zookeeper 接受用戶端(這裡所說的用戶端不是使用者串連 Zookeeper 伺服器的用戶端,而是 Zookeeper 伺服器叢集中串連到 Leader 的 Follower 伺服器)初始化串連時最長能忍受多少個心跳時間間隔數。當已經超過 10 個心跳的時間(也就是 tickTime)長度後 Zookeeper 伺服器還沒有收到用戶端的返回資訊,那麼表明這個用戶端串連失敗。總的時間長度就是 10*2000=20 秒
  • syncLimit:這個配置項標識 Leader 與 Follower 之間發送訊息,請求和應答時間長度,最長不能超過多少個 tickTime 的時間長度,總的時間長度就是 5*2000=10 秒
  • server.A=B:C:D:其中 A 是一個數字,表示這個是第幾號伺服器;B 是這個伺服器的 ip 地址;C 表示的是這個伺服器與叢集中的 Leader 伺服器交換資訊的連接埠;D 表示的是萬一叢集中的 Leader 伺服器掛了,需要一個連接埠來重新進行選舉,選出一個新的 Leader,而這個連接埠就是用來執行選舉時伺服器相互連信的連接埠。如果是偽叢集的配置方式,由於 B 都是一樣,所以不同的 Zookeeper 執行個體通訊連接埠號不能一樣,所以要給它們分配不同的連接埠號碼。

除了修改 zoo.cfg 設定檔,叢集模式下還要配置一個檔案 myid,這個檔案在 dataDir 目錄下,這個檔案裡面就有一個資料就是 A 的值,Zookeeper 啟動時會讀取這個檔案,拿到裡面的資料與 zoo.cfg 裡面的配置資訊比較從而判斷到底是哪個 server。

單機偽叢集模式配置步驟如下:

3.1 設定檔修改

conf目錄下複製三次zoo.cfg到同目錄下,分別命名zoo1.cfg,zoo2.cfg,zoo3.cfg,對三個檔案進行如下修改

zoo1.cfg

zoo2.cfg

zoo3.cfg

相對應的,需要在各自dataDir下建立myid檔案(無尾碼名),內容分別為1,2,3,表示其為第幾號伺服器。

3.2 修改服務端啟動配置

bin目錄下複製三次zkServer.cmd到同目錄下,分別命名zkServer1.cmd,zkServer2.cmd,zkServer3.cmd,對三個檔案進行如下修改

zkServer1.cmd

 

zkServer2.cmd

zkServer3.cmd

 

3.3 啟動服務

cmd下啟動三個zkServer,在這裡我是開啟三個cmd視窗啟動的,順序1-2-3,三個zkServer沒全啟動的時候會報如下錯誤,這是zookeeper的Leader選舉演算法的異常資訊,當節點沒有啟動完畢的時候,Leader無法正常進行工作,這種錯誤資訊是可以忽略的,等其他節點啟動之後就正常了。

 

三個連接埠全部啟動

 4. java串連測試
import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;public class WatcherTest implements Watcher{    public void process(WatchedEvent arg0){        System.out.println("========================");        System.out.println("path:"+arg0.getPath());        System.out.println("type:"+arg0.getType());        System.out.println("state:"+arg0.getState());    }}

建立ZooKeeper執行個體時,如果有多個串連,則使用逗號隔開。

import java.io.IOException;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.ZooDefs;import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.data.Stat;import com.zang.WatcherTest;public class App {        public static void main( String[] args ) throws IOException, KeeperException, InterruptedException {        //建立一個Zookeeper執行個體,第一個參數為目標伺服器地址和連接埠,第二個參數為Session逾時時間,第三個為節點變化時的回調方法       ZooKeeper zk = new ZooKeeper("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183", 30000, new WatcherTest());              String node = "/node2";       Stat stat = zk.exists(node, false);       if(null == stat){           //建立一個節點,資料為test,不進行ACL許可權控制,節點為永久性的            String createResult = zk.create(node, "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);           System.out.println(createResult);       }              //取得/node2/test節點下的資料,返回byte[]        byte[] b = zk.getData(node, false, stat);       System.out.println(new String(b));       zk.close();    }}

 

 

 

參考:79192819

https://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/

ZooKeeper安裝及配置(Windows系統下)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.