zoukankan      html  css  js  c++  java
  • ZooKeeper-API 监听

    以服务动态上下线通知为例

    Client 监听服务器状态

    public class DistributeClient {
    
        private String connectString = "127.0.0.1:2181";
        private int sessionTimeout = 2000;
        private ZooKeeper zkClient;
    
        public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
            BasicConfigurator.configure();
            DistributeClient client = new DistributeClient();
            // 获取 zookeeper 集群连接
            client.getConnect();
            // 注册监听
            client.getChlidren();
            Thread.sleep(Long.MAX_VALUE);
        }
    
        private void getConnect() throws IOException {
            zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    try {
                        // 具体监听业务
                        getChlidren();
                    } catch (KeeperException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        private void getChlidren() throws KeeperException, InterruptedException {
            if (zkClient.exists("/servers", false) == null) {
                zkClient.create("/servers", "server".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
            // 监听 /servers 节点
            List<String> children = zkClient.getChildren("/servers", true);
            // 存储服务器节点主机名称集合
            ArrayList<String> hosts = new ArrayList<>();
            for (String child : children) {
                // 获取节点内容,即主机名称
                byte[] data = zkClient.getData("/servers/" + child, false, null);
                hosts.add(new String(data));
            }
            System.out.println("在线主机:" + hosts);
        }
    }

    Server 服务器,上线后 Client 端会收到通知

    public class DistributeServer {
    
        private String connectString = "127.0.0.1:2181";
        private int sessionTimeout = 2000;
        private ZooKeeper zkClient;
    
        public static void main(String[] args) throws Exception {
            BasicConfigurator.configure();
            DistributeServer server = new DistributeServer();
            // 连接 zookeeper 集群
            server.getConnect();
            // 注册节点
            server.regist(UUID.randomUUID().toString());
            Thread.sleep(Long.MAX_VALUE);
        }
    
        private void getConnect() throws IOException {
            zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    // TODO Auto-generated method stub
                }
            });
        }
    
        private void regist(String hostname) throws KeeperException, InterruptedException {
            // 创建临时带序号节点
            zkClient.create("/servers/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
            System.out.println(hostname + ":上线");
        }
    }

    测试

    1.直接运行 Client

    2.运行 Server 后再查看 Client 的控制台

    3.关闭  Server 后再查看 Client 的控制台


    http://zookeeper.apache.org/doc/r3.4.14/javaExample.html

    https://my.oschina.net/u/164027/blog/1921308

  • 相关阅读:
    CF1438D Powerful Ksenia(构造题)
    AT5759 ThREE(构造)
    浏览器中上面三个字,下面两个字 两端对齐(转)
    luoguP3372 【模板】线段树 1
    大数据-linux实操篇-组管理和权限管理
    大数据-linux实操篇-解压和压缩类指令
    大数据-linux实操篇-搜索查找类指令
    大数据-linux实操篇-文件目录类指令
    大数据-linux实操篇-帮助指令
    大数据-linux实操篇-实用指令(七个级别、忘记root密码)
  • 原文地址:https://www.cnblogs.com/jhxxb/p/10759851.html
Copyright © 2011-2022 走看看