不多說,直接上干貨!
為什么要在Win下來安裝Zookeeper呢?
其實玩過大數據的人很清楚,在Linux下我更不說了。在win下,如Disconf 、Dubbo等應用。
所以,它的應用是非常廣的。
ZooKeeper是一個分布式的,開放源碼的分布式應用程序協調服務,是Google的Chubby一個開源的實現,是Hadoop和Hbase的重要組件。它是一個為分布式應用提供一致性服務的軟件,提供的功能包括:配置維護、域名服務、分布式同步、組服務等。
ZooKeeper的目標就是封裝好復雜易出錯的關鍵服務,將簡單易用的接口和性能高效、功能穩定的系統提供給用戶。
ZooKeeper包含一個簡單的原語集,提供Java和C的接口。
ZooKeeper代碼版本中,提供了分布式獨享鎖、選舉、隊列的接口,代碼在zookeeper-3.4.8\src\recipes。其中分布鎖和隊列有Java和C兩個版本,選舉只有Java版本。
zookeeper下載地址:
http://archive.apache.org/dist/zookeeper/zookeeper-3.4.8/
(可自行在官網下載自己需要的版本)
我這里以zookeeper-3.4.8為例。
我的是解壓到指定目錄下 D:\SoftWare\zookeeper-3.4.8
本博文,只關心其bin目錄和conf目錄。
以下是默認的內容
# 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
主要修改配置中的 dataDir ,根據自己實際修改即可,如下
比如我這里是
dataDir=D:\SoftWare\zookeeper-3.4.8\data
所以,我的為
# 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=D:\SoftWare\zookeeper-3.4.8\data
# 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
進入cmd下進bin目錄,執行 zkserver.cmd 即可啟動
至此,完成!
下載的Zookeeper是.cmd的批處理命令運行的,默認沒有提供以windows服務的方式運行的方案!!!
為此,本文下載prunsrv 來作為zookeeper的服務管理。
將zookeeper做成 windows 服務,避免每次關閉后,再啟動還需要使用cmd。
下載prunsrv
下載地址:http://archive.apache.org/dist/commons/daemon/binaries/windows/ ,找到commons-daemon-1.0.15-bin-windows
解壓后復制以下文件
注意:64位系統的機器用amd64/prunsrv.exe
(1)復制 commons-daemon-1.0.15-bin-windows/amd64/prunsrv.exe 至 zookeeper-3.4.8\bin目錄下
這是做服務的執行程序
(2)復制 commons-daemon-1.0.15-bin-windows/prunmgr.exe 至 zookeeper-3.4.8\bin目錄下
監控服務配置及運行的程序
為Zookeeper配置環境變量
添加ZOOKEEPER_SERVICE, ZOOKEEPER_HOME兩個環境變量
ZOOKEEPER_SERVICE:服務名稱(不要有中文),我這里取的是,命名為zookeeper_service
ZOOKEEPER_HOME:設置到zookeeper-3.4.8目錄下
新建 zkServerStop.cmd 文件
在zookeeper-3.4.8\bin目錄下添加一個 zkServerStop.cmd 文件.
(注:cmd文件或者下面的bat文件,我們都可以先創建一個文本文件,然后把下面的內容copy進去,然后修改文本文件的后綴名即可)
內容如下:
@echo off
setlocal
TASKLIST /svc | findstr /c:"%ZOOKEEPER_SERVICE%" > %ZOOKEEPER_HOME%\zookeeper_svc.pid
FOR /F "tokens=2 delims= " %%G IN
(%ZOOKEEPER_HOME%\zookeeper_svc.pid) DO (
@set zkPID=%%G
)
taskkill /PID %zkPID% /T /F
del %ZOOKEEPER_HOME%/zookeeper_svc.pid
endlocal
新建一個批處理安裝文件install.bat
(注: install.bat 文件也需要放在zookeeper-3.4.8\bin 目錄下才可以)
prunsrv.exe "//IS//%ZOOKEEPER_SERVICE%" ^ --DisplayName="Zookeeper (%ZOOKEEPER_SERVICE%)" ^
--Description="Zookeeper (%ZOOKEEPER_SERVICE%)" ^
--Startup=auto --StartMode=exe ^
--StartPath=%ZOOKEEPER_HOME% ^
--StartImage=%ZOOKEEPER_HOME%\bin\zkServer.cmd ^
--StopPath=%ZOOKEEPER_HOME%\ ^
--StopImage=%ZOOKEEPER_HOME%\bin\zkServerStop.cmd ^
--StopMode=exe --StopTimeout=5 ^
--LogPath=%ZOOKEEPER_HOME% --LogPrefix=zookeeper-wrapper ^
--PidFile=zookeeper.pid --LogLevel=Info --StdOutput=auto --StdError=auto
最后在cmd中以管理員身份運行install.bat
(注:其實我想說在zookeeper-3.4.8\bin目錄下,雙擊
install.bat文件就可以了,然后進入服務管理,就可以看到相應的服務了,不過這時候還沒啟動,右擊啟動即可。)
查看: 開始→運行(Windows+R快捷鍵也能調出運行) 輸入:services.msc確定
如果你想徹底刪除這個服務,則
開始”——“運行”——“regedit”)然后依次打開HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services