欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java調用Zookeeper的實現步驟

 更新時間:2021年08月19日 08:39:30   作者:blue星空  
本文主要介紹了Java調用Zookeeper的實現步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

watch機制

Zookeeper watch是一種監(jiān)聽通知機制,可以隨時監(jiān)聽一些數據的變化,從而實現數據的及時性。

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

Watch的三個關鍵點:

  • 一次有效:當設置監(jiān)視的數據發(fā)生改變時,該監(jiān)視事件會被發(fā)送到客戶端,并且該監(jiān)聽將會停止,除非重啟注冊監(jiān)聽;
  • 順序保證:網絡延遲或者其他因素可能導致不同的客戶端在不同的時刻感知某一監(jiān)視事件,但是不同的客戶端所看到的一切具有一致的順序;
  • 可以監(jiān)聽數據和子節(jié)點:getData()和 exists()可以設置監(jiān)聽數據變化;getChildren 可以設置監(jiān)聽子節(jié)點變化;

常用API

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

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

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

/**
*
* @param path 節(jié)點路徑
* @param watcher 監(jiān)聽器
* @param stat  狀態(tài)信息【可以為null】
* @return 節(jié)點數據的二進制數組【可以通過new String()轉換成字符串信息】
* @throws KeeperException
* @throws InterruptedException
*/
public byte[] getData(String path, Watcher watcher, Stat stat) throws KeeperException, InterruptedException

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

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

/**
*如果Stat為null,則節(jié)點不存在
* @param path 節(jié)點路徑
* @param watch 監(jiān)聽器
* @return 狀態(tài)信息
* @throws KeeperException
* @throws InterruptedException
*/
public Stat exists(String path, boolean watch) throws KeeperException, InterruptedException

/**
* 如果要刪除的節(jié)點有子節(jié)點,會報錯:KeeperException$NotEmptyException: KeeperErrorCode = Directory not empty for
* 如果節(jié)點不存在,會報錯:KeeperException$NoNodeException: KeeperErrorCode = NoNode for
* @param path 節(jié)點路徑
* @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();
}

創(chuàng)建節(jié)點: create

@Test
public void create() throws KeeperException, InterruptedException {
    //參數:1,節(jié)點路徑; 2,要存儲的數據; 3,節(jié)點的權限; 4,節(jié)點的類型
    String nodePath = zooKeeper.create("/java/2183", "This is Java Node 2183.".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    System.out.println(nodePath);
}

獲取子節(jié)點: ls

public void getChildren() throws KeeperException, InterruptedException {
    List<String> children = zooKeeper.getChildren("/", true);
    for (String child : children) {
        System.out.println("child: "+child);
    }
}

同步獲取節(jié)點內容: 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));
}

異步獲取節(jié)點內容: 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);
    //更新節(jié)點后,version會自動+1。故,返回值為0
    System.out.println(stat.getAversion());
}

多線程下更新數據:set

@Test
public void setDataThread() throws KeeperException, InterruptedException {
    String path = "/java";
    Stat stat = new Stat();
    //1,先獲取節(jié)點的當前版本
    zooKeeper.getData(path,false,stat);
    //2,在當前版本的基礎上修改節(jié)點內容
    zooKeeper.setData(path, "This is from java.".getBytes(), stat.getVersion());
}

判斷節(jié)點是否存在

@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.");
    }
}

刪除節(jié)點

@Test
public void delete() throws KeeperException, InterruptedException {
    //version = -1 : 匹配所有的版本
    zooKeeper.delete("/java/2182", -1);
}

到此這篇關于Java調用Zookeeper的實現步驟的文章就介紹到這了,更多相關Java調用Zookeeper內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • java 多線程死鎖詳解及簡單實例

    java 多線程死鎖詳解及簡單實例

    這篇文章主要介紹了java 多線程死鎖詳解及簡單實例的相關資料,需要的朋友可以參考下
    2017-01-01
  • Java實現簡單的銀行管理系統(tǒng)的示例代碼

    Java實現簡單的銀行管理系統(tǒng)的示例代碼

    這篇文章主要介紹了如何利用Java實現簡單的銀行管理系統(tǒng),可以實現存款,取款,查詢等功能,文中的示例代碼講解詳細,感興趣的可以了解一下
    2022-09-09
  • Java實現SSH模式加密

    Java實現SSH模式加密

    這篇文章主要介紹了Java實現SSH模式加密的相關資料,需要的朋友可以參考下
    2016-01-01
  • java如何交換這兩個變量的值方法介紹

    java如何交換這兩個變量的值方法介紹

    在編程中可能會使用java來完成兩個變量值的交換,本文將介紹如何解決此類問題,希望可以幫助您
    2012-11-11
  • 如何利用grep-console插件使Intellij idea顯示多顏色調試日志

    如何利用grep-console插件使Intellij idea顯示多顏色調試日志

    這篇文章主要介紹了利用grep-console插件使Intellij idea顯示多顏色調試日志,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • Java類鎖、對象鎖、私有鎖沖突測試

    Java類鎖、對象鎖、私有鎖沖突測試

    這篇文章主要介紹了Java類鎖、對象鎖、私有鎖沖突測試,得出結論是加鎖方法夠成了競爭關系,同一時刻只能有一個方法能執(zhí)行,需要的朋友可以參考下
    2014-10-10
  • SpringBoot整合Spring Security的詳細教程

    SpringBoot整合Spring Security的詳細教程

    這篇文章主要介紹了SpringBoot整合Spring Security的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • java實現簡易的五子棋游戲

    java實現簡易的五子棋游戲

    這篇文章主要為大家詳細介紹了java實現簡易的五子棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Mybatis兩種不同批量插入方式的區(qū)別

    Mybatis兩種不同批量插入方式的區(qū)別

    隨著業(yè)務需要,有時我們需要將數據批量添加到數據庫,mybatis提供了將list集合循環(huán)添加到數據庫的方法,這篇文章主要給大家介紹了關于Mybatis兩種不同批量插入方式的區(qū)別,需要的朋友可以參考下
    2021-09-09
  • 淺談Java開發(fā)中的安全編碼問題

    淺談Java開發(fā)中的安全編碼問題

    這篇文章主要介紹了淺談Java開發(fā)中的安全編碼問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10

最新評論