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

猜你喜欢

转载自my.oschina.net/u/1271447/blog/2986988
今日推荐