中間件 ZK分佈式專題與Dubbo微服務入門 6-10 判斷zk節點是否存在

0    課程地址

https://coding.imooc.com/lesson/201.html#mid=12726

 

1    重點關注

1.1    本節內容

判斷zk節點是否存在demo

 

1.2    關鍵代碼

        /**
         * 參數:
         * path:節點路徑
         * watch:watch事件
         */
        Stat stat = zkServer.getZookeeper().exists("/imooc-fake", true);
        if (stat != null) {
            System.out.println("查詢的節點版本爲dataVersion:" + stat.getVersion());
        } else {
            System.out.println("該節點不存在...");
        }
        

 

//本類watch事件,process,本類實現watcher
public Stat exists(String path, boolean watch);

//其他類watch事件,process。其他類實現Watcher
public Stat exists(final String path, Watcher watcher)

//本類watch事件,process,本類實現watcher,回調函數
 public void exists(String path, boolean watch, StatCallback cb, Object ctx);


//其他類watch事件,process。其他類實現Watcher,回調函數
public void exists(final String path, Watcher watcher,
            StatCallback cb, Object ctx);

 

 

 

2    課程內容

 

 

 

3    Coding

3.1    判斷zk節點是否存在

  • 啓動服務端
    進入到
cd /usr/local/zookeeper/bin

 
    重啓zookeeper服務端
./zkServer.sh restart

 

  • 主類
package com.imooc.zk.demo;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

/**
 * @Description: zookeeper 判斷階段是否存在demo
 */
public class ZKNodeExist implements Watcher {

    private ZooKeeper zookeeper = null;
    
    public static final String zkServerPath = "172.26.139.4:2181";
    public static final Integer timeout = 5000;
    
    public ZKNodeExist() {}
    
    public ZKNodeExist(String connectString) {
        try {
            zookeeper = new ZooKeeper(connectString, timeout, new ZKNodeExist());
        } catch (IOException e) {
            e.printStackTrace();
            if (zookeeper != null) {
                try {
                    zookeeper.close();
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
    
    private static CountDownLatch countDown = new CountDownLatch(1);
    
    public static void main(String[] args) throws Exception {
    
        ZKNodeExist zkServer = new ZKNodeExist(zkServerPath);
        
        /**
         * 參數:
         * path:節點路徑
         * watch:watch
         */
        Stat stat = zkServer.getZookeeper().exists("/imooc-fake", true);
        if (stat != null) {
            System.out.println("查詢的節點版本爲dataVersion:" + stat.getVersion());
        } else {
            System.out.println("該節點不存在...");
        }
        
        countDown.await();
    }
    
    @Override
    public void process(WatchedEvent event) {
        if (event.getType() == EventType.NodeCreated) {
            System.out.println("節點創建");
            countDown.countDown();
        } else if (event.getType() == EventType.NodeDataChanged) {
            System.out.println("節點數據改變");
            countDown.countDown();
        } else if (event.getType() == EventType.NodeDeleted) {
            System.out.println("節點刪除");
            countDown.countDown();
        }
    }
    
    public ZooKeeper getZookeeper() {
        return zookeeper;
    }
    public void setZookeeper(ZooKeeper zookeeper) {
        this.zookeeper = zookeeper;
    }
}

  • 打印日誌1(暫停狀態,等待創建節點)
該節點不存在...

 

  • linux客戶端創建節點
--啓動客戶端
zkCli.sh

[zk: localhost:2181(CONNECTED) 0] ls /
[imooc2, names, zookeeper, imooc3, imooc, testnode, imooc5]

[zk: localhost:2181(CONNECTED) 1] create /imooc-fake 555
Created /imooc-fake
[zk: localhost:2181(CONNECTED) 2] 

 

  • 打印日誌2

該節點不存在...

節點創建

 

 

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