Chinaunix首页 | 论坛 | 博客
  • 博客访问: 908992
  • 博文数量: 91
  • 博客积分: 803
  • 博客等级: 准尉
  • 技术积分: 1051
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-24 13:42
文章分类

全部博文(91)

文章存档

2021年(1)

2020年(4)

2019年(4)

2018年(9)

2017年(11)

2016年(11)

2015年(6)

2014年(3)

2013年(28)

2012年(14)

分类: 大数据

2018-05-09 19:43:01

0.连接类

点击(此处)折叠或打开

  1. package com.test.zookeeper;

  2. import java.io.IOException;
  3. import java.util.concurrent.CountDownLatch;

  4. import org.apache.zookeeper.WatchedEvent;
  5. import org.apache.zookeeper.Watcher;
  6. import org.apache.zookeeper.ZooKeeper;
  7. import org.apache.zookeeper.Watcher.Event.KeeperState;

  8. class ConnectionWatcher implements Watcher {
  9.     
  10.      private static final int SESSION_TIMEOUT = 5000;

  11.      protected ZooKeeper zk;
  12.      private CountDownLatch connectedSignal = new CountDownLatch(1);

  13.      public void connect(String hosts) throws IOException, InterruptedException {
  14.      zk = new ZooKeeper(hosts, SESSION_TIMEOUT, this);
  15.      connectedSignal.await();
  16.      }
  17.     
  18.      @Override
  19.      public void process(WatchedEvent event) {
  20.      if (event.getState() == KeeperState.SyncConnected) {
  21.      connectedSignal.countDown();
  22.      }
  23.      }
  24.     
  25.      public void close() throws InterruptedException {
  26.      zk.close();
  27.      }
  28. }


1.创建组节点
创建持久节点代表一个组.

点击(此处)折叠或打开

  1. package com.test.zookeeper;

  2. import org.apache.zookeeper.CreateMode;
  3. import org.apache.zookeeper.KeeperException;
  4. import org.apache.zookeeper.ZooDefs.Ids;

  5. public class CreateGroup extends ConnectionWatcher {

  6.     public void create(String groupName) throws KeeperException,InterruptedException {
  7.         String path = "/" + groupName;
  8.         //创建一个空数据的持久节点 访问权限为public
  9.         String createdPath = zk.create(path, null/*data*/, Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
  10.         System.out.println("Created " + createdPath);
  11.      }

  12.     public static void main(String[] args) throws Exception {
  13.         CreateGroup createGroup = new CreateGroup();
  14.      createGroup.connect("192.168.140.128:2181");
  15.      createGroup.create("DemoGroup");
  16.      createGroup.close();
  17.     }
  18. }
2.加入组
创建临时节点,会话中断则节点被删除.

点击(此处)折叠或打开

  1. package com.test.zookeeper;


  2. import org.apache.zookeeper.CreateMode;
  3. import org.apache.zookeeper.KeeperException;
  4. import org.apache.zookeeper.ZooDefs.Ids;

  5. public class JoinGroup extends ConnectionWatcher {
  6.     public void join(String groupName, String memberName) throws KeeperException,InterruptedException {
  7.      String path = "/" + groupName + "/" + memberName;
  8.      //为组成员创建临时节点
  9.      //data中可以存储当前节点服务器的配置信息 例如主机名等
  10.      String createdPath = zk.create(path, null/*data*/, Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL);
  11.      System.out.println("Created " + createdPath);
  12.     }
  13.     public static void main(String[] args) throws Exception {
  14.      JoinGroup joinGroup = new JoinGroup();
  15.      joinGroup.connect("192.168.140.128:2181");
  16.      joinGroup.join("DemoGroup", "node1");
  17.     
  18.      //通过sleep模拟组成员正在执行的任务
  19.      Thread.sleep(Long.MAX_VALUE);
  20.     }
  21. }
3.列出组成员


点击(此处)折叠或打开

  1. package com.test.zookeeper;

  2. import java.util.List;

  3. import org.apache.zookeeper.WatchedEvent;
  4. import org.apache.zookeeper.Watcher;
  5. import org.apache.zookeeper.Watcher.Event.EventType;
  6. import org.apache.zookeeper.ZooKeeper;

  7. public class ListGroup extends ConnectionWatcher {
  8.      public void list(String groupName) throws Exception{
  9.          String path = "/" + groupName;
  10.          List<String> children = zk.getChildren(path,new ChildWatcher(zk,path));
  11.      if (children.isEmpty()) {
  12.      System.out.printf("No members in group %s\n", groupName);
  13.      System.exit(1);
  14.      }
  15.      for (String child : children) {
  16.      System.out.println(child);
  17.      }
  18.      }
  19.      public static void main(String[] args) throws Exception {
  20.          ListGroup listGroup = new ListGroup();
  21.          listGroup.connect("192.168.140.128:2181");
  22.          listGroup.list("DemoGroup");
  23.          //观察者会在子节点发生变化之后打印节点列表
  24.          Thread.sleep(Long.MAX_VALUE);
  25.          //listGroup.close();
  26.      }
  27. }
  28. /**
  29.   * 子节点观察者
  30.   */
  31. class ChildWatcher implements Watcher{
  32.     protected ZooKeeper zk = null;
  33.     private String path = null;
  34.     public ChildWatcher(ZooKeeper zk,String path) {
  35.         this.zk = zk;
  36.         this.path=path;
  37.     }
  38.     @Override
  39.     public void process(WatchedEvent event) {
  40.         try {
  41.             if(event.getType() == EventType.NodeChildrenChanged){
  42.                 System.out.println("子节点反生了变化");
  43.                 //递归调用
  44.                 List<String> cs = zk.getChildren(path, new ChildWatcher(zk,path));
  45.                 for(String c : cs){
  46.                     System.out.println(c);
  47.                 }
  48.             }
  49.         } catch (Exception e) {
  50.             e.printStackTrace();
  51.         }
  52.     }
  53.     
  54. }
4.删除组成员

点击(此处)折叠或打开

  1. package com.test.zookeeper;

  2. import java.util.List;

  3. public class DeleteGroup extends ConnectionWatcher {
  4.     //删除组下所有成员
  5.     public void delete(String groupName) throws Exception {
  6.         String path = "/" + groupName;
  7.      List<String> children = zk.getChildren(path, false);
  8.      for (String child : children) {
  9.          //1.提供节点路径和版本号,如果版本号和当前节点版本号一致则删除(乐观锁机制)
  10.          //2.绕过版本检测机制
  11.      zk.delete(path + "/" + child, -1);
  12.      }
  13.      zk.delete(path, -1);
  14.     }
  15.     public static void main(String[] args) throws Exception {
  16.          DeleteGroup deleteGroup = new DeleteGroup();
  17.          deleteGroup.connect("192.168.140.128:2181");
  18.          deleteGroup.delete("DemoGroup");
  19.          deleteGroup.close();
  20.     }
  21. }




阅读(1581) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~