一、引入依賴
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
二、常用方法簡析
這裏只列舉了一些常用的方法,其他方法可以查看官方api:https://zookeeper.apache.org/doc/r3.4.10/api/index.html
1、Zookeeper構造方法
方法簽名 | 參數解析 |
ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) | connectString: 連接字符串,格式爲主機:端口號,多個用逗號分隔,如:127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002 sessionTimeout: session超時時間 watcher:watcher事件通知處理 |
ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly) | canBeReadOnly:用於標識當前會話是否支持“read-only”(只讀模式)。默認情況下,在Zookeeper集羣中,一個機器如果和集羣中過半及以上機器失去了網絡連接,那麼這個機器將不再處理客戶端請求(包括讀寫請求)。但是在某些使用場景下,當ZooKeeper服務器發生此類故障的時候,我們還是希望Zookeeper服務器能夠提供讀服務。這就是Zookeeper的“read-only”模式。 |
ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd) | sessionId:會話ID sessionPasswd:會話祕鑰 這兩個參數能夠唯一確定一個會話,同時客戶端使用這兩個參數可以實現客戶端會話複用,從而達到恢復會話的效果。具體使用方法是,第一次連接上ZooKeeper服務器時,通過調用Zookeeper對象實例的以下兩個接口,即可獲得當前會話的ID和祕鑰: long getSessionId(); byte[] getSessionPasswd(); |
ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd, boolean canBeReadOnly) |
注:ZooKeeper客戶端和服務端會話的建立是一個異步的過程。
2、創建節點
方法簽名 | 參數解析 |
String create(final String path, byte data[], List<ACL> acl, CreateMode createMode) | 返回創建的節點的實際路徑 path:路徑 data: 數據 acl:節點的ACL策略 createMode :創建模式 PERSISTENT 持久化目錄節點 PERSISTENT_SEQUENTIAL 順序自動編號的目錄節點,這種目錄節點會根據當前已近存在的節點數自動加 1 EPHEMERAL 臨時目錄節點,一旦創建這個節點的客戶端與服務器端口也就是 session 超時,這種節點會被自動刪除 EPHEMERAL_SEQUENTIAL 臨時自動編號節點 |
void create(String path, byte[] data, List<ACL> acl, CreateMode createMode, AsyncCallback.StringCallback cb, Object ctx) | 異步的創建節點的方法 cb: 一個異步回調函數 ctx: 用於傳遞一個對象,可以在回調方法執行的時候使用,通常是放一個上下文(Context)信息 |
3、判斷節點是否存在
方法簽名 | 參數解析 |
Stat exists(String path, Watcher watcher) | path:節點路徑 watcher:註冊的Watcher,用於監聽以下三類事件: 節點被創建 節點被刪除 節點被更新 |
Stat exists(String path, boolean watch) | watcher:指定是否複用ZooKeeper中默認的Watcher |
void exists(String path, Watcher watcher, AsyncCallback.StatCallback cb, Object ctx) | 異步的判斷節點是否存在的方法 cb:註冊一個異步回調函數 ctx:用於傳遞上下文信息的對象 |
void exists(String path, boolean watch, AsyncCallback.StatCallback cb, Object ctx) | 異步的判斷節點是否存在的方法 |
4、設置數據
方法簽名 | 參數解析 |
Stat setData(String path, byte[] data, int version) | path:節點路徑 data:數據 version:節點的版本,只有版本匹配上了纔會修改。-1表示匹配所有版本 |
void setData(String path, byte[] data, int version, AsyncCallback.StatCallback cb, Object ctx) | 異步的設置數據的方法 |
5、獲取數據
方法簽名 | 參數解析 |
void getData(String path, boolean watch, AsyncCallback.DataCallback cb, Object ctx) | 異步的獲取數據的方法 |
byte[] getData(String path, boolean watch, Stat stat) | |
void getData(String path, Watcher watcher, AsyncCallback.DataCallback cb, Object ctx) | 異步的獲取數據的方法 |
byte[] getData(String path, Watcher watcher, Stat stat) |
6、刪除節點
方法簽名 | 參數解析 |
void delete(String path, int version) | path:節點路徑 version:節點的版本,只有版本匹配上了纔會刪除。-1表示匹配所有版本 |
void delete(String path, int version, AsyncCallback.VoidCallback cb, Object ctx) | 異步的刪除節點的方法 |
三、示例
package com.my.zktest;
import org.apache.zookeeper.*;
/**
* Title: <br/>
* Intention: <br/>
* <p>
* Class Name: com.my.zktest.TestZookeeper<br/>
* Create Date: 2017/8/1 23:42 <br/>
* Project Name: MyTest <br/>
* Company: All Rights Reserved. <br/>
* Copyright © 2017 <br/>
* </p>
* <p>
* author: GaoWei <br/>
* 1st_examiner: <br/>
* 2nd_examiner: <br/>
* </p>
*
* @version 1.0
* @since JDK 1.7
*/
public class TestZookeeper {
public static void main(String[] args) throws Exception{
ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 100, new Watcher() {
public void process(WatchedEvent event) {
System.out.println("node changes");
}
});
String path = "/fruit";
String returnPath = zk.create(path, "banana".getBytes("UTF-8"), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("returnPath="+returnPath);
System.out.println("創建節點" + path + ",數據爲:" + new String(zk.getData(path, null, null), "UTF-8"));
zk.setData(path, "orange".getBytes(), -1);
}
}
參考:
1、http://ifeve.com/zookeeper-talk-quick-start/
2、http://www.cnblogs.com/ggjucheng/p/3370359.html
3、從PAXOS到ZOOKEEPER分佈式一致性原理與實踐.