- ZooKeeper JavaAPI 操作
- 3.1 pom.xml引入
- 3.2 创建连接【命令:./zkCli.sh –server ip:port】
- 3.3 创建节点【命令:create /节点path】
- 3.4 创建节点加内容【命令:create /节点path value】
- 3.5创建节点类型 【命令:create -e /节点path】
- 3.6 创建多级节点 【命令:create /父节点path create /子节点path】
- 3.7 查询节点 【命令:get /节点path】
- 3.8 查询子节点【命令:ls】
- 3.9 查询节点状态信息 【命令:ls -s】
- 3.10 修改节点内容【命令:set /节点path value】
- 3.11 锁定节点后,进行修改节点【命令:stat /节点path [watch] set /节点path value [version]】
- 3.12 删除节点【命令:delete /节点path】
- 3.13 删除带有子节点的节点【命令:deleteall /节点path】
- 3.14 必须成功删除节点
- 3.15 删除成功后的回调方法
- 4 Zookeeper JavaAPI操作-Watch监听概述
ZooKeeper JavaAPI 操作
3.1 pom.xml引入
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!--curator-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.0</version>
</dependency>
3.2 创建连接【命令:./zkCli.sh –server ip:port】
@Test
public void testConnection() {
// 第一种方式
CuratorFramework newClient = CuratorFrameworkFactory.newClient("192.168.135.20:2181",
60 * 1000, 15 * 1000,
new ExponentialBackoffRetry(3000, 10));
// 第二种方式
CuratorFramework client = CuratorFrameworkFactory.builder().
connectString("192.168.135.20:2181") // 服务端
.sessionTimeoutMs(60 * 1000) // session失效时间
.connectionTimeoutMs(15 * 1000) // 连接最大时间
.retryPolicy(new ExponentialBackoffRetry(3000, 10)) // 重试策略
.namespace("hikktn") // 客户端名称
.build();
// 开启连接
client.start();
}
3.3 创建节点【命令:create /节点path】
@Test
public void testCreate(){
try {
// 如果没有指定数据,默认将客户端ip作为节点内容
String path = client.create().forPath("/app1");
System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
}
3.4 创建节点加内容【命令:create /节点path value】
@Test
public void testCreate2(){
try {
// 创建节点和内容
String path = client.create().forPath("/app1","hikktn2".getBytes());
System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
}
3.5创建节点类型 【命令:create -e /节点path】
(持久化节点,临时节点,顺序节点)
@Test
public void testCreate3(){
try {
// 设置节点类型 -- CreateMode 枚举类,选择什么关键字就是什么类型节点,持久化节点,临时节点
String path = client.create().withMode(CreateMode.EPHEMERAL).forPath("/app1");
System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
}
3.6 创建多级节点 【命令:create /父节点path create /子节点path】
可以不再像命令执行一样,先创建父节点,再来创建子节点
@Test
public void testCreate4(){
try {
// 创建多级节点
// 如果父节点不存在,则创建父节点
String path = client.create().creatingParentsIfNeeded().forPath("/app1/h1");
System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
}
3.7 查询节点 【命令:get /节点path】
@Test
public void testGet1(){
try {
// 查询节点 get
byte[] bytes = client.getData().forPath("/app1");
System.out.println(new String(bytes));
} catch (Exception e) {
e.printStackTrace();
}
}
3.8 查询子节点【命令:ls】
@Test
public void testGet2(){
try {
// 查询子节点 ls
List<String> list = client.getChildren().forPath("/app1");
System.out.println(list);
} catch (Exception e) {
e.printStackTrace();
}
}
3.9 查询节点状态信息 【命令:ls -s】
@Test
public void testGet3(){
try {
// 查询节点状态信息 ls -s
Stat stat =new Stat();
byte[] bytes = client.getData().storingStatIn(stat).forPath("/app1");
System.out.println(new String(bytes));
} catch (Exception e) {
e.printStackTrace();
}
}
3.10 修改节点内容【命令:set /节点path value】
@Test
public void testSet1(){
try {
Stat stat = client.setData().forPath("/app1", "hehe".getBytes());
System.out.println(stat);
} catch (Exception e) {
e.printStackTrace();
}
}
3.11 锁定节点后,进行修改节点【命令:stat /节点path [watch] set /节点path value [version]】
@Test
public void testSet2(){
try {
Stat status = new Stat();
//3. 查询节点状态信息:ls -s
client.getData().storingStatIn(status).forPath("/app1");
int version = status.getVersion();//查询出来的 3
System.out.println(version);
client.setData().withVersion(version).forPath("/app1", "hehe".getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
3.12 删除节点【命令:delete /节点path】
@Test
public void testDelete() throws Exception {
// 1. 删除单个节点
client.delete().forPath("/app1");
}
3.13 删除带有子节点的节点【命令:deleteall /节点path】
@Test
public void testDelete2() throws Exception {
//2. 删除带有子节点的节点
client.delete().deletingChildrenIfNeeded().forPath("/app4");
}
3.14 必须成功删除节点
@Test
public void testDelete3() throws Exception {
//3. 必须成功的删除
client.delete().guaranteed().forPath("/app2");
}
3.15 删除成功后的回调方法
@Test
public void testDelete4() throws Exception {
//4. 回调
client.delete().guaranteed().inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
System.out.println("我被删除了~");
System.out.println(event);
}
}).forPath("/app1");
}
4 Zookeeper JavaAPI操作-Watch监听概述
•NodeCache : 只是监听某一个特定的节点
•PathChildrenCache : 监控一个ZNode的子节点.
•TreeCache : 可以监控整个树上的所有节点,类似于PathChildrenCache和NodeCache的组合
4.1 监听一个节点注册,当父节点新增父节点的情况,附带一些操作
/**
* 演示 NodeCache:给指定一个节点注册监听器
*/
@Test
public void testNodeCache() throws Exception {
//1. 创建NodeCache对象
final NodeCache nodeCache = new NodeCache(client, "/app1");
//2. 注册监听
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println("节点变化了~");
//获取修改节点后的数据
byte[] data = nodeCache.getCurrentData().getData();
System.out.println(new String(data));
}
});
//3. 开启监听.如果设置为true,则开启监听是,加载缓冲数据
nodeCache.start(true);
while (true) {
}
}
4.2 监听一个节点注册,监听父节点下的所有子节点变化
@Test
public void testPathChildrenCache() throws Exception {
//1.创建监听对象
PathChildrenCache pathChildrenCache = new PathChildrenCache(client, "/app2", true);
//2. 绑定监听器
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
System.out.println("子节点变化了~");
System.out.println(event);
//监听子节点的数据变更,并且拿到变更后的数据
//1.获取类型
PathChildrenCacheEvent.Type type = event.getType();
//2.判断类型是否是update
if (type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)) {
System.out.println("数据变了!!!");
byte[] data = event.getData().getData();
System.out.println(new String(data));
}
}
});
//3. 开启
pathChildrenCache.start();
while (true) {
}
}
4.3 监听一个节点注册,包括父节点和父节点下所有子节点变化
/**
* 演示 TreeCache:监听某个节点自己和所有子节点们
*/
@Test
public void testTreeCache() throws Exception {
//1. 创建监听器
TreeCache treeCache = new TreeCache(client, "/app2");
//2. 注册监听
treeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
System.out.println("节点变化了");
System.out.println(event);
}
});
//3. 开启
treeCache.start();
while (true) {
}
}