zookeeper基本操作

  理解Zookeeper的一种方法是将他视为一个提供高可用性的文件系统。它没有文件和目录,但是有一个统一概念的节点,叫做znode,作为数据以及其他znode的容器。znode来自于一个层次级的命名空间。传统的建立成员列表的方法是以小组的名称创建一个父znode,同时子znode使用的是组成员的名称。

  1.创建组

  下面要写一个为组创建一个znode的程序,来介绍一下Zookeeper 的Java API。如下:

public class ConnectionManager implements Watcher{

    private static final int SESSION_TIMEOUT = 5000;
    protected ZooKeeper zk;
    private CountDownLatch countDownLatch = new CountDownLatch(1);

    public void connect(String hosts) throws IOException, InterruptedException {
        zk = new ZooKeeper(hosts,SESSION_TIMEOUT,this);
        countDownLatch.await();
    }

    public void process(WatchedEvent watchedEvent) {
        if(watchedEvent.getState() == Event.KeeperState.SyncConnected) {
            countDownLatch.countDown();
        }
    }

    public void close() throws InterruptedException {
        zk.close();
    }
}
public class CreateGroup extends ConnectionManager{

    public void createGroup(String groupName) throws KeeperException, InterruptedException {
        String path = "/" + groupName;
        String createPath = zk.create(path,null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("Create:"+createPath);
    }

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        CreateGroup group = new CreateGroup();
        group.connect("localhost");
        group.createGroup("/zoo");
        group.close();
    }
}

  main方法执行的时候,先创建一个CreateGroup对象,并调用它的connect方法,此方法实例化一个新的zookeeper对象,他是客户端API的主要类并且维护着客户端和zookeeper服务端的链接。这个构造函数有三个参数,第一个是Zookeeper的主机地址,第二个是每个会话的超时时间,第三个是Watcher对象的实例,Watcher接收Zookeeper的响应,并通知它各种事件,这个例子中ConnectionManager是一个Watcher,因此我们将他传递给Zookeeper的构造函数。

  当一个zookeeper的实例被创建后,它启动一个线程链接到zookeeper服务。对构造函数的响应返回很快,因此在使用zookeeper对象前等待链接建立非常重要。在这里我们使用Java的CountDownLatch来阻塞,直到zookeeper准备好客户端链接到zookeeper后,Watcher的process方法会被调用,并收到一个事件,表明链接已经建立。当收到该事件的时候,我们使用CountDownLatch的countDown操作减掉一个计数。此时计数器归0,await方法返回。当connect方法完成后,调用createGroup方法。在这个方法里我们使用zookeeper的create方法创建一个新的zookeeper的node。znode可能是临时的或则永久性的。一个临时性的znode,在客户端与服务端断开连接后,服务端便把节点删除。create方法的返回值是ZookEeper的创建路径。

  2加入组

  下面是一个将成员注入到组里的程序,每一个程序在程序运行的时候加入到组中,当程序结束的时候,它必须从这个组中移除。我们可以在Zookeeper的命名空间下创建临时节点来实现。

public class JoinGroup extends ConnectionManager{

    public void joinGroup(String groupName,String memberName) throws KeeperException, InterruptedException {
        String path = "/" + groupName + "/" + memberName;
        String createPath = zk.create(path,null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("Create:"+createPath);
    }

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        JoinGroup group = new JoinGroup();
        group.connect("localhost");
        group.joinGroup("/zoo","test");
        group.close();
    }
}

  JoinGroup与CreateGroup十分的相似,在joinGroup中创建一个临时的节点作为znode的子节点,最后会看到在程序结束的时候,临时节点也相应的被删除。

  3.列出组成员

  现在我们实现一个程序,找出组中的成员,实现如下:
  

public class ListGroup extends ConnectionManager{

    public void listGroup(String groupName) throws KeeperException, InterruptedException {
        String path = "/" + groupName;
        List<String> children = zk.getChildren(path,false);
        if(children.isEmpty()){
            System.out.println("no child");
            System.exit(1);
        }else{
            for(String child : children){
                System.out.println(child);
            }
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        ListGroup group = new ListGroup();
        group.connect("localhost");
        group.listGroup("/zoo");
        group.close();
    }
}

  4.删除一个组

  Zookeeper提供了一个带有路径和版本号的delete方法,Zookeeper只在删除的znode的版本号和已经定义过的版本号一样的时候才会删除该znode,乐观锁机制能够使客户端发现znode修改的冲突,你可以不管版本号而使用版本号-1来删除该znode。早zookeeper中没有递归删除操作,因此在删除父节点前要先删除子节点信息

  

public class DeleteGroup extends ConnectionManager{

    public void deleteGroup(String groupName) throws KeeperException, InterruptedException {
        String path = "/" + groupName;
        List<String> children = zk.getChildren(path,false);
        for(String child : children){
            String tempPath = path + "/" + child;
            List<String> temp = zk.getChildren(tempPath,false);
            if(temp.isEmpty()) {
                zk.delete(path + "/" + child, -1);
            }else{
                deleteGroup(tempPath);
            }
        }
        zk.delete(path,-1);
    }

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        DeleteGroup group = new DeleteGroup();
        group.connect("localhost");
        group.deleteGroup("/zoo");
        group.close();
    }
}

  

posted @ 2017-12-23 15:37  木易森林  阅读(1770)  评论(0编辑  收藏  举报