讀取zookeeper上的dubbo註冊信息

dubbo有自己的服務監聽服務器,incubator-dubbo-ops-develop,github可以下載到,網上也有很多本地部署的例子,就想了下能不能自己監聽dubbo的服務,於是寫了如下代碼。特別注意的是zookeeper的watch機制是一次性觸發,當監聽到該節點的事件被觸發執行了一次watch之後,後面zk該節點的數據需要再次設置watch。以下代碼只是提升對zk節點變化的瞭解,第三方的zk客戶端有例如Curator,它是Netflix公司開源的一個Zookeeper客戶端,與Zookeeper提供的原生客戶端相比,Curator的抽象層次更高,簡化了Zookeeper客戶端的開發量。 還有一個101tec zkclient。curator提供的功能更豐富,zkclient功能相對簡單,參考文檔不如curator。

public class NesttyMain implements Watcher, AsyncCallback.StatCallback {
    private static ZooKeeper zooKeeper;
    private static Stat stat = new Stat();
    private static NesttyMain nestty = new NesttyMain();
    public static void main(String[] args) throws InterruptedException, IOException, KeeperException {
        zooKeeper = new ZooKeeper("127.0.0.1:2181", 5000, nestty);
        Thread.sleep(1000);
        nestty.loopWatch("/");
        while (true){

        }
    }

    public void loopWatch(String path){
        try {
            List<String> paths = zooKeeper.getChildren(path,nestty);
            if(paths.isEmpty()){
                System.out.println(path+" have no children,data are "+new String(zooKeeper.getData(path,true,stat)));
                zooKeeper.exists(path,nestty);
                return;
            }
            System.out.println("parent path is: "+path+", children are: "+paths);
            for(String childPath: paths){
                if (path.equals("/")){
                    loopWatch("/"+childPath);
                }else{
                    loopWatch(path+"/"+childPath);
                }
            }
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void process(WatchedEvent event) {
        if (Event.KeeperState.SyncConnected == event.getState()) {
            System.out.println("========event get started========"+new Date());
            if(Event.EventType.None == event.getType() && null == event.getPath()){
                // 連接時的監聽事件
                System.out.println("get connection msg!");
//                try {
//                    zooKeeper.exists("/nestty",nestty);
//                } catch (KeeperException e) {
//                    e.printStackTrace();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            } else { // 內容變更時的監聽
                try {
                    //監聽當前路徑節點事件,exists只能監聽到當前路徑,子節點的還是需要getchildren
                    //一次watch處理,只針對一次事件,後續再觸發事件需要再次設置監視
                    zooKeeper.exists(event.getPath(),nestty);
                    if(event.getType().equals(Event.EventType.NodeChildrenChanged)){
                        //監聽子節點變化事件
                        loopWatch(event.getPath());
                    }
                    zooKeeper.getChildren(event.getPath(),nestty);
                    System.out.println("event path is: "+event.getPath()+", event type is: "+event.getType()+", get msg content data:"
                            + new String(zooKeeper.getData(event.getPath(),true,stat)));
                    System.out.println("get msg stat:czxid=" + stat.getCzxid()
                            + ";mzxid=" + stat.getMzxid() + ";version="  + stat.getVersion());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void processResult(int rc, String path, Object ctx, Stat stat) {

        System.out.println("xzxzc");
    }
}

只需要依賴

org.apache.zookeeper,pom文件如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cqq</groupId>
    <artifactId>nestty</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <zookeeper.version>3.4.12</zookeeper.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.springframework.boot</groupId>-->
            <!--<artifactId>spring-boot-starter-web</artifactId>-->
        <!--</dependency>-->
    </dependencies>

</project>

 

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