前面已经安装好Zookeeper,也说了怎么用命令去添加节点了,那么我们现在就用程序去实现添加节点

1、首先引用依赖包

<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.6</version>
		</dependency>


2、创建一个Zookeeper连接Client类

/**
 * 
 *@author LK
 */
public class ZookeeperClient implements Watcher
{

	public ZooKeeper zookeeper;
	private static int SESSION_TIME_OUT = 2000;
	private CountDownLatch countDownLatch = new CountDownLatch(1);
	
	/**
	 * 连接zookeeper
	 * @param host
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public void connectZookeeper(String host) throws IOException, InterruptedException{
		zookeeper = new ZooKeeper(host, SESSION_TIME_OUT, this);
		countDownLatch.await();
		System.out.println("zookeeper connect ok");
	}
	
	/**
	 * 实现watcher的接口方法,当连接zookeeper成功后,zookeeper会通过此方法通知watcher
	 * 此处为如果接受到连接成功的event,则countDown,让当前线程继续其他事情。
	 */
	@Override
	public void process(WatchedEvent event) {
		if(event.getState() == KeeperState.SyncConnected){
			System.out.println("watcher receiver event");
			countDownLatch.countDown();
		}
	}
    
	/**
	 * 根据路径创建节点,并且设置节点数据
	 * @param path
	 * @param data
	 * @return
	 * @throws KeeperException
	 * @throws InterruptedException
	 */
	public String createNode(String path,byte[] data) throws KeeperException, InterruptedException{
		return this.zookeeper.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	}
	
	/**
	 * 根据路径获取所有孩子节点 
	 * @param path
	 * @return
	 * @throws KeeperException
	 * @throws InterruptedException
	 */
	public List<String> getChildren(String path) throws KeeperException, InterruptedException{
		return this.zookeeper.getChildren(path, false);
	}
	
	public Stat setData(String path,byte[] data,int version) throws KeeperException, InterruptedException{
		return this.zookeeper.setData(path, data, version);
	}
	
	/**
	 * 根据路径获取节点数据
	 * @param path
	 * @return
	 * @throws KeeperException
	 * @throws InterruptedException
	 */
	public byte[] getData(String path) throws KeeperException, InterruptedException{
		return this.zookeeper.getData(path, false, null);
	}
	
	/**
	 * 删除节点
	 * @param path
	 * @param version
	 * @throws InterruptedException
	 * @throws KeeperException
	 */
	public void deleteNode(String path,int version) throws InterruptedException, KeeperException{
		this.zookeeper.delete(path, version);
	}
	
	/**
	 * 关闭zookeeper连接
	 * @throws InterruptedException
	 */
	public void closeConnect() throws InterruptedException{
		if(null != zookeeper){
			zookeeper.close();
		}
	}
	 
}

3、测试客户端是否有效

private static final Logger log = LoggerFactory.getLogger(ZKClient.class);
	public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
		ZookeeperClient client = new ZookeeperClient();
		String host = "192.168.32.129:2181";

		// 连接zookeeper
		client.connectZookeeper(host);
		log.info("zookeeper连接成功!");

		// 创建节点
		byte[] data = { 1, 2, 3, 4, 5 };
		String result = client.createNode("/test", data);
		log.info(result + "节点创建成功!");

		// 获取某路径下所有节点
		List<String> children = client.getChildren("/");
		for (String child : children) {
			log.info(child);
		}
		log.info("成功获取child节点");

		// 获取节点数据
		byte[] nodeData = client.getData("/test");
		log.info(Arrays.toString(nodeData));
		log.info("成功获取节点数据!");

		// 更新节点数据
		data = "test data".getBytes();
		client.setData("/test", data, 0);
		log.info("成功更新节点数据!");
		nodeData = client.getData("/test");
		client.closeConnect();
	}


以上只是最简单的实例,如果要使用,还得好好完善才行。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐