Zookeeper学习笔记2-------group创建、查询、删除

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yue7603835/article/details/77374084

下面一段代码涉及到了 ZooKeeper 创建group、为group添加member、列出group下面的 member、递归删除group。  

这里我引用的是 ZooKeeper 3.4.10 提供的jar包。


GroupHelper.java

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

public class GroupHelper implements Watcher {
	private static final int SESSION_TIMEOUT = 5000;
	private ZooKeeper zk;
	private CountDownLatch connectSignal = new CountDownLatch(1);
	private String groupName=null;

	/**
	 * connect to zookeeper
	 * @param host
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public void connect(String host) throws IOException, InterruptedException {
		this.zk = new ZooKeeper(host, SESSION_TIMEOUT, this);
		connectSignal.await();
	}

	@Override
	public void process(WatchedEvent event) {
		// TODO Auto-generated method stub
		if (event.getState() == KeeperState.SyncConnected) {
			connectSignal.countDown();
		}
	}
	
	/**
	 * 
	 * @param groupName
	 * @throws KeeperException
	 * @throws InterruptedException
	 */
	public void create(String groupName) throws KeeperException, InterruptedException {
		this.groupName=groupName;
		String path="/".concat(groupName) ;
		String createPath=zk.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		System.out.println("Create Path:"+createPath);
	}
	
	/**
	 * @param memberName
	 * @throws KeeperException
	 * @throws InterruptedException
	 */
	public void joinMember(String memberName) throws KeeperException, InterruptedException {
		 String path=String.format("/%s/%s", this.groupName,memberName);  
		 path=zk.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);  
		 System.out.println("Path:"+path);
	} 
	
	/**
	 * 
	 * @param groupName
	 * @throws InterruptedException 
	 * @throws KeeperException 
	 */
	public void listMember(String groupName) throws  InterruptedException {
		  
		List<String> children;
		try {
			children = zk.getChildren(groupName, false);
			if(children.isEmpty()) {
				System.out.println("No Member in List......");
				System.exit(-1);
			}
			for(String child :children) {
				 System.out.println(child);
			}
		} catch (KeeperException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(-1);
		}
	}
	
	/**
	 * 
	 * @param path
	 * @throws KeeperException
	 * @throws InterruptedException
	 */
	public void deleteMember(String path) throws KeeperException, InterruptedException {
		List<String> children = zk.getChildren(path, false);
		if(children.isEmpty()) {
			return ;
		}
		for(String child:children) {
			String tmpPath=path+"/"+child;
			List<String> tmpList=zk.getChildren(tmpPath, false);
			if(tmpList.isEmpty()) {
				zk.delete(tmpPath, -1);	
			}else {
				this.deleteMember(tmpPath);
			}
		}
		zk.delete(path, -1);
	}
	

}

GroupTest.java  

我们创建一个组a ,然后又在组a下面创建c、ds、b成员,然后又在/a/ds下创建ss成员 ,最后递归删除他。

import java.io.IOException;

import org.apache.log4j.BasicConfigurator;
import org.apache.zookeeper.KeeperException;

public class ZookeeperTest {
	 static {
		 BasicConfigurator.configure();
	 }
	  public static void main(String []argv) throws IOException, InterruptedException, KeeperException {
		  GroupHelper cg=new GroupHelper();  
		  cg.connect("127.0.0.1:2181");   
		  cg.create("a");  
		  cg.joinMember("b");
		  cg.joinMember("c");
		  cg.joinMember("ds");
		  cg.joinMember("ds/ss");
		  cg.listMember("/a");
		  //递归删除
		  cg.deleteMember("/a");
	  }
	
	
}

运行结果:


对 Zookeeper的组操作是非常简单的。


猜你喜欢

转载自blog.csdn.net/yue7603835/article/details/77374084