目錄
一、概述
上兩篇文章對zookeeper的原生java api調用進行了學習。能夠對zookeeper的tree數據節點進行新增,刪除,修改數據等操
作,認真的童鞋會發現zookeeper原生提供的api調用比較麻煩,會有如下缺點:
1.session會話超時斷開
2.監聽機制watch只適用於一次,需用戶反覆註冊
爲了簡化api開發,並優化相關功能,提供一個更加簡易的客戶端,目前較爲通用的有zkClient和curator客戶端。
ZkClient是Github上的一個開源Zookeeper客戶端,是由Datameer工程師Stefan Groschupf和Peter Voss一起開發。
Curator是Netflix公司由Jordan Zimmerman開源的一套Zookeeper客戶端框架。Curator解決除了ZkClient提供的功能外,新增如下功能:
1)提供了一套Fluent風格的客戶端API框架。
2)提供了各種應用場景(Recipe,如共享鎖服務、Master選舉機制和分佈式計數器)的抽樣封裝
下面我們對兩個客戶端操作分別進行講解和學習
二、pom 依賴
<dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.2</version> </dependency>
三、ZkClient
1.創建節點
package com.szwn.cli;
import org.I0Itec.zkclient.ZkClient;
public class Create_Node_Sample {
public static void main(String[] args) throws Exception {
// 直接new ZkClient創建客戶端連接,參數分別爲服務器連接地址,集羣用逗號隔開
ZkClient zkClient = new ZkClient("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183", 5000);
String path = "/zk-client/c1";
// 創建持久化節點,第二個參數爲是否創建父節點
zkClient.createPersistent(path, true);
// zkClient.createEphemeral(path);
// zkClient.createPersistentSequential(path,"123".getBytes());
System.out.println("success create znode.");
}
}
2.獲取數據
package com.szwn.cli;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
public class Get_Data_Sample {
public static void main(String[] args) throws Exception {
String path = "/zk-client-getData";
// 直接new ZkClient創建客戶端連接,參數分別爲服務器連接地址,集羣用逗號隔開
ZkClient zkClient = new ZkClient("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183", 5000);
// 創建臨時節點,路徑爲path,數據爲123
zkClient.createEphemeral(path, "123");
// 發佈訂閱模式,訂閱給data監聽器
zkClient.subscribeDataChanges(path, new IZkDataListener() {
// 處理數據刪除事件
public void handleDataDeleted(String dataPath) throws Exception {
System.out.println("Node " + dataPath + " deleted.");
}
// 處理數據改變事件
public void handleDataChange(String dataPath, Object data) throws Exception {
System.out.println("Node " + dataPath + " changed, new data: " + data);
}
});
// 獲取數據
System.out.println(zkClient.readData(path));
// 修改數據
zkClient.writeData(path, "456");
Thread.sleep(1000);
// 刪除節點
zkClient.delete(path);
Thread.sleep(Integer.MAX_VALUE);
}
}
3.設置數據
package com.szwn.cli;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.data.Stat;
public class Set_Data_Sample {
public static void main(String[] args) throws Exception {
String path = "/zk-client-setData";
// 直接new ZkClient創建客戶端連接,參數分別爲服務器連接地址,集羣用逗號隔開
ZkClient zkClient = new ZkClient("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183", 2000);
// 創建臨時節點,路徑爲path,數據爲1
zkClient.createEphemeral(path, 1);
// 修改數據
Stat stat = zkClient.writeData(path, 2);
System.out.println(stat.getVersion());
// 按照版本號修改數據
Stat stat1 = zkClient.writeData(path, 3, -1);
System.out.println(stat1.getVersion());
}
}
4.獲取子節點
package com.szwn.cli;
import java.util.List;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
public class Get_Children_Sample {
public static void main(String[] args) throws Exception {
String path = "/zk-client-getChildren";
// 直接new ZkClient創建客戶端連接,參數分別爲服務器連接地址,集羣用逗號隔開
ZkClient zkClient = new ZkClient("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183", 5000);
// 發佈訂閱模式,子節點變化監聽
zkClient.subscribeChildChanges(path, new IZkChildListener() {
public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
System.out.println(parentPath + " 's child changed, currentChilds:" + currentChilds);
}
});
// 創建持久節點
zkClient.createPersistent(path);
Thread.sleep(1000);
// 創建子節點
zkClient.createPersistent(path + "/c1");
Thread.sleep(1000);
// 刪除子節點
zkClient.delete(path + "/c1");
Thread.sleep(1000);
// 刪除節點
zkClient.delete(path);
Thread.sleep(Integer.MAX_VALUE);
}
}
5.設置參數
package com.szwn.cli;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import org.apache.zookeeper.data.Stat;
public class Set_Param_Sample {
public static void main(String[] args) throws Exception {
String path = "/zk-client-setAuth";
// 直接new ZkClient創建客戶端連接,參數分別爲服務器連接地址,集羣用逗號隔開
ZkClient zkClient = new ZkClient("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183", 2000);
// 創建臨時節點,路徑爲path,數據爲1
zkClient.createEphemeral(path, 1);
// 設置序列化器
zkClient.setZkSerializer(new SerializableSerializer());
// 設置shutDown客戶端連接
zkClient.setShutdownTrigger(true);
}
}
6.節點存在
package com.szwn.cli;
import org.I0Itec.zkclient.ZkClient;
public class Exist_Node_Sample {
public static void main(String[] args) throws Exception {
String path = "/zk-client-exist";
// 直接new ZkClient創建客戶端連接,參數分別爲服務器連接地址,集羣用逗號隔開
ZkClient zkClient = new ZkClient("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183", 2000);
// 節點存在
System.out.println("Node " + path + " exists " + zkClient.exists(path));
}
}
7.刪除節點
package com.szwn.cli;
import org.I0Itec.zkclient.ZkClient;
import java.util.List;
public class Del_Data_Sample {
public static void main(String[] args) throws Exception {
String path = "/zk-client-delete";
// 直接new ZkClient創建客戶端連接,參數分別爲服務器連接地址,集羣用逗號隔開
ZkClient zkClient = new ZkClient("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183", 5000);
zkClient.createPersistent(path, "");
// 創建子節點
zkClient.createPersistent(path+"/c1", "");
List<String> children = zkClient.getChildren(path);
System.out.println(children);
// 遞歸刪除
zkClient.deleteRecursive(path);
System.out.println("success delete znode.");
}
}