ZooKeeper Java API简单示例

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介:         代码示例如下: import java.io.IOException;import org.apache.zookeeper.CreateMode;import org.

        代码示例如下:

import java.io.IOException;

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

public class TestZooKeeper {

	public static void main(String[] args) {

		ZooKeeper zk = null;
		try {

			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			
			System.out.println("开始连接ZooKeeper...");

			// 创建与ZooKeeper服务器的连接zk
			String address = "192.168.1.226:2181";
			int sessionTimeout = 3000;
			zk = new ZooKeeper(address, sessionTimeout, new Watcher() {
				// 监控所有被触发的事件
				public void process(WatchedEvent event) {
					if (event.getType() == null || "".equals(event.getType())) {
						return;
					}
					System.out.println("已经触发了" + event.getType() + "事件!");
				}
			});

			System.out.println("ZooKeeper连接创建成功!");

			// // 也可以通过register()方法后续注册Watcher
			// Watcher watcher = new Watcher() {
			//
			// @Override
			// public void process(WatchedEvent event) {
			// // TODO Auto-generated method stub
			// System.out.println("后续注册的:已经触发了" + event.getType() + "事件!");
			// }
			// };
			// zk.register(watcher);

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			// 创建根目录节点
			// 路径为/tmp_root_path
			// 节点内容为字符串"我是根目录/tmp_root_path"
			// 创建模式为CreateMode.PERSISTENT
			System.out.println("开始创建根目录节点/tmp_root_path...");
			zk.create("/tmp_root_path", "我是根目录/tmp_root_path".getBytes(),
					Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			System.out.println("根目录节点/tmp_root_path创建成功!");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			// 创建第一个子目录节点
			// 路径为/tmp_root_path/childPath1
			// 节点内容为字符串"我是第一个子目录/tmp_root_path/childPath1"
			// 创建模式为CreateMode.PERSISTENT
			System.out.println("开始创建第一个子目录节点/tmp_root_path/childPath1...");
			zk.create("/tmp_root_path/childPath1",
					"我是第一个子目录/tmp_root_path/childPath1".getBytes(),
					Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			System.out.println("第一个子目录节点/tmp_root_path/childPath1创建成功!");
			
			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			// 获取子目录节点列表
			System.out.println("开始获取根目录/tmp_root_path节点的子目录节点列...");
			System.out.println(zk.getChildren("/tmp_root_path", true));
			System.out.println("根目录/tmp_root_path节点的子目录节点列获取成功!");
			
			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			// 修改子目录节点数据
			System.out.println("开始修改第一个子目录节点/tmp_root_path/childPath1数据...");
			zk.setData("/tmp_root_path/childPath1",
					"我是修改数据后的第一个子目录/tmp_root_path/childPath1".getBytes(), -1);
			System.out.println("修改第一个子目录节点/tmp_root_path/childPath1数据成功!");
			
			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			// 获取根目录节点状态
			System.out.println("开始获取根目录节点状态...");
			System.out.println(zk.exists("/tmp_root_path", true));
			System.out.println("根目录节点状态获取成功");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			// 创建第二个子目录节点
			// 路径为/tmp_root_path/childPath2
			// 节点内容为字符串"我是第二个子目录/tmp_root_path/childPath2"
			// 创建模式为CreateMode.PERSISTENT
			System.out.println("开始创建第二个子目录节点/tmp_root_path/childPath2...");
			zk.create("/tmp_root_path/childPath2",
					"我是第二个子目录/tmp_root_path/childPath2".getBytes(),
					Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			System.out.println("第二个子目录节点/tmp_root_path/childPath2创建成功!");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			// 获取第二个子目录节点/tmp_root_path/childPath2节点数据
			System.out.println("开始获取第二个子目录节点/tmp_root_path/childPath2节点数据...");
			System.out.println(new String(zk.getData(
					"/tmp_root_path/childPath2", true, null)));
			System.out.println("第二个子目录节点/tmp_root_path/childPath2节点数据获取成功!");
			
			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			// 获取子目录节点列表
			System.out.println("开始获取根目录/tmp_root_path节点的子目录节点列...");
			System.out.println(zk.getChildren("/tmp_root_path", true));
			System.out.println("根目录/tmp_root_path节点的子目录节点列获取成功!");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			
			// 获取根目录节点状态
			System.out.println("开始获取根目录节点状态...");
			System.out.println(zk.exists("/tmp_root_path", true));
			System.out.println("根目录节点状态获取成功");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			// 删除第一个子目录节点
			System.out.println("开始删除第一个子目录节点/tmp_root_path/childPath1...");
			zk.delete("/tmp_root_path/childPath1", -1);
			System.out.println("第一个子目录节点/tmp_root_path/childPath1删除成功!");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			
			// 获取根目录节点状态
			System.out.println("开始获取根目录节点状态...");
			System.out.println(zk.exists("/tmp_root_path", true));
			System.out.println("根目录节点状态获取成功");
			
			Thread.currentThread().sleep(1000l);

			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			// 删除第二个子目录节点
			System.out.println("开始删除第二个子目录节点/tmp_root_path/childPath2...");
			zk.delete("/tmp_root_path/childPath2", -1);
			System.out.println("第二个子目录节点/tmp_root_path/childPath2删除成功!");
			
			Thread.currentThread().sleep(1000l);

			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			
			// 获取根目录节点状态
			System.out.println("开始获取根目录节点状态...");
			System.out.println(zk.exists("/tmp_root_path", true));
			System.out.println("根目录节点状态获取成功");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			// 删除根目录节点
			System.out.println("开始删除根目录节点/tmp_root_path...");
			zk.delete("/tmp_root_path", -1);
			System.out.println("根目录节点/tmp_root_path删除成功!");
			
			Thread.currentThread().sleep(1000l);

			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

			// 获取根目录节点状态
			System.out.println("开始获取根目录节点状态...");
			System.out.println(zk.exists("/tmp_root_path", true));
			System.out.println("根目录节点状态获取成功");

			Thread.currentThread().sleep(1000l);
			
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");
			System.out.println("...");

		} catch (IOException | KeeperException | InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 关闭连接
			if (zk != null) {
				try {
					zk.close();
					System.out.println("释放ZooKeeper连接成功!");

				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}
}
        输出结果如下:

...
...
...
...
开始连接ZooKeeper...
ZooKeeper连接创建成功!
已经触发了None事件!
...
...
...
...
开始创建根目录节点/tmp_root_path...
根目录节点/tmp_root_path创建成功!
...
...
...
...
开始创建第一个子目录节点/tmp_root_path/childPath1...
第一个子目录节点/tmp_root_path/childPath1创建成功!
...
...
...
...
开始获取根目录/tmp_root_path节点的子目录节点列...
[childPath1]
根目录/tmp_root_path节点的子目录节点列获取成功!
...
...
...
...
开始修改第一个子目录节点/tmp_root_path/childPath1数据...
修改第一个子目录节点/tmp_root_path/childPath1数据成功!
...
...
...
...
开始获取根目录节点状态...
2006956,2006956,1458958753172,1458958753172,0,1,0,0,29,1,2006957

根目录节点状态获取成功
...
...
...
...
开始创建第二个子目录节点/tmp_root_path/childPath2...
已经触发了NodeChildrenChanged事件!
第二个子目录节点/tmp_root_path/childPath2创建成功!
...
...
...
...
开始获取第二个子目录节点/tmp_root_path/childPath2节点数据...
我是第二个子目录/tmp_root_path/childPath2
第二个子目录节点/tmp_root_path/childPath2节点数据获取成功!
...
...
...
...
开始获取根目录/tmp_root_path节点的子目录节点列...
[childPath2, childPath1]
根目录/tmp_root_path节点的子目录节点列获取成功!
...
...
...
...
开始获取根目录节点状态...
2006956,2006956,1458958753172,1458958753172,0,2,0,0,29,2,2006959

根目录节点状态获取成功
...
...
...
...
开始删除第一个子目录节点/tmp_root_path/childPath1...
已经触发了NodeChildrenChanged事件!
第一个子目录节点/tmp_root_path/childPath1删除成功!
...
...
...
...
开始获取根目录节点状态...
2006956,2006956,1458958753172,1458958753172,0,3,0,0,29,1,2006960

根目录节点状态获取成功
...
...
...
...
开始删除第二个子目录节点/tmp_root_path/childPath2...
已经触发了NodeDeleted事件!
第二个子目录节点/tmp_root_path/childPath2删除成功!
...
...
...
...
开始获取根目录节点状态...
2006956,2006956,1458958753172,1458958753172,0,4,0,0,29,0,2006961

根目录节点状态获取成功
...
...
...
...
开始删除根目录节点/tmp_root_path...
已经触发了NodeDeleted事件!
根目录节点/tmp_root_path删除成功!
...
...
...
...
开始获取根目录节点状态...
null
根目录节点状态获取成功
...
...
...
...
释放ZooKeeper连接成功!
        至于为啥Watcher会监控那些,以后再做分析!


相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
相关文章
|
2天前
|
Java API
Java基础&API(3)
Java基础&API(3)
|
2天前
|
Java 机器人 API
Java基础&常用API(1)
Java基础&常用API(1)
|
2天前
|
安全 Java 程序员
|
6天前
|
Java API Apache
ZooKeeper【基础 03】Java 客户端 Apache Curator 基础 API 使用举例(含源代码)
【4月更文挑战第11天】ZooKeeper【基础 03】Java 客户端 Apache Curator 基础 API 使用举例(含源代码)
24 11
|
8天前
|
安全 Java API
java借助代理ip,解决访问api频繁导致ip被禁的问题
java借助代理ip,解决访问api频繁导致ip被禁的问题
|
3天前
|
安全 Java 调度
Java线程:深入理解与实战应用
Java线程:深入理解与实战应用
21 0
|
1天前
|
消息中间件 缓存 NoSQL
Java多线程实战-CompletableFuture异步编程优化查询接口响应速度
Java多线程实战-CompletableFuture异步编程优化查询接口响应速度
|
1天前
|
数据采集 存储 Java
高德地图爬虫实践:Java多线程并发处理策略
高德地图爬虫实践:Java多线程并发处理策略
|
2天前
|
缓存 Java
【Java基础】简说多线程(上)
【Java基础】简说多线程(上)
6 0