1. 程式人生 > >讀取zookeeper上的dubbo註冊資訊

讀取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");
    }
}