zookeeper 官網:http://zookeeper.apache.org/ 現在最新版本是 3.4.6 ,但是這個版本我沒有運行起來,可能是那配置出現問題了,現在我用的是3.4.5 http://apache.fayea.com/apache-mirror/zookeeper/zookeeper-3.4.5/
環境:windows 8.1 、zookeeper3.4.5
現在要做的是 單機僞集羣(木有辦法 沒有那麼多機器啊)
-
修改配置文件,在conf中
將zoo_sample.cfg文件複製一個重命名爲zoo.cfg
-
修改zoo.cfg 內容
打開zoo.cfg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<code class= "hljs vala"
style= "padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)" ><span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # The number of milliseconds of each tick</span> tickTime=<span class= "hljs-number"
style= "padding:0px; margin:0px; color:rgb(42,161,152)" >2000< /span > <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # The number of ticks that the initial </span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # synchronization phase can take</span> initLimit=<span class= "hljs-number"
style= "padding:0px; margin:0px; color:rgb(42,161,152)" >10< /span > <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # The number of ticks that can pass between </span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # sending a request and getting an acknowledgement</span> syncLimit=<span class= "hljs-number"
style= "padding:0px; margin:0px; color:rgb(42,161,152)" >5< /span > <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # the directory where the snapshot is stored.</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # do not use /tmp for storage, /tmp here is just </span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # example sakes.</span> dataDir= /tmp/zookeeper <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # the port at which the clients will connect</span> clientPort=<span class= "hljs-number"
style= "padding:0px; margin:0px; color:rgb(42,161,152)" >2181< /span > <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > #</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # Be sure to read the maintenance section of the </span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # administrator guide before turning on autopurge.</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > #</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > #</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # The number of snapshots to retain in dataDir</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > #autopurge.snapRetainCount=3</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # Purge task interval in hours</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # Set to "0" to disable auto purge feature</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > #autopurge.purgeInterval=1</span></code> |
修改後:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<code class= "hljs vala"
style= "padding:0.5em; margin:0px; display:block; color:rgb(101,123,131); overflow-x:auto; background:rgb(253,246,227)" ><span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # The number of milliseconds of each tick</span> tickTime=<span class= "hljs-number"
style= "padding:0px; margin:0px; color:rgb(42,161,152)" >2000< /span > <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # The number of ticks that the initial </span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # synchronization phase can take</span> initLimit=<span class= "hljs-number"
style= "padding:0px; margin:0px; color:rgb(42,161,152)" >10< /span > <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # The number of ticks that can pass between </span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # sending a request and getting an acknowledgement</span> syncLimit=<span class= "hljs-number"
style= "padding:0px; margin:0px; color:rgb(42,161,152)" >5< /span > <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # the directory where the snapshot is stored.</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # do not use /tmp for storage, /tmp here is just </span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # example sakes.</span> dataDir=E: /zookeepercluster/servcer001/data dataLogDir=E: /zookeepercluster/servcer001/logs <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # the port at which the clients will connect</span> clientPort=<span class= "hljs-number"
style= "padding:0px; margin:0px; color:rgb(42,161,152)" >2181< /span > <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > #</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # Be sure to read the maintenance section of the </span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # administrator guide before turning on autopurge.</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > #</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > #</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # The number of snapshots to retain in dataDir</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > #autopurge.snapRetainCount=3</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # Purge task interval in hours</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > # Set to "0" to disable auto purge feature</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > #autopurge.purgeInterval=1</span> server.1=localhost:<span class= "hljs-number"
style= "padding:0px; margin:0px; color:rgb(42,161,152)" >8881< /span >:<span class= "hljs-number"
style= "padding:0px; margin:0px; color:rgb(42,161,152)" >7771< /span > server.2=localhost:<span class= "hljs-number"
style= "padding:0px; margin:0px; color:rgb(42,161,152)" >8882< /span >:<span class= "hljs-number"
style= "padding:0px; margin:0px; color:rgb(42,161,152)" >7772< /span > <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > #server.3=192.168.192.7:8883:7773</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > #server.4=192.168.192.7:8884:7774</span> <span class= "hljs-preprocessor"
style= "padding:0px; margin:0px; color:rgb(203,75,22)" > #server.5=192.168.192.7:8885:7775</span></code> |
主要是在下邊添加幾個服務器的ip地址,因爲我的都是本機,所以我這都是一樣的地址。
參數解釋:
-
tickTime:發送心跳的間隔時間,單位:毫秒
-
dataDir: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 實例通信端口號不能一樣,所以要給它們分配不同的端口號。
3. 在data下面創建一個myid文件,內容爲1
這個內容對應的是這個zookeeper的順序,第一個zookeeper就是1,第二個zookeeper就是2,沒有先後順序,只是不能重複。
這個最好是和 server.x 中的x對應
依次修改配置文件 簡歷server02,server03,server04節點 目錄結構如下:
├── server001
│ ├── data
│ ├── logs
│ └── zookeeper-3.4.5
├── server002
│ ├── data
│ ├── logs
│ └── zookeeper-3.4.5
├── server003
│ ├── data
│ ├── logs
│ └── zookeeper-3.4.5
├── server004
│ ├── data
│ ├── logs
│ └── zookeeper-3.4.5
└── server005
├── data
├── logs
└── zookeeper-3.4.5
這樣就配置好了,windows下的單機僞集羣。
下面就是啓動了:
進入bin下雙擊 :zkServer.cmd
報這個錯誤是因爲還有節點沒有啓動起來,都啓動了就會不報錯了
運行 zkServer.cmd status 可以查看節點的角色 是leader 還是follower
這樣一個僞集羣就成功了,如果有不對的地方希望大家批評指正,ORZ。
參考文章:
http://www.cnblogs.com/haippy/archive/2012/07/19/2599989.html
http://zookeeper.apache.org/doc/current/zookeeperStarted.html
http://zookeeper.apache.org/doc/current/index.html
http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/