zookeeper 官網:http://zookeeper.apache.org/ 如今最新版本是 3.4.6 ,可是這個版本我沒有運行起來,多是那配置出現問題了,如今我用的是3.4.5 http://apache.fayea.com/apache-mirror/zookeeper/zookeeper-3.4.5/html
環境:windows 8.1 、zookeeper3.4.5
shell
如今要作的是 單機僞集羣(木有辦法 沒有那麼多機器啊)apache
修改配置文件,在conf中windows
將zoo_sample.cfg文件複製一個重命名爲zoo.cfg 服務器
修改zoo.cfg 內容ide
打開zoo.cfg:ui
# 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 # # 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
修改後:spa
# 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=E:/zookeepercluster/servcer001/data dataLogDir=E:/zookeepercluster/servcer001/logs # 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. # # 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=localhost:8881:7771 server.2=localhost:8882:7772 #server.3=192.168.192.7:8883:7773 #server.4=192.168.192.7:8884:7774 #server.5=192.168.192.7:8885:7775
主要是在下邊添加幾個服務器的ip地址,由於個人都是本機,因此我這都是同樣的地址。code
參數解釋:orm
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/