之前說了那么多配置信息,具體做的怎么樣,還需要真正的獲取ZooKeeper的運(yùn)行時信息才行。這里介紹ZooKeeper的監(jiān)控。

有如下方式:
- 使用內(nèi)置的四字命令來獲取健康狀態(tài)信息
- 使用JMX查看運(yùn)行狀態(tài)
- 使用Exhibitor
- 使用Sass
四字命令
最簡單的監(jiān)控方式就是使用ZooKeeper的四字命令,你可以直接通過telnet或者nc命令查看狀態(tài)。



常用的四字命令如下:
- conf: 打印ZooKeeper的配置信息
- cons: 列出所有的客戶端會話鏈接
- crst: 重置所有的客戶端連接
- dump: 打印集群的所有會話信息,包括ID,以及臨時節(jié)點(diǎn)等信息。用在Leader節(jié)點(diǎn)上才有效果。
- envi: 列出所有的環(huán)境參數(shù)
- ruok: "諧音為Are you ok"。檢查當(dāng)前服務(wù)器是否正在運(yùn)行。
- stat: 獲取ZooKeeper服務(wù)器運(yùn)行時的狀態(tài)信息,包括版本,運(yùn)行時角色,集群節(jié)點(diǎn)個數(shù)等信息。
- srst: 重置服務(wù)器統(tǒng)計信息
- srvr: 和stat輸出信息一樣,只不過少了客戶端連接信息。
- wchs: 輸出當(dāng)前服務(wù)器上管理的Watcher概要信息
- wchc: 輸出當(dāng)前服務(wù)器上管理的Watcher的詳細(xì)信息,以session為單位進(jìn)行歸組
- wchp: 和wchc非常相似,但是以節(jié)點(diǎn)路徑進(jìn)行歸組
- mntr: 輸出比stat更為詳細(xì)的服務(wù)器統(tǒng)計信息
JMX
JMX是用來遠(yuǎn)程監(jiān)控Java應(yīng)用的框架,這個也可以用來監(jiān)控其他的Java應(yīng)用。JMX使用起來也很簡單。
在運(yùn)行Java的jar的時候:
//不需要用戶密碼的
-Dcom.sun.management.jmxremote.port=5000
-Dcom.sun.management.jmxremote.authenticate=true
-Dcom.sun.management.jmxremote.ssl=false
// 使用需要用戶密碼登錄的
-Dcom.sun.management.jmxremote.port=5000
-Dcom.sun.management.jmxremote.authenticate=true
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.access.file=jmxremote.access
-Dcom.sun.management.jmxremote.password.file=jmxremote.password
? cat jmxremote.access
monitorRole readonly
controlRole readwrite
? cat jmxremote.password
monitorRole mypassword
controlRole mypassword
接下來,我們可以使用jConsole或者VisualVM進(jìn)行監(jiān)控。
默認(rèn)情況下,ZooKeeper允許本地進(jìn)行監(jiān)控。

如果想要遠(yuǎn)程監(jiān)控,可以修改zkServer.sh文件中的shell腳本

修改方式為我們剛才看到的加上
-Dcom.sun.management.jmxremote.access.file=jmxremote.access
-Dcom.sun.management.jmxremote.password.file=jmxremote.password
Exhibitor
Exhibitor是一個ZooKeeper的管理工具,由Netfix公司開發(fā),在ZooKeeper監(jiān)控,備份恢復(fù),清潔,在可視化上提供了不小的幫助。
- 首先我們下載exhibitor的pom文件
mkdir build
cd build
wget -c https://raw.github.com/Netflix/exhibitor/master/exhibitor-standalone/src/main/resources/buildscripts/standalone/maven/pom.xml
- 打包編譯jar包
//這個打包的時間真心長
mvn clean package
// 測試是否正確打包,這個exhibitor根據(jù)你自己打包出來的文件名進(jìn)行測試
java -jar target/exhibitor-1.6.0.jar --help
- 運(yùn)行
java -jar exhibitor-1.6.0.jar -c file
在瀏覽器中輸入exhibitor的地址
http://服務(wù)器地址:8080/exhibitor/v1/ui/index.html-
配置Exhibitor
Exhibitor面板
- 更多Exhitor的詳細(xì)信息,參考github的wiki
https://github.com/soabase/exhibitor/wiki
云服務(wù)商
當(dāng)然也有一些云服務(wù)商提供ZooKeeper的監(jiān)控服務(wù),一般都是收費(fèi)的。
國外的有:server density. 網(wǎng)站:https://www.serverdensity.com/
最后
在看ZooKeeper監(jiān)控的時候,發(fā)現(xiàn)ZooKeeper的監(jiān)控工具都很老了,大部分都是幾年前的了,ZooKeeper這是要被淘汰了嗎...,好用的軟件應(yīng)該都有很好用的工具才對。不過既然當(dāng)初它那么流行,還是有值得研究的地方...
參考
- 《從Paxos到ZooKeeper-分布式一致性原理與實踐》
- 《Apache ZooKeeper Essetial》
- Exhibitor Wiki
