设为首页 加入收藏

TOP

Zookeeper三个监听案例(一)
2019-09-17 18:19:49 】 浏览:33
Tags: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(Wa
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇磊哥评测之数据库:腾讯云MongoDB.. 下一篇golden gate 加initial load 在ra..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目