Read dubbo registration information on zookeeper

Posted by don_s on Tue, 03 Dec 2019 09:03:03 +0100

dubbo has its own service monitoring server, incubator dubbo OPS development, and github can be downloaded. There are also many local deployment examples on the Internet, so I wondered if I could monitor dubbo's service by myself, so I wrote the following code. In particular, zookeeper's watch mechanism is a one-time trigger. When it is heard that the event of this node is triggered and a watch is executed, the data of this node in zk needs to be set again. The following code only improves the understanding of the changes of zk nodes. The third-party zk client, for example, coordinator, is an open-source zookeeper client of Netflix. Compared with the native client provided by zookeeper, the coordinator has a higher level of abstraction and simplifies the development of zookeeper client. There is also a 101tec zkclient. The functions provided by the cursor are more abundant, the function of zkclient is relatively simple, and the reference documents are not as good as the cursor.

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()){
                // Listening event on connection
                System.out.println("get connection msg!");
//                try {
//                    zooKeeper.exists("/nestty",nestty);
//                } catch (KeeperException e) {
//                    e.printStackTrace();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            } else { // Monitoring when content changes
                try {
                    //Listen to the current path node event. exists can only listen to the current path. The child node still needs getchildren
                    //One time watch processing, only for one event, subsequent re triggering events need to be monitored again
                    zooKeeper.exists(event.getPath(),nestty);
                    if(event.getType().equals(Event.EventType.NodeChildrenChanged)){
                        //Listen for child node change events
                        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");
    }
}

Topics: Programming Zookeeper Dubbo github