1 Maven导入
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
2 基本操作
2.1 连接客户端
//3000为超时时间(单位:毫秒),10为最大重试次数
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
//-------------------第一种方式------------------------
//参数1:服务端地址+端口号,参数2:会话超时时间,参数3:连接超时时间
CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.170.148:2181",
60 * 1000, 15 * 1000, retryPolicy);
//-------------------第二种方式------------------------
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("192.168.170.148:2181")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy).build();
//开启连接
client.start();
2.2 创建节点
//1、基本创建:默认存储当前客户端的ip地址
String path = curatorFramework.create().forPath("/app3");
System.out.println(path);
//2、创建临时节点
String path2 = curatorFramework.create().withMode(CreateMode.EPHEMERAL).forPath("/app4");
System.out.println(path2);
//3、创建多级节点
String path3 = curatorFramework.create().creatingParentsIfNeeded().forPath("/app5/p1");
System.out.println(path3);
2.3 查询节点
//1、查询数据:get
byte[] data = curatorFramework.getData().forPath("/app1");
System.out.println(new String(data));
//2、查询子节点:ls
List<String> list = curatorFramework.getChildren().forPath("/");
System.out.println(list);
//3、查询节点状态信息:ls -s
Stat stat = new Stat();
curatorFramework.getData().storingStatIn(stat).forPath("/app1");
System.out.println(stat);
2.4 修改节点
//1、修改节点数据(基本修改)
curatorFramework.setData().forPath("/app1", "333".getBytes(StandardCharsets.UTF_8));
//2、根据版本号修改
Stat stat1 = new Stat();
curatorFramework.getData().storingStatIn(stat1).forPath("/app1");
curatorFramework.setData().withVersion(stat1.getVersion()).forPath("/app1", "itcast".getBytes(StandardCharsets.UTF_8));
2.5 删除节点
//1、删除单个节点
curatorFramework.delete().forPath("/app1");
//2、删除带有子节点的节点
curatorFramework.delete().deletingChildrenIfNeeded().forPath("/app1");
//3、必须成功地删除,删除不成功会持续重试
curatorFramework.delete().guaranteed().forPath("/app1");
//4、删除后回调
curatorFramework.delete().inBackground((curatorFramework1, curatorEvent) -> {
System.out.println(curatorFramework1);
System.out.println(curatorEvent);
}).forPath("/app1");
2.6 监听节点
2.6.1 监听单个节点
CuratorCache curatorCache = CuratorCache.build(client, "/test1");
curatorCache.listenable().addListener((type, childData, childData1) -> {
System.out.println("--------------------------");
//变化类型
System.out.println(type);
System.out.println("--------------------------");
//变化前节点
System.out.println(childData);
System.out.println(new String(childData.getData()));
System.out.println("--------------------------");
//变化后节点
System.out.println(childData1);
System.out.println(new String(childData1.getData()));
System.out.println("--------------------------");
});
curatorCache.start();
2.6.2 监听所有子节点
CuratorCache curatorCache = CuratorCache.build(client, "/test1");
CuratorCacheListener curatorCacheListener = CuratorCacheListener
.builder()
.forPathChildrenCache("/test1", client, (curatorFramework, pathChildrenCacheEvent) -> {
System.out.println("子节点变化了~");
//监听子节点的数据变更,并且拿到变更后的数据
//1.获取类型
PathChildrenCacheEvent.Type type = pathChildrenCacheEvent.getType();
System.out.println(type);
//2.判断类型是否是update
if(type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){
System.out.println("数据变了!!!");
byte[] data = pathChildrenCacheEvent.getData().getData();
System.out.println(new String(data));
}
}).build();
curatorCache.listenable().addListener(curatorCacheListener);
curatorCache.start();
2.6.3 监听节点树
CuratorCache curatorCache = CuratorCache.build(client, "/");
CuratorCacheListener curatorCacheListener = CuratorCacheListener
.builder()
.forTreeCache(client, (curatorFramework, treeCacheEvent) -> {
System.out.println("节点变化了");
System.out.println(treeCacheEvent);
System.out.println(new String(treeCacheEvent.getData().getData()));
}).build();
curatorCache.listenable().addListener(curatorCacheListener);
curatorCache.start();
2.6.3 5.0以前版本
//----------------- 监听单个节点 -----------------------------------
//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);
//----------------- 监听子节点 -----------------------------------
//1.创建监听对象
PathChildrenCache pathChildrenCache = new PathChildrenCache(client,"/app2",true);
//2. 绑定监听器
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) {
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();
//----------------- 监听节点树 -----------------------------------
//1. 创建监听器
TreeCache treeCache = new TreeCache(client,"/app2");
//2. 注册监听
treeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) {
System.out.println("节点变化了");
System.out.println(event);
}
});
//3. 开启
treeCache.start();
网友评论