Zookeeper Zkclient客戶端


  Zkclient是對Zookeeper的原生API進行了包裝,實現了超時重連、Watcher反復注冊等功能,它可以實現遞歸創建,刪除節點,但是zkClient不能遞歸給節點賦值。

主要的api如下:

  創建永久節點:

  public void createPersistent(String path)

  public void createPersistent(String path, Object data)

  public void createPersistent(String path, Object data, List<ACL> acl)  

  創建臨時節點:

  public void createEphemeral(final String path)

  public void createEphemeral(final String path, final Object data)

  public void createEphemeral(final String path, final Object data, final List<ACL> acl)

  刪除節點:

  public boolean delete(final String path)

  public boolean delete(final String path, final int version)

  public boolean deleteRecursive(String path)(遞歸刪除節點,在原生api,如果一個節點存在子節點,那么它將無法直接刪除,必須一層層遍歷先刪除全部子節點,然后才能將目標節點刪除)

  讀取節點:

  public List<String> getChildren(String path)

  更新數據:

  public void writeData(String path, Object object)

  public void writeData(final String path, Object datat, final int expectedVersion)

  判斷節點是否存在:

  protected boolean exists(final String path, final boolean watch)

  注冊監聽事件:

  ZkClient的subscribeChildChanges方法  

  ZkClient的subscribeDataChanges方法

  下面看一個測試,首先導入包:

<dependency>
     <groupId>org.apache.zookeeper</groupId>
     <artifactId>zookeeper</artifactId>
     <version>3.5.0</version>
 </dependency>
 <dependency>
     <groupId>com.github.sgroschupf</groupId>
     <artifactId>zkclient</artifactId>
     <version>0.1</version>
 </dependency>

 

package com.test.protobuf;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.List;

/**
 * Created by szekinwin on 2017/7/8.
 */
public class ZkClientDemo {

    //zookeeper連接地址
    private static final String CONNECT_ADR="172.31.19.222:2181,172.31.19.223:2181,172.31.19.234:2181";

    public static void main(String[]args){

        //5000,連接超時時間
        ZkClient zkClient = new ZkClient(CONNECT_ADR,5000);

        System.out.println("Connect successfully..");

        String path="/testRoot";
        //監聽節點  subscribeChildChanges 監聽當前節點以及子節點增加或者刪除
        zkClient.subscribeChildChanges(path, new IZkChildListener() {
            public void handleChildChange(String s, List<String> list) throws Exception {
                System.out.println("路徑:"+s);
                System.out.println("變更的節點為:"+list);
            }
        });
        //監聽節點  subscribeDataChanges 監聽當前節點以及子節點內容的變更
        zkClient.subscribeDataChanges(path, new IZkDataListener() {
            public void handleDataChange(String s, Object o) throws Exception {
                System.out.println("路徑:"+s);
                System.out.println("變更的內容為:"+o.toString());
            }

            public void handleDataDeleted(String s) throws Exception {
                System.out.println("路徑:"+s);
            }
        });
        //創建節點 true表示遞歸創建
        zkClient.createPersistent("/testRoot/children",true);
        //修改節點信息
        zkClient.writeData("/testRoot","testRoot");
        zkClient.writeData("/testRoot","new testRoot");
        //修改子節點信息
        zkClient.writeData("/testRoot/children","testRoot children");
        //遞歸刪除節點
        zkClient.deleteRecursive(path);
        try {
            Thread.sleep(50000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM