Zookeeper三個監聽案例


一、監聽某一節點內容

/**
 * @author: PrincessHug
 * @date: 2019/2/25, 14:28
 * @Blog: https://www.cnblogs.com/HelloBigTable/
 * 監聽一個節點內容的變化
 */
public class WatchZoneDemo {
    ZooKeeper zkCli = null;

    public static void main(String[] args) throws IOException, InterruptedException {
        WatchZoneDemo wz = new WatchZoneDemo();
        wz.getConnection();
        Thread.sleep(Long.MAX_VALUE);
    }

    private void getConnection() throws IOException {
        zkCli = new ZooKeeper("192.168.126.128:2181,192.168.126.129:2181,192.168.126.130:2181", 3000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                try {
                    byte[] data = zkCli.getData("/Wyh", true, null);
                    System.out.println("監聽類型為:" + watchedEvent.getType());
                    System.out.println("監聽路徑為:" + watchedEvent.getPath());
                    System.out.println("數據被修改為:" + new String(data));
                    System.out.println("=======================================");
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

二、監聽某節點目錄的變化

/**
 * @author: PrincessHug
 * @date: 2019/2/25, 14:57
 * @Blog: https://www.cnblogs.com/HelloBigTable/
 * 監聽一個節點的子節點的變化
 */
public class WatchChildrenDemo {
    ZooKeeper zkCli = null;

    public static void main(String[] args) throws IOException, InterruptedException {
        WatchChildrenDemo wc = new WatchChildrenDemo();
        wc.getConnction();
        Thread.sleep(Long.MAX_VALUE);
    }

    private void getConnction() throws IOException {
        zkCli = new ZooKeeper("192.168.126.128:2181,192.168.126.129:2181,192.168.126.130:2181", 3000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                ArrayList<String> nodes = new ArrayList<String>();
                try {
                    List<String> children = zkCli.getChildren("/", true);
                    for (String c:children){
                        nodes.add(c);
                    }
                    System.out.println(nodes);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

三、Zookeeper當太上下線的感知系統

  1.需求:某分布式系統中,主節點有多台,可以進行動態上下限,當有任何一台機器發生了動態的上下線, 任何一台客戶端都能感知得到

  2.思路:

    (1)創建客戶端與服務端

    (2)啟動client端 並監聽

    (3)啟動server端 並注冊

    (4)當server端發生上下線

    (5)client端都能感知的到

  3.代碼

public class ZKServer {
    ZooKeeper zk = null;
    private String parentNode = "/Servers";

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        String childNode = "hd1-1";
        ZKServer zkServer = new ZKServer();
        //獲取連接
        zkServer.getConnection();
        //注冊信息
        zkServer.regist(childNode);
        //業務邏輯,提示上線
        zkServer.build(childNode);

    }

    private void build(String hostname) throws InterruptedException {
        System.out.println(hostname + "上線了!!");
        Thread.sleep(Long.MAX_VALUE);
    }

    private void regist(String hostname) throws KeeperException, InterruptedException {
        String path = zk.create(parentNode + "/server", hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(path);
    }

    private void getConnection() throws IOException {
        zk = new ZooKeeper("192.168.126.128:2181,192.168.126.129:2181,192.168.126.130:2181", 3000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {

            }
        });
    }
}

public class ZKClient {
    ZooKeeper zk = null;
    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        ZKClient zkClient = new ZKClient();
        zkClient.getConnection();
        zkClient.watching();
    }

    private void watching() throws InterruptedException {
        Thread.sleep(Long.MAX_VALUE);
    }

    private void getConnection() throws IOException {
        zk = new ZooKeeper("192.168.126.128:2181,192.168.126.129:2181,192.168.126.130:2181", 3000, new Watcher() {
            public void process(WatchedEvent watchedEvent) {
                try {
                    List<String> children = zk.getChildren("/Servers", true);
                    ArrayList<String> node = new ArrayList<String>();
                    for (String c:children){
                        byte[] data = zk.getData("/Servers/" + c, true, null);
                        node.add(new String(data));
                    }
                    System.out.println(node);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM