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();
                }
            }
        });
    }
}

 

转载于:https://www.cnblogs.com/HelloBigTable/p/10434204.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值