Zookeeper的原生api操作

pom文件引入

<dependencies>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.14</version>
    </dependency>
</dependencies>

幾個狀態和概念
節點創建的權限信息ACL 參數的類型

  • ANYONE_ID_UNSAFE : 表示任何⼈
  • AUTH_IDS :此ID僅可⽤於設置ACL。它將被客戶機驗證的ID替換。
  • OPEN_ACL_UNSAFE :這是⼀個完全開放的ACL(常⽤)-->world:anyone
  • CREATOR_ALL_ACL :此ACL授予創建者身份驗證ID的所有權限
    節點的幾種類型的枚舉
  • PERSISTENT:持久節點
  • PERSISTENT_SEQUENTIAL:持久順序節點
  • EPHEMERAL:臨時節點
  • EPHEMERAL_SEQUENTIAL:臨時順序節點

zookeeper原生api創建會話操作

public class CreateSession implements Watcher {
    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws IOException, InterruptedException {
        ZooKeeper zooKeeper = new ZooKeeper("119.45.52.68:2181",6000,new CreateSession());
        System.out.println(zooKeeper.getState());
        countDownLatch.await();
        System.out.println("zk session  create success");

    }
    @Override
    public void process(WatchedEvent watchedEvent) {
        if (watchedEvent.getState()==Event.KeeperState.SyncConnected){
            countDownLatch.countDown();
        }
    }
}

正常創建回話file

zookeeper原生api獲取信息操作

public class getNoetNode implements Watcher {

    private  static ZooKeeper zooKeeper;

    public static void main(String[] args) throws InterruptedException, IOException {
        zooKeeper = new ZooKeeper("119.45.52.68:2181",10000,new getNoetNode());
        //countDownLatch.await();
        Thread.sleep(Integer.MAX_VALUE);
    }

    @Override
    public void process(WatchedEvent event) {
        if(event.getType() ==Event.EventType.NodeChildrenChanged){
//再次獲取子節點數據,監聽子節點變化
            try {
                List<String> children =
                        zooKeeper.getChildren(event.getPath(), true);
                System.out.println(children);
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //當連接創建了,服務端發送給客戶端SyncConnected事件
        if(event.getState() == Event.KeeperState.SyncConnected){
            try {
//調⽤獲取單個節點數據⽅法
                getNodeData();
                getChildren();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println();
    }

    private static void getNodeData() throws Exception {
        byte[] data = zooKeeper.getData("/persistent/children", true,
                null);
        System.out.println(new String(data,"utf-8"));
    }

    public static  void getChildren() throws Exception{
        List<String> childrens = zooKeeper.getChildren("persistent",true);
        System.out.println(childrens);

    }
}

zookeeper原生api更新節點信息

public class UpdateNode implements Watcher {

    private static ZooKeeper zooKeeper;

    public static void main(String[] args) throws Exception{
        zooKeeper = new ZooKeeper("119.45.52.68:2181",6000,new UpdateNode());
        Thread.sleep(Integer.MAX_VALUE);

    }
    @Override
    public void process(WatchedEvent event) {
        try {
            byte[] before = zooKeeper.getData("/persistent/children",false,null);
            System.out.println("修改前的值"+new String(before));
           Stat stat = zooKeeper.setData("/persistent","客戶端修改的內容".getBytes(),-1);
            System.out.println(stat);
            byte[] after = zooKeeper.getData("/persistent/children",false,null);
            System.out.println("修改後的值"+new String(after));
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

zookeeper原生api刪除節點信息

public class DeleteNode implements Watcher {
    private static  ZooKeeper zooKeeper;
    public static void main(String[] args) throws  Exception{
        zooKeeper = new ZooKeeper("119.45.52.68:2181",6000,new UpdateNode());
        Thread.sleep(Integer.MAX_VALUE);
    }
    @Override
    public void process(WatchedEvent event) {
        try {
            Stat exists = zooKeeper.exists("/persistent/children", false);
            System.out.println(exists == null ? "該節點不存在":"該節點存在");
            zooKeeper.delete("/persistent/children",-1);
            Stat exists2 = zooKeeper.exists("/persistent/children", false);
            System.out.println(exists2 == null ? "該節點不存在":"該節點存在");
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

代碼地址爲https://github.com/zhendiao/deme-code/tree/main/zk

歡迎搜索關注本人與朋友共同開發的微信面經小程序【大廠面試助手】和公衆號【微瞰技術】,以及總結的分類面試題https://github.com/zhendiao/JavaInterview

file
file

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章