Zookeeper是一個開放源代碼的分佈式協調服務,由雅虎建立,是Google Chubby的開源實現。Zookeeper的設計目標是將那些複雜且容易出錯的分佈式一致性服務封裝起來,構成一個高效可靠的元語集,並以一系列簡單易用的接口提供給用戶使用。 html
單機模式部署與運行(Windows) java
- 確保已經安裝了JAVA 1.6及其以上版本的JDK
- 下載Zookeeper http://zookeeper.apache.org/releases.html 目前穩定版本即stable版本爲3.4.6。將下載完成的壓縮包解壓到%ZK_HOME%下,其中ZK_HOME 目錄能夠隨意設定,如:D://programes 等
- 配置文件 zoo.cfg ,單機模式下,只須要將%ZK_HOME%/conf/zoo_sample.cfg文件重命名便可
- 啓動服務,在%ZK_HOME%bin目錄下執行zkServer.cmd便可啓動zookeeper服務器
Zookeeper 可執行腳本說明 apache
- zkCleanup 清理ZooKeeper歷史數據,包括事務日誌和快照文件
- zkCli ZooKeeper的一個建議客戶端
- zkEnv 設置ZooKeeper的環境變量
- zkServer ZooKeeper服務器的啓動、中止和重啓腳本
建立會話 安全
客戶端能夠經過建立一個ZooKeeper實例來鏈接ZooKeeper服務器。該類中的註釋這樣寫道: 服務器
ZooKeeper類是ZooKeeper客戶端庫的主類。要使用ZooKeeper服務,應用程序首先必需要實現一個ZooKeeper的實例。全部的迭代都將經過調用ZooKeeper類的方法來完成。這個類的方法都是線程安全的除非另有說明。
一旦一個與服務器的鏈接被創建,會分配給客戶端一個Session ID. 客戶端會週期性地發送心跳到服務器端,以保持會話有效。
只要Session ID 有效,應用程序均可以經過客戶端調用ZooKeeper的 API
若是由於某些緣由,致使客戶端未能在指定的時間段內(超過SessionTimeout的值)發送心跳到服務器端,則該會話將過時,Session ID 將失效。
若是客戶端對象不可用,爲了調用ZooKeeper API,應用程序必須建立一個新的客戶端 session
ZooKeeper的構造方法有: socket
ZooKeeper(String connectString,int sessionTimeout,Watcher watcher) 分佈式
ZooKeeper(String connectString,int sessionTimeout,Watcher watcher,boolean canBeReadOnly) ui
ZooKeeper(String connectString,int sessionTimeout,Watcher watcher,long sessionId,boolean canBeReadOnly) spa
ZooKeeper(String connectString,int sessionTimeout,Watcher watcher,long sessionId,byte[] sessionPasswd, boolean canBeReadOnly)
connectString 是指ZooKeeper服務器的列表,host:prot,host:port 這樣的格式構成,即主機:端口號經過英文逗號分隔。固然若是是單擊模式下只須要一個;
sessionTimeout 值會話的超時時間,單位是毫秒
watcher Watcher事件的處理器,若是設置爲null,表示不須要默認的Watcher事件處理器
canBeReadOnly 用於表示當前會話是否支持 read-only
sessionId,sessionPasswd 分別表明 會話的Id和密鑰。這兩個參數能夠惟一肯定一個會話。能夠經過ZooKeeper 實例調用 getSesssionId()和getSessionPasswd()方法獲取。
Java代碼
- /**
- *建立一個最基本的ZooKeeper會話示例
- * @author zhangwei_david
- * @version $Id: ZKConstructorDemo.java, v 0.1 2015年5月1日 下午5:55:21 zhangwei_david Exp $
- */
- public class ZKConstructorDemo implements Watcher {
-
- private static CountDownLatch connectedSemphore = new CountDownLatch(1);
-
- public static void main(String[] args) throws Exception {
- //建立Zookeeper會話實例
- ZooKeeper zookeeper = new ZooKeeper("127.0.0.1:2181", 5000, new ZKConstructorDemo());
- // 輸出當前會話的狀態
- System.out.println("zk客戶端的狀態是:" + zookeeper.getState());
- System.out.println("zk 客戶端的sessionId=" + zookeeper.getSessionId() + ", sessionPasswd是:"
- + new String(zookeeper.getSessionPasswd()));
- try {
- // 當前閉鎖在爲0以前一直等待,除非線程中斷
- connectedSemphore.await();
- } catch (Exception e) {
- System.out.println("Zookeeper session established");
- }
- }
-
- /**
- * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatchedEvent)
- */
- public void process(WatchedEvent event) {
- System.out.println("Receive watched event:" + event);
- //若是客戶端已經處於鏈接狀態閉鎖減去1
- if (KeeperState.SyncConnected == event.getState()) {
- connectedSemphore.countDown();
- }
- }
- }
結果:
Java代碼
- 2015-05-01 19:03:43 [ main:0 ] - [ INFO ] Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
- 2015-05-01 19:03:43 [ main:0 ] - [ INFO ] Client environment:host.name=David
- 2015-05-01 19:03:43 [ main:0 ] - [ INFO ] Client environment:java.version=1.8.0
- 2015-05-01 19:03:43 [ main:0 ] - [ INFO ] Client environment:java.io.tmpdir=C:\Users\Lenovo\AppData\Local\Temp\
- 2015-05-01 19:03:43 [ main:0 ] - [ INFO ] Client environment:java.compiler=<NA>
- 2015-05-01 19:03:43 [ main:0 ] - [ INFO ] Client environment:os.name=Windows 8.1
- 2015-05-01 19:03:43 [ main:0 ] - [ INFO ] Client environment:os.arch=x86
- 2015-05-01 19:03:43 [ main:0 ] - [ INFO ] Client environment:os.version=6.3
- 2015-05-01 19:03:43 [ main:0 ] - [ INFO ] Client environment:user.name=Lenovo
- 2015-05-01 19:03:43 [ main:0 ] - [ INFO ] Client environment:user.home=C:\Users\Lenovo
- 2015-05-01 19:03:43 [ main:0 ] - [ INFO ] Client environment:user.dir=H:\Alipay.com\workspace4alipay\demo
- 2015-05-01 19:03:43 [ main:0 ] - [ INFO ] Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=5000 watcher=com.cathy.demo.zk.ZKConstructorDemo@1e717c2
- zk客戶端的狀態是:CONNECTING
- zk 客戶端的sessionId=0, sessionPasswd是:
Java代碼
- **
- *使用SessionId和Sessionpwd建立的ZooKeeper會話示例
- * @author zhangwei_david
- * @version $Id: ZKConstructorWithIdAndPasswdDemo.java, v 0.1 2015年5月1日 下午5:55:21 zhangwei_david Exp $
- */
- public class ZKConstructorWithIdAndPasswdDemo implements Watcher {
-
- private static CountDownLatch connectedSemphore = new CountDownLatch(1);
-
- public static void main(String[] args) throws Exception {
- //建立Zookeeper會話實例
- ZooKeeper zookeeper = new ZooKeeper("127.0.0.1:2181", 5000,
- new ZKConstructorWithIdAndPasswdDemo());
- // 輸出當前會話的狀態
- System.out.println("未使用id和密鑰建立zk客戶端的狀態是:" + zookeeper.getState());
- long sessionId = zookeeper.getSessionId();
- byte[] sessionPasswd = zookeeper.getSessionPasswd();
- System.out.println("zk 客戶端的sessionId=" + zookeeper.getSessionId() + ", sessionPasswd是:"
- + new String(zookeeper.getSessionPasswd()));
- zookeeper = new ZooKeeper("127.0.0.1:2181", 5000, new ZKConstructorWithIdAndPasswdDemo(),
- sessionId, sessionPasswd);
- System.out.println("使用id和密鑰建立zk客戶端的狀態是:" + zookeeper.getState());
-
- zookeeper = new ZooKeeper("127.0.0.1:2181", 5000, new ZKConstructorWithIdAndPasswdDemo(),
- 1L, "test".getBytes());
- System.out.println("使用錯誤id和密鑰建立zk客戶端的狀態是:" + zookeeper.getState());
- try {
- // 當前閉鎖在爲0以前一直等待,除非線程中斷
- connectedSemphore.await();
- } catch (Exception e) {
- System.out.println("Zookeeper session established");
- }
- }
-
- /**
- * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatchedEvent)
- */
- public void process(WatchedEvent event) {
- System.out.println("Receive watched event:" + event);
- //若是客戶端已經處於鏈接狀態閉鎖減去1
- if (KeeperState.SyncConnected == event.getState()) {
- connectedSemphore.countDown();
- }
- }
- }
結果:
Java代碼
- 2015-05-01 19:14:34 [ main:0 ] - [ INFO ] Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=5000 watcher=com.cathy.demo.zk.ZKConstructorWithIdAndPasswdDemo@1e717c2
- 未使用id和密鑰建立zk客戶端的狀態是:CONNECTING
- zk 客戶端的sessionId=0, sessionPasswd是:
- 2015-05-01 19:14:34 [ main:31 ] - [ INFO ] Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=5000 watcher=com.cathy.demo.zk.ZKConstructorWithIdAndPasswdDemo@aa7bc2 sessionId=0 sessionPasswd=<hidden>
- 使用id和密鑰建立zk客戶端的狀態是:CONNECTING
- 2015-05-01 19:14:34 [ main:31 ] - [ INFO ] Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=5000 watcher=com.cathy.demo.zk.ZKConstructorWithIdAndPasswdDemo@ccd017 sessionId=1 sessionPasswd=<hidden>
- 使用錯誤id和密鑰建立zk客戶端的狀態是:CONNECTING
- 2015-05-01 19:14:34 [ main-SendThread(127.0.0.1:2181):31 ] - [ INFO ] Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
- 2015-05-01 19:14:34 [ main-SendThread(127.0.0.1:2181):31 ] - [ INFO ] Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
- 2015-05-01 19:14:34 [ main-SendThread(127.0.0.1:2181):31 ] - [ INFO ] Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
- 2015-05-01 19:14:34 [ main-SendThread(127.0.0.1:2181):47 ] - [ INFO ] Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
- 2015-05-01 19:14:34 [ main-SendThread(127.0.0.1:2181):47 ] - [ INFO ] Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
- 2015-05-01 19:14:34 [ main-SendThread(127.0.0.1:2181):47 ] - [ INFO ] Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
- 2015-05-01 19:14:34 [ main-SendThread(127.0.0.1:2181):47 ] - [ INFO ] Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x14d0ee972c70006, negotiated timeout = 5000
- 2015-05-01 19:14:34 [ main-SendThread(127.0.0.1:2181):47 ] - [ INFO ] Unable to reconnect to ZooKeeper service, session 0x1 has expired, closing socket connection
- Receive watched event:WatchedEvent state:SyncConnected type:None path:null
- Receive watched event:WatchedEvent state:Expired type:None path:null
- 2015-05-01 19:14:34 [ main-SendThread(127.0.0.1:2181):47 ] - [ INFO ] Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x14d0ee972c70007, negotiated timeout = 5000
- 2015-05-01 19:14:34 [ main-EventThread:47 ] - [ INFO ] EventThread shut down