本文為原創文章,轉載請註明出處,謝謝 ZkClient使用 1、jar包引入,演示版本為0.8,非maven項目,可以下載jar包導入到項目中 2、創建Zookeeper連接 示例:ZkClient zc = new ZkClient("192.168.117.128:2181",10000,100 ...
本文為原創文章,轉載請註明出處,謝謝
ZkClient使用
1、jar包引入,演示版本為0.8,非maven項目,可以下載jar包導入到項目中
<dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.8</version> </dependency>
2、創建Zookeeper連接
示例:ZkClient zc = new ZkClient("192.168.117.128:2181",10000,10000,new SerializableSerializer());
API:ZkClient(java.lang.String zkServers, int sessionTimeout, int connectionTimeout, org.I0Itec.zkclient.serialize.ZkSerializer zkSerializer)
- zkServers:Zookeeper伺服器地址
- sessionTimeout:session超時時間
- connectionTimeout:連接超時時間
- zkSerializer:序列化器,ZkClient提供2種
- SerializableSerializer:對象序列化,可轉換對象
- BytesPushThroughSerializer:位元組數組序列化
3、創建節點
示例:
@Test public void createNode() { createSession(); User user = new User(); user.setId(1l); user.setName("scot"); String path = zc.create("/zkClient_01/02",user, CreateMode.PERSISTENT); System.out.println("path :" + path); }
API:
public java.lang.String create(java.lang.String path, java.lang.Object data, org.apache.zookeeper.CreateMode mode) public java.lang.String create(java.lang.String path, java.lang.Object data, java.util.List<org.apache.zookeeper.data.ACL> acl, org.apache.zookeeper.CreateMode mode)
示例用的為第一種,沒有設置訪問許可權列表(List<org.apache.zookeeper.data.ACL> acl)
- path:創建節點路徑。(需要確保父路徑存在)
- data:節點數據。設置與獲取要註意創建連接時候的序列化器
- Acl:許可權列表。詳情查看 2.1Zookeeper原始API使用
- mode:節點類型。詳情查看 2.1Zookeeper原始API使用
4、節點是否存在
boolean exists(java.lang.String path);
5、獲取節點信息
示例:
@Test public void getNode() { createSession(); User user = zc.readData("/zkClient_01"); System.out.println(user.getName()); Stat stat = new Stat(); User user1 = zc.readData("/zkClient_01",stat); System.out.println(user.getName()); System.out.println(stat); }
API:
public <T> T readData(java.lang.String path)public <T> T readData(java.lang.String path, boolean returnNullIfPathNotExists)public <T> T readData(java.lang.String path, org.apache.zookeeper.data.Stat stat)
- path:節點路徑。
- returnnullIfPathNotExists:同字面意思,節點不存在返回null
- stat:節點詳細信息。傳遞stat對象到readData方法,方法內部會填充stat數據
6、獲取子節點
示例:
@Test public void getChild() { createSession(); String path = "/node_scot"; boolean b = this.exists(path); if(b) { List<String> children =zc.getChildren(path); System.out.println(children.size()); }else { System.out.println("do not have this node"); } }
API:
public java.util.List<java.lang.String> getChildren(java.lang.String path)
7、刪除節點
示例:
@Test public void del() { createSession(); String path = "/zkClient_01/01"; if(this.exists(path)) { //zc.delete(path);//刪除當前節點,有子節點無法刪除 zc.deleteRecursive(path);//刪除非子節點 } }
API:
public boolean delete(java.lang.String path) public boolean delete(java.lang.String path, int version) public boolean deleteRecursive(java.lang.String path)
- version:節點的版本。如果版本不符無法刪除
8、更新節點信息
示例:
@Test public void writeNode() { createSession(); User user = new User(); user.setId(11l); user.setName("sksujer002"); zc.writeData("/zkClient_01",user); }
9、監控子節點改變(當前節點不存在也可以設置監控)
示例:
@Test public void subscribeChildChange() throws InterruptedException { createSession(); zc.subscribeChildChanges("/zkClient_01",new MyZkChildListener()); Thread.sleep(Integer.MAX_VALUE); } static class MyZkChildListener implements IZkChildListener { @Override public void handleChildChange(String s, List<String> strings) throws Exception { System.out.println("s:"+s); System.out.println("Strings:" + strings); } }
API:
public java.util.List<java.lang.String> subscribeChildChanges(java.lang.String path, org.I0Itec.zkclient.IZkChildListener listener) public interface IZkChildListener { void handleChildChange(java.lang.String parentPath, java.util.List<java.lang.String> currentChilds) throws java.lang.Exception; }
-
subscribeChildChanges:註冊子節點改變監控
-
path:路徑
-
IZkChildListener listener:子節點監控介面
-
-
IZkChildListener - handChildChange:子節點列表發生改變觸發此方法
-
parentPath:監控節點路徑
-
currentChilds:子節點列表
-
10、監控節點數據
示例:
@Test public void subscribeDataChange () throws InterruptedException { createSession(); zc.subscribeDataChanges("/zkClient_01",new MyZkDataListener()); Thread.sleep(Integer.MAX_VALUE); } static class MyZkDataListener implements IZkDataListener { @Override public void handleDataChange(String s, Object o) throws Exception { System.out.println("節點信息改變"); System.out.println("s:"+s); System.out.println("o:"+o); } @Override public void handleDataDeleted(String s) throws Exception { System.out.println("節點被刪除:"+s); } }
API:
public void subscribeDataChanges(java.lang.String path, org.I0Itec.zkclient.IZkDataListener listener) public interface IZkDataListener { void handleDataChange(java.lang.String dataPath, java.lang.Object object) throws java.lang.Exception; void handleDataDeleted(java.lang.String dataPath) throws java.lang.Exception; }
-
subscribeDataChanges:註冊節點數據監控
-
IZkDataListener listener:數據改變監控介面
-
-
IZkDataListener - handleDataChange:節點數據改變觸發此方法
-
dataPath:監控節點路徑
-
object:節點的新數據
-
-
IZkDataListener - handleDataDeleted:節點被刪除觸發此方法
-
dataPath:監控節點路徑