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实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
相关文章
|
13天前
|
安全 Java 大数据
|
10天前
|
设计模式 Java API
Java 可扩展 API 设计:打造灵活的应用架构
【4月更文挑战第27天】设计可扩展的 API 是构建灵活、易于维护的应用程序架构的关键。Java 提供了丰富的工具和技术来实现这一目标,使开发者能够构建具有高度可扩展性的应用程序。
31 4
|
6天前
|
分布式计算 Java API
Java 8新特性之Lambda表达式与Stream API
【5月更文挑战第1天】本文将介绍Java 8中的两个重要特性:Lambda表达式和Stream API。Lambda表达式是一种新的函数式编程语法,可以简化代码并提高可读性。Stream API是一种用于处理集合的新工具,可以方便地进行数据操作和转换。通过结合Lambda表达式和Stream API,我们可以更加简洁高效地编写Java代码。
|
7天前
|
Java
简单的 Java 计算器示例
这是一个Java计算器程序,接收用户输入的两个数字和一个运算符(+,-,*,/),通过`Scanner`获取输入,使用`switch`进行计算。当运算符为除法时,检查除数是否为0,防止除零错误。最后,它打印出计算结果。
12 0
|
10天前
|
Java
Java 事件驱动编程:概念、优势与实战示例
【4月更文挑战第27天】事件驱动编程是一种编程范式,其中程序的执行流程由外部事件的发生而触发或驱动。
19 0
|
10天前
|
Java 程序员
Java 异步编程:概念、优势与实战示例
【4月更文挑战第27天】在现代软件开发中,异步编程是一种重要的编程范式,特别适用于处理长时间运行的任务,如网络通信、文件操作等。
19 0
|
10天前
|
Java 数据处理 API
Java 函数式编程:概念、优势与实战示例
【4月更文挑战第27天】函数式编程(Functional Programming,简称 FP)是一种编程范式,它将计算视为数学函数的求值并避免使用程序状态以及可变数据。
17 1
|
11天前
|
Java API 开发者
【专栏】Java 8新特性之Stream API详解
【4月更文挑战第27天】Java 8的Stream API是处理集合数据的新方式,强调简洁和声明式编程。它基于延迟执行和惰性求值,提供创建、中间操作(如filter、map)和终端操作(如forEach、collect)。示例展示了如何通过Stream排序、过滤、映射和聚合数据。
|
12天前
|
存储 SQL Java
Java8 Stream API 详解:流式编程进行数据处理
Java8 Stream API 详解:流式编程进行数据处理
|
安全 JavaScript 前端开发
java基础示例
这篇文章将为大家详细讲解有关java基础之方法的示例分析