一、Zookeeper 安裝
1.zookeeper支持brew安裝。
➜ ~ brew info zookeeper zookeeper: stable 3.4.10 (bottled), HEAD Centralized server for distributed coordination of services https://zookeeper.apache.org/ Not installed From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/zookeeper.rb ==> Requirements Optional: python ✔ ==> Options --with-perl Build Perl bindings --with-python Build with python support --HEAD Install HEAD version ==> Caveats To have launchd start zookeeper now and restart at login: brew services start zookeeper Or, if you don't want/need a background service you can just run: zkServer start
通過brew install zookeeper 安裝:
➜ ~ brew install zookeeper Please wait for it to finish or terminate it to continue. ==> Downloading https://homebrew.bintray.com/bottles/zookeeper-3.4.10.sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring zookeeper-3.4.10.sierra.bottle.tar.gz ==> Caveats To have launchd start zookeeper now and restart at login: brew services start zookeeper Or, if you don't want/need a background service you can just run: zkServer start ==> Summary 🍺 /usr/local/Cellar/zookeeper/3.4.10: 241 files, 31.4MB
在/usr/local/etc/zookeeper/目錄下,已經有了缺省的配置文件
➜ zookeeper ll total 32 -rw-r--r-- 1 huangweijie admin 67B 9 4 22:13 defaults -rw-r--r-- 1 huangweijie admin 339B 9 4 22:13 log4j.properties -rw-r--r-- 1 huangweijie admin 941B 9 4 22:13 zoo.cfg -rw-r--r-- 1 huangweijie admin 941B 9 4 22:13 zoo_sample.cfg
zookeeper 默認配置文件內容:
# 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=/usr/local/var/run/zookeeper/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
配置文件主要作用為以下幾個:
tickTime=2000 dataDir=/usr/local/zk/data dataLogDir=/usr/local/zk/dataLog clientPort=2181
啟動服務
zkServer
1.zkServer --查看服務目錄
➜ zookeeper zkServer ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo.cfg Usage: ./zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
2.zkServer start --啟動服務
➜ zookeeper zkServer start ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo.cfg Starting zookeeper ... STARTED
3.zkServer status--查看服務狀態
➜ zookeeper zkServer status ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo.cfg Mode: standalone
4.zkServer stop --關閉服務
➜ zookeeper zkServer stop ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo.cfg Stopping zookeeper ... STOPPED
zkClient
➜ ~ zkCli Connecting to localhost:2181 Welcome to ZooKeeper! JLine support is enabled WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0]
其他操作系統的安裝教程可參考:
http://www.cnblogs.com/wuxl360/p/5817489.html
http://blog.csdn.net/morning99/article/details/40426133
二、Zookeeper 集群
ZooKeeper分布式模式安裝(ZooKeeper集群)也比較容易,這里說明一下基本要點。 首先要明確的是,ZooKeeper集群是一個獨立的分布式協調服務集群,“獨立”的含義就是說,如果想使用ZooKeeper實現分布式應用的協調與管理,簡化協調與管理,任何分布式應用都可以使用,這就要歸功於Zookeeper的數據模型(DataModel)和層次命名空間(Hierarchical Namespace)結構。
1、主機名稱映射配置
本機安裝將采用單機安裝集群,因此在配置主機時我們將采用同一Ip,不同端口的操作,具體配置如下:
server.1=localhost:2287:3387 server.2=localhost:2288:3388 server.3=localhost:2289:3389
在本次安裝中,我們將使用三台zookeeper 服務器,分別使用的端口上述端口。
如果在開發中需要配置多台服務器,則可以將對應的ip修改為目標服務器地址:
server.1=10.192.1.120:2288:3388 server.2=10.192.1.121:2288:3388 server.3=10.192.1.122:2288:3388
除了使用Ip 直接訪問外,也可以在本地設置host 域名解析,設置成相應的映射:
host 文件添加
10.192.1.120 slave-01 10.192.1.121 slave-02 10.192.1.122 slave-03
zookeeper 配置文件:
server.1=slave-01:2888:3888 server.2=slave-02:2888:3888 server.3=slave-03:2888:3888
server.A=B:C:D
A:其中 A 是一個數字,表示這個是服務器的編號;
B:是這個服務器的 ip 地址;
C:Leader選舉的端口;
D:Zookeeper服務器之間的通信端口。
2、創建配置文件
在安裝完Zookeeper 后,我們可以在安裝目錄下找到zoo.cfg配置文件,其中主要的幾個配置項內容如下:
#f 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. dataDir=/usr/local/var/run/zookeeper/zoo2/data # the port at which the clients will connect clientPort=2182
- client:監聽客戶端連接端口
- tickTime:基本事件單元,這個時間是作為Zookeeper服務器之間或客戶端與服務器之間維持心跳的時間間隔,每隔tickTime時間就會發送一個心跳;最小的session過期時間為2倍tickTime
- syncLimit:fowller與leader之間的心跳時間
- dataDir:存儲內存中數據庫快照的位置
在Zookeeper 的工作目錄下創建zoo1.cfg,配置內容如下:
#f 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. dataDir=/usr/local/var/run/zookeeper/zoo1/data # the port at which the clients will connect clientPort=2181 #the location of the log file server.1=localhost:2287:3387 server.2=localhost:2288:3388 server.3=localhost:2289:3389
拷貝兩份到zoo2.cfg, zoo3.cfg 主要區別為,端口,dataDir需要修改。
Zookeeper的工作目錄如下:
➜ zookeeper ll total 56 -rw-r--r-- 1 huangweijie admin 67B 9 4 22:13 defaults -rw-r--r-- 1 huangweijie admin 339B 9 4 22:13 log4j.properties -rw-r--r-- 1 huangweijie admin 941B 9 4 22:13 zoo.cfg -rw-r--r-- 1 huangweijie admin 512B 9 11 21:01 zoo1.cfg -rw-r--r-- 1 huangweijie admin 512B 9 11 21:01 zoo2.cfg -rw-r--r-- 1 huangweijie admin 512B 9 11 21:13 zoo3.cfg -rw-r--r-- 1 huangweijie admin 941B 9 4 22:13 zoo_sample.cfg
快照存放目錄創建完后如下:
➜ zookeeper ll total 0 drwxr-xr-x 3 huangweijie admin 102B 9 11 20:47 data drwxr-xr-x 4 huangweijie admin 136B 9 11 20:33 zoo1 drwxr-xr-x 4 huangweijie admin 136B 9 11 20:33 zoo2 drwxr-xr-x 4 huangweijie admin 136B 9 11 20:55 zoo3
3、設置myid
在我們配置的dataDir指定的目錄下面,創建一個myid文件,里面內容為一個數字,用來標識當前主機,conf/zoo.cfg文件中配置的server.X中X為什么數字,則myid文件中就輸入這個數字,例如:
zookeeper echo "1" zoo1/data/myid zookeeper echo "2" zoo2/data/myid zookeeper echo "3" zoo3/data/myid
4、啟動Zookeeper 集群
在Zookeeper 的工作目錄下,啟動各個節點服務:
➜ zookeeper zkServer start zoo1.cfg ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo1.cfg Starting zookeeper ... STARTED ➜ zookeeper zkServer start zoo2.cfg ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo2.cfg Starting zookeeper ... STARTED ➜ zookeeper zkServer start zoo3.cfg ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo3.cfg Starting zookeeper ... STARTED
啟動完成后,可以通過 jps 或 zkServer status 查看服務是否啟動:
➜ zookeeper jps 13024 QuorumPeerMain 13176 QuorumPeerMain 13007 QuorumPeerMain 13183 Jps ➜ zookeeper zkServer status zoo1.cfg ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo1.cfg Mode: follower ➜ zookeeper zkServer status zoo2.cfg ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo2.cfg Mode: leader
三、Zookeeper 命令
1、Zookeeper的四字命令
Zookeeper支持某些特定的四字命令字母與其的交互。他們大多數是查詢命令,用來獲取Zookeeper服務的當前狀態及相關信息。用戶在客戶端可以通過telnet或nc向Zookeeper提交相應的命令。
Zookeeper 命令 | 功能描述 |
---|---|
conf | 列出相關配置的詳細信息 |
cons | 列出所有連接到客戶端的回話詳細信息 |
dump | 列出未經處理的會話和臨時會話 |
envi | 輸出關於服務環境的詳細信息 |
reqs | 列出未經處理的請求 |
ruok | 測試服務是否處於正確狀態 |
stat | 列出關於性能和連接的客戶端列表 |
wchs | 列出服務器 watch 的詳細信息 |
wchc | 通過session 列出服務器watch 的詳細信息 |
wchp | 通過路徑列出服務器wtch 的信息信息 |
調用樣例:
- echo stat|nc 127.0.0.1 2181 來查看哪個節點被選擇作為follower或者leader
- echo ruok|nc 127.0.0.1 2181 測試是否啟動了該Server,若回復imok表示已經啟動。
- echo dump| nc 127.0.0.1 2181 ,列出未經處理的會話和臨時節點。
- echo kill | nc 127.0.0.1 2181 ,關掉server
- echo conf | nc 127.0.0.1 2181 ,輸出相關服務配置的詳細信息。
- echo cons | nc 127.0.0.1 2181 ,列出所有連接到服務器的客戶端的完全的連接 / 會話的詳細信息。
- echo envi |nc 127.0.0.1 2181 ,輸出關於服務環境的詳細信息(區別於 conf 命令)。
- echo reqs | nc 127.0.0.1 2181 ,列出未經處理的請求。
- echo wchs | nc 127.0.0.1 2181 ,列出服務器 watch 的詳細信息。
- echo wchc | nc 127.0.0.1 2181 ,通過 session 列出服務器 watch 的詳細信息,它的輸出是一個與 watch 相關的會話的列表。
- echo wchp | nc 127.0.0.1 2181 ,通過路徑列出服務器 watch 的詳細信息。它輸出一個與 session 相關的路徑。
2、Zookeeper的 Shell 操作
bin目錄下常用的腳本解釋
- zkCleanup 清理Zookeeper歷史數據,包括食物日志文件和快照數據文件
- zkCli Zookeeper的一個簡易客戶端
- zkEnv 設置Zookeeper的環境變量
- zkServer Zookeeper服務器的啟動、停止、和重啟腳本
2.1、ZkServer
zkServer 命令用於對服務器進行操作,主要命令為:
- zkServer start 啟動服務
- zkServer stop 停止服務
- zkServer status 查看服務狀態
2.2、ZkCli
zkCli 為進入服務的客戶端,與redis-cli 類似,當服務啟動后,可以通過zkCli 進入服務客戶端。常用命令為:
- zkCli 進入本機默認服務,可以通過指定配置文件進入該服務
- zkCli -server ip:port 遠程連接別的主機的服務
2.3、ZNodes
2.3.1 create
使用create 命令,可以創建一個zookeeper節點,如:
create [-s] [-e] path data acl
其中,-s或-e分別指定節點特性,順序或臨時節點,若不指定,則表示持久節點;acl用來進行權限控制。
a、創建順序節點:
create -s /zk-temp 123
執行命令后,會在目錄下創建 zk-test 節點,需要注意的是,由於加入了 **-e ** 參數,執行命令后,生成的節點目錄名會再后續加上一串數字: zk-test00000019
b、創建臨時節點:
create -e /zk-temp 123
臨時節點在客戶端回話結束后(quit),將會自動刪除。再次進入客戶端時,將不存在剛創建的臨時節點
c、創建永久節點:
create /zk-temp 123
不使用參數時,將會默認創建永久節點
2.3.2 讀取節點
- ls path [watch] :查看當前Zookeeper中所包含的內容
- get path [watch] :獲取節點數據內容和屬性
- ls2 path [watch] :ls 和 get 的功能結合
命令調用示例:
[zk: localhost:2181(CONNECTED) 1] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 4] get /zk myData cZxid = 0x500000006 ctime = Fri Oct 17 03:54:20 PDT 2014 mZxid = 0x500000006 mtime = Fri Oct 17 03:54:20 PDT 2014 pZxid = 0x500000006 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 6 numChildren = 0
2.3.3 Set
set path data [version]
使用Set 命令,可以更新指定節點的數據內容,例如:
[zk: localhost:2181(CONNECTED) 5] set /zk Jayce cZxid = 0x500000006 ctime = Fri Oct 17 03:54:20 PDT 2014 mZxid = 0x500000007 mtime = Fri Oct 17 03:55:50 PDT 2014 pZxid = 0x500000006 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 9 numChildren = 0 [zk: localhost:2181(CONNECTED) 6] get /zk Jayce cZxid = 0x500000006 ctime = Fri Oct 17 03:54:20 PDT 2014 mZxid = 0x500000007 mtime = Fri Oct 17 03:55:50 PDT 2014 pZxid = 0x500000006 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 9 numChildren = 0
2.3.3 Delete
delete path [version]
通過delete 命令可以刪除目標節點內容:
[zk: localhost:2181(CONNECTED) 7] delete /zk [zk: localhost:2181(CONNECTED) 8] ls / [zookeeper]