Java呼叫Zookeeper

blue星空發表於2021-08-18

 


watch機制


 Zookeeper watch是一種監聽通知機制,可以隨時監聽一些資料的變化,從而實現資料的及時性。

Zookeeper所有的讀操作getData(), getChildren()和 exists()都可以設定監聽(watch)。【寫操作則是不能設定監視點的。】

 

Watch的三個關鍵點:
  • 一次有效:當設定監視的資料發生改變時,該監視事件會被髮送到客戶端,並且該監聽將會停止,除非重啟註冊監聽;
  • 順序保證:網路延遲或者其他因素可能導致不同的客戶端在不同的時刻感知某一監視事件,但是不同的客戶端所看到的一切具有一致的順序;
  • 可以監聽資料和子節點:getData()和 exists()可以設定監聽資料變化;getChildren 可以設定監聽子節點變化;

 


常用API 


 

/**
* 構造器
* @param connectString 叢集的IP:埠號;多個伺服器時,中間用逗號分割
* @param sessionTimeout 超時時間,單位:毫秒
* @param watcher  監聽器,監聽節點變化
* @throws IOException
*/
public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher) throws IOException

/**
*
* @param path 節點路徑
* @param data 資料
* @param acl 訪問控制列表
* @param createMode 節點型別
* @return
* @throws KeeperException
* @throws InterruptedException
*/
public String create(String path, byte[] data, List<ACL> acl, CreateMode createMode) throws KeeperException, InterruptedException

/**
*
* @param path 節點路徑
* @param watch 監聽器
* @return 所有的子節點的名稱
* @throws KeeperException
* @throws InterruptedException
*/
public List<String> getChildren(String path, boolean watch) throws KeeperException, InterruptedException

/**
*
* @param path 節點路徑
* @param watcher 監聽器
* @param stat  狀態資訊【可以為null】
* @return 節點資料的二進位制陣列【可以通過new String()轉換成字串資訊】
* @throws KeeperException
* @throws InterruptedException
*/
public byte[] getData(String path, Watcher watcher, Stat stat) throws KeeperException, InterruptedException

/**
*
* @param path 節點路徑
* @param watch 監聽器
* @param cb 回撥函式
* @param ctx  上下文引數 ?【該引數不太理解,望知道的留言講解,謝謝】
*/
public void getData(String path, boolean watch, AsyncCallback.DataCallback cb, Object ctx)

/**
*
* @param path 節點路徑
* @param data 資料
* @param version 版本號【初始通常賦值為-1,每次更新會自動+1】
* @return  狀態資訊
* @throws KeeperException
* @throws InterruptedException
*/
public Stat setData(String path, byte[] data, int version) throws KeeperException, InterruptedException

/**
*如果Stat為null,則節點不存在
* @param path 節點路徑
* @param watch 監聽器
* @return 狀態資訊
* @throws KeeperException
* @throws InterruptedException
*/
public Stat exists(String path, boolean watch) throws KeeperException, InterruptedException

/**
* 如果要刪除的節點有子節點,會報錯:KeeperException$NotEmptyException: KeeperErrorCode = Directory not empty for
* 如果節點不存在,會報錯:KeeperException$NoNodeException: KeeperErrorCode = NoNode for
* @param path 節點路徑
* @param version 版本號[version = -1 : 匹配所有的版本]
* @throws InterruptedException
* @throws KeeperException
*/
public void delete(String path, int version) throws InterruptedException, KeeperException

 

 


JAVA呼叫


 

  • 初始化
try {
  ZooKeeper zooKeeper = new ZooKeeper("172.23.34.13:2181", 15000, event -> {
        if (event.getType() == Watcher.Event.EventType.None && event.getState() == Watcher.Event.KeeperState.SyncConnected) {
            System.out.println("Connectted successful.");
        }
    });
} catch (IOException e) {
    e.printStackTrace();
}
  • 建立節點: create
@Test
public void create() throws KeeperException, InterruptedException {
    //引數:1,節點路徑; 2,要儲存的資料; 3,節點的許可權; 4,節點的型別
    String nodePath = zooKeeper.create("/java/2183", "This is Java Node 2183.".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    System.out.println(nodePath);
}
  • 獲取子節點: ls
public void getChildren() throws KeeperException, InterruptedException {
    List<String> children = zooKeeper.getChildren("/", true);
    for (String child : children) {
        System.out.println("child: "+child);
    }
}
  • 同步獲取節點內容: get
@Test
public void getData() throws KeeperException, InterruptedException {
    String path = "/java";
    byte[] bytes = zooKeeper.getData(path, event -> {
        if (event.getType() == Watcher.Event.EventType.NodeDataChanged && path.equals(event.getPath())) {
            System.out.println("Date changed.");
        }
    }, null);
    System.out.printf("The data of %s is : %s \n",path, new String(bytes));
}
  • 非同步獲取節點內容: get
@Test
public void getDataAsync() {
    String path = "/java";
    zooKeeper.getData(path, false, new AsyncCallback.DataCallback() {
        @Override
        public void processResult(int i, String s, Object o, byte[] bytes, Stat stat) {
            System.out.printf("The data of %s is : %s \n",path, new String(bytes));
        }
    },"1000");

    //休眠20秒,檢視響應結果
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
  • 指定版本號更新資料:set
@Test
public void setData() throws KeeperException, InterruptedException {
    Stat stat = zooKeeper.setData("/java", "This is from java.".getBytes(), -1);
    //更新節點後,version會自動+1。故,返回值為0
    System.out.println(stat.getAversion());
}
  • 多執行緒下更新資料:set
@Test
public void setDataThread() throws KeeperException, InterruptedException {
    String path = "/java";
    Stat stat = new Stat();
    //1,先獲取節點的當前版本
    zooKeeper.getData(path,false,stat);
    //2,在當前版本的基礎上修改節點內容
    zooKeeper.setData(path, "This is from java.".getBytes(), stat.getVersion());
}
  • 判斷節點是否存在
@Test
public void exists() throws KeeperException, InterruptedException {
    Stat stat = zooKeeper.exists("/java", false);
    if (stat == null) {
        System.out.println("Not Exists.");
    }else {
        System.out.println("Exists.");
    }
}
  • 刪除節點
@Test
public void delete() throws KeeperException, InterruptedException {
    //version = -1 : 匹配所有的版本
    zooKeeper.delete("/java/2182", -1);
}

 

相關文章