使用zookeeper 实现客户端动态洞察服务器上下线

业务模型如下:
使用zookeeper 实现客户端动态洞察服务器上下线_第1张图片
具体实现代码:
导包

  <dependency>
     <groupId>org.apache.zookeeper</groupId>
     <artifactId>zookeeper</artifactId>
     <version>3.4.14</version>
  </dependency>

服务器上的客户端

public class ZooKeeperCon {
    private String address = "host:2181,host:2182,host:2183";
    private int timeout = 200000;
    private ZooKeeper zk;

    public void init() throws IOException {
        zk = new ZooKeeper(address, timeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                List<String> children = null;
                try {
                    children = zk.getChildren("/sanguo",false);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (String s:children){
                    System.out.println(s);
                }
            }
        });
    }

    public String regist(String node,String name) throws KeeperException, InterruptedException {
        String path = zk.create("/Servers"+"/"+node,name.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        if (path!=null){
            System.out.println(name +"is online");
        }
        return path;
    }

    public void createNode() throws KeeperException, InterruptedException {
        String path = zk.create("/wsd2","wushunda2".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println(path);
    }
}
public class Main {

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
      ZooKeeperCon con = new ZooKeeperCon();
      con.init();
      con.regist("server1","1号机器");
      System.in.read();
    }
}

运行完上述代码,既会出现Servers下创建一个临时节点Server1,当代码停止运行(服务器宕机)时,临时节点会消失。

客户端代码

public class ZooKeeperCon {
    private String address = "host:2181,host:2182,host:2183";
    private int timeout = 200000;
    private ZooKeeper zk;

    public void init() throws IOException {
        zk = new ZooKeeper(address, timeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                List<String> children = null;
                try {
                    children = zk.getChildren("/sanguo",false);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (String s:children){
                    System.out.println(s);
                }
            }
        });
    }
    public void getChildren(String node) throws KeeperException, InterruptedException {
        List<String> children = zk.getChildren("/"+node,true);
        for (String s:children){
            System.out.println(s);
        }
    }
}

public class Main {

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
      ZooKeeperCon con = new ZooKeeperCon();
      con.init();
      con.getChildren("Server");
      System.in.read();
    }
}

以上代码即可完成上述业务逻辑

你可能感兴趣的:(ZooKeeper)