zoukankan      html  css  js  c++  java
  • zookeeper学习二

    1.zookeeper的watches

    1)可以理解为一个监听,ZooKeeper中的所有读取操作 - getData()getChildren()exists() - 都可以设置选择监视

    2)watches的范围

    创建事件,删除事件,更改事件,子事件

    3)删除watches

    我们可以通过调用removeWatches删除在znode上注册的手表

    2.watches样例(java)

    public class ZKDemo implements Watcher {
      private static CountDownLatch countDownLatch = new CountDownLatch(1);
      private static ZooKeeper zooKeeper;
      public static void main(String[] args) throws Exception {
        connetionZK("");
        close();
      }

      public static void connetionZK(String zk) throws IOException, KeeperException,   InterruptedException {
        //zookeeper的ip:端口
        String path = "192.168.10.150:2181";
        zooKeeper = new ZooKeeper(path, 20*1000, new ZKDemo());
        //创建节点并给值
        // createZnode(zooKeeper, "/test", "test1".getBytes());
        //子节点列表更改
        // createZnodeChild(zooKeeper, "/test", "test2".getBytes());
        //修改节点数据
        // createZnodeAndChanged(zooKeeper, "/test", "test3".getBytes());
        //创建并删除节点
        // createZnodeAndDelete(zooKeeper, "/test", "test4".getBytes());
        //数据节点下删除watch
        // createZnodeAndRMDataWatch(zooKeeper, "/test", "test5".getBytes());
        //子节点下删除watch
        // createZnodeAndRMChildWatch(zooKeeper, "/test", "test6".getBytes());
        countDownLatch.await();//阻塞程序继续执行
      }

      /**
      * 创建节点并给值
      * @param zooKeeper
      * @param path
      * @param data
      */
      public static void createZnode(ZooKeeper zooKeeper,String path,byte [] data) {
        //创建节点 路径,节点值,Watch,类型
        try {
          zooKeeper.exists(path, true);//使用默认的watcher,这里的默认指new ZooKeeper(path, 20*1000, new ZKDemo())时设置的watcher
          zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);//
        } catch (KeeperException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      /**
      * 子节点列表更改
      * @param zooKeeper
      * @param path
      * @param data
      */
      public static void createZnodeChild(ZooKeeper zooKeeper,String path,byte [] data) {
        //创建节点 路径,节点值,Watch,类型
        try {
          zooKeeper.exists(path, true);//使用默认的watcher,这里的默认指new ZooKeeper(path, 20*1000, new ZKDemo())时设置的watcher
        zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);//
        zooKeeper.getChildren(path, true);
        zooKeeper.create(path+"/child1", "child".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);//
        } catch (KeeperException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      /**
      * 修改节点数据
      * @param zooKeeper
      * @param path
      * @param data
      */
      public static void createZnodeAndChanged(ZooKeeper zooKeeper,String path,byte [] data) {
        //创建节点 路径,节点值,Watch,类型
        try {
          zooKeeper.exists(path, true);//使用默认的watcher,这里的默认指new ZooKeeper(path, 20*1000, new ZKDemo())时设置的watcher
          zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);//
          zooKeeper.exists(path, true);
          zooKeeper.setData(path, "Changed".getBytes(), -1);
        } catch (KeeperException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      /**
      * 创建并删除节点
      * @param zooKeeper
      * @param path
      * @param data
      */
      public static void createZnodeAndDelete(ZooKeeper zooKeeper,String path,byte [] data) {
        //创建节点 路径,节点值,Watch,类型
        try {
          zooKeeper.exists(path, true);//使用默认的watcher,这里的默认指new ZooKeeper(path, 20*1000, new ZKDemo())时设置的watcher
          zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);//
          zooKeeper.exists(path, true);
          zooKeeper.delete(path, -1);
        } catch (KeeperException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      /**
      * 数据节点下删除watch
      * @param zooKeeper
      * @param path
      * @param data
      */
      public static void createZnodeAndRMDataWatch(ZooKeeper zooKeeper,String path,byte [] data) {
        //创建节点 路径,节点值,Watch,类型
        try {
          zooKeeper.exists(path, true);//使用默认的watcher,这里的默认指new ZooKeeper(path, 20*1000, new ZKDemo())时设置的watcher
          zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);//
          zooKeeper.setData(path, "watch1".getBytes(), -1);
          ZKDemo zkDemo = new ZKDemo();
          byte[] bytes = zooKeeper.getData(path, zkDemo, new Stat());
          System.out.println(new String(bytes));
          zooKeeper.removeWatches(path, zkDemo, WatcherType.Data, true);
        } catch (KeeperException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      /**
      * 子节点下删除watch
      * @param zooKeeper
      * @param path
      * @param data
      */
      public static void createZnodeAndRMChildWatch(ZooKeeper zooKeeper,String path,byte [] data) {
        //创建节点 路径,节点值,Watch,类型
        try {
          ZKDemo zkDemo = new ZKDemo();
          zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);//
          zooKeeper.create(path+"/child2", "child2".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);//
          zooKeeper.setData(path, data, -1);
          zooKeeper.getChildren(path, zkDemo);
          zooKeeper.removeWatches(path, zkDemo, WatcherType.Children, true);
        } catch (KeeperException e) {
          e.printStackTrace();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      /**
      * 关闭zk
      */
      public static void close() {
        try {
          if (zooKeeper != null) {
            zooKeeper.close();
          }
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }

      public void process(WatchedEvent event) {
        countDownLatch.countDown();
        if (Event.KeeperState.SyncConnected == event.getState()) {//连接状态
          System.out.println("连接状态");
        } else if (Event.KeeperState.Disconnected == event.getState()) {//断开状态
          System.out.println("断开zookeeper");
        } else if (Event.KeeperState.AuthFailed == event.getState()) {//身份验证失败
          System.out.println("身份验证失败");
        } else if (Event.KeeperState.ConnectedReadOnly == event.getState()) {//连接只读状态
          System.out.println("连接只读状态");
        } else if (Event.KeeperState.SaslAuthenticated == event.getState()) {//sasl授权权限执行ZooKeeper操作
          System.out.println("sasl授权权限执行ZooKeeper操作");
        } else if (Event.KeeperState.Expired == event.getState()) {//服务器失效
          System.out.println("服务器失效");
        } else if (Event.KeeperState.Closed == event.getState()) {//客户端关闭
          System.out.println("客户端关闭");
        } else {
          System.out.println("未知");
        }
        System.out.println("**************************************");
        if (Event.EventType.None == event.getType()) {
          System.out.println(event.getType());
        } else if (Event.EventType.NodeCreated == event.getType()) {
          System.out.println(event.getType());
        } else if (Event.EventType.NodeDeleted == event.getType()) {
          System.out.println(event.getType());
        } else if (Event.EventType.NodeDataChanged == event.getType()) {
          System.out.println(event.getType());
        } else if (Event.EventType.NodeChildrenChanged == event.getType()) {
          System.out.println(event.getType());
        } else if (Event.EventType.DataWatchRemoved == event.getType()) {
          System.out.println(event.getType());
        } else if (Event.EventType.ChildWatchRemoved == event.getType()) {
          System.out.println(event.getType());
        } else {
          System.out.println("未知");
        }
          System.out.println("######################################");
        }
      }

  • 相关阅读:
    Luogu3119 [USACO15JAN]Grass Cownoisseur G
    BZOJ4361 isn
    洛谷1330 封锁阳光大学
    codechef AUG17 T2 Chef and Mover
    codechef AUG17 T1 Chef and Rainbow Array
    【bzoj3211】花神游历各国&&【bzoj3038】上帝造题的七分钟2
    noip 瑞士轮 ————归并排序解法
    记录string的妙用
    洛谷P1550 [USACO08OCT]打井Watering Hole
    汕头市队赛 SRM 09 C 撕书
  • 原文地址:https://www.cnblogs.com/ku-ku-ku/p/10980726.html
Copyright © 2011-2022 走看看