设为首页 加入收藏

TOP

Zookeeper之创建组,加入组,列出组成员和删除组
2019-05-11 02:27:02 】 浏览:53
Tags:Zookeeper 创建 加入 列出 组成员 删除
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/dapeng1995/article/details/87910537

Zookeeper之创建组,加入组,列出组成员和删除组

public class CreateGroup implements Watcher {
    private static final int SESSION_TIMEOUT=5000;
    //ZooKeeper类是客户端API的主要类,用于维护客户端和ZooKeeper服务之间的连接
    private ZooKeeper zk;
    //锁存器(latch)此计数器为1,表示在释放所有等待线程之前需要发生的事件数,
    private CountDownLatch connectedSignal= new CountDownLatch(1);

    public void connect(String hosts) throws InterruptedException, IOException {
        //参数this表示一个Watcher对象接收来自于Zookeeper的回调,以获得各种事件的通知,在此表示CreateGroup对象
        zk = new ZooKeeper(hosts, SESSION_TIMEOUT, this);
        connectedSignal.await();
    }

    public void process(WatchedEvent watchedEvent) { // Watcher interface
        if (watchedEvent.getState()==Event.KeeperState.SyncConnected){
            //在调用这个方法表示计数器递减1,若计数器的值变为0,则await()方法返回
            connectedSignal.countDown();
        }
    }

    //创建组
    public void create(String groupName) throws KeeperException, InterruptedException {
        String path="/"+groupName;
        String createPath = zk.create(path, null/*data*/, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);// 持久化的 znode
        System.out.println("Created "+createPath);
    }

    //加入组
    public  void join(String groupName,String memberName) throws KeeperException, InterruptedException {
        String path = "/" + groupName + "/" + memberName;
        String createPath = zk.create(path, null/*data*/, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);// 持久化的 znode
        System.out.println("Created "+createPath);
    }

    //列出组成员
    public  void ListGroup(String groupName){
        String path="/"+groupName;
        try {
            List<String> children = zk.getChildren(path, false);
            if (children.isEmpty()){
                System.out.println(String.format("No members in group %s",groupName));
                System.exit(1);
            }
            for (String child:children){
                System.out.println(child);
            }
        } catch (KeeperException.NoNodeException e) {
            System.out.println(String.format("Group %s does not exist\n",groupName));
            System.exit(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
    }
    //删除组成员
    public void deleteGroup(String groupName){
        String path="/"+groupName;
        try {
            List<String> children = zk.getChildren(path, false);
            for (String child:children){
                //节点路径和版本号  将版本号设置为-1 可以绕过版本检测机制
                zk.delete(path+"/"+child,-1);
            }
            zk.delete(path,-1);
        } catch (KeeperException.NoNodeException e) {
            System.out.println(String.format("Group %s does not exist\n",groupName));
            System.exit(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
    }

    public void close() throws InterruptedException {
        zk.close();
    }



    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        CreateGroup createGroup = new CreateGroup();
        createGroup.connect("192.168.1.132:2181");
        //createGroup.join("a","b");
        //createGroup.ListGroup("a");
        createGroup.deleteGroup("a");
        createGroup.close();
    }
}

posted @ 2019-02-24 20:41 流氓小伙子 阅读(...) 评论(...) 编辑 收藏
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇kettle6.1源码部署 下一篇StreamCQL源码阅读(1) 提交任务

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目