1 Maven导入
- 官网:Apache Curator –
- API文档:Overview (Apache Curator 5.2.1-SNAPSHOT API)
org.apache.curator
curator-framework
5.1.0
org.apache.curator
curator-recipes
5.1.0
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 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();