zookeeper监控节点是否挂掉

package com.baidu;

import org.apache.zookeeper.*;
import java.util.ArrayList;
import java.util.List;

public class ZKTest {
private static List<String> nodes=new ArrayList<String>();
private static List<ZooKeeper> ns=new ArrayList<ZooKeeper>();
public static void main(String[] args){

    try {
        Watcher watcher=new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if(event.getType()== Event.EventType.NodeDeleted){
                    System.out.print("path="+event.getPath()+"    临时节点被删除了。。");
                }
                if(event.getType()== Event.EventType.NodeChildrenChanged){
                    System.out.println("发现子节点变化!!!!");
                    try {
                        nodes= getConnectedZK().getChildren("/root",true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    String path="";
                    if(!nodes.contains("eph125")){
                        path="eph125";
                    }else if(!nodes.contains("eph126")){
                        path="eph126";
                    }else if(!nodes.contains("eph128")){
                        path="eph128";
                    }
                    System.out.println("path="+path+"    子节点被删除了");

                /* 生存的每个节点都将执行一次此处代码,若执行一次,此
                 处先选取master,可以根据ip排序,将最大或者最小选举为
                  master,然后master根据自己规则去确定执行方案*/

                }
            }
        };

        System.out.println("分界线======");


        ZooKeeper zk125 = new ZooKeeper("192.168.174.125:2181", 500000, null);
        zk125.create("/root/eph125",null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        ns.add(zk125);

        ZooKeeper zk126 = new ZooKeeper("192.168.174.126:2181", 500000,null);
        zk126.create("/root/eph126",null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        ns.add(zk126);

        ZooKeeper zk128 = new ZooKeeper("192.168.174.128:2181", 500000, null);
        zk128.create("/root/eph128",null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        zk128.getChildren("/root",watcher);
        zk125.getChildren("/root",watcher);
        zk126.getChildren("/root",watcher);
        ns.add(zk128);

//            zk128.create("/root/128",null, ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL);

//            rmr(zk125,"/root"); 

        Thread.sleep(1000*60);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * 删除目录
 */
public static void rmr(ZooKeeper zk,String path){
    try {
        List<String> list=zk.getChildren(path,true);
        if(list!=null&&list.size()>0){
            for(String str:list){
                rmr(zk,path+"/"+str);
            }
        }
       zk.delete(path,-1);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static ZooKeeper getConnectedZK(){
    for(ZooKeeper zk:ns){
        ZooKeeper.States stat=zk.getState();
       if( stat.isConnected()){
           return zk;
       }
    }
    return  null;
}

}
    原文作者:背麻袋的袋鼠
    原文地址: https://www.jianshu.com/p/9413f2af9503
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞