Apache Curator
Apache Curator 是Apache ZooKeeper 的Java 客户端库。Curator 项目的目标是简化ZooKeeper 客户端的使用。例如,在以前的代码展示中,我们都要自己处理
ConnectionLossException 。另外Curator 为常见的分布式协同服务提供了高质量的实现。Apache Curator 最初是Netflix 研发的,后来捐献给了Apache 基金会,目前是Apache 的顶级项目。
Curator 技术栈
Client:封装了 Zookeeper 类,管理和Zookeeper集群的连接,并提供了重建连接机制。
Framework:为所有的Zookeeper操作提供了重试机制,对提供了一个Fluent风格的API。
Recipes:使用framework实现了大量 Zookeeper 协同服务。
Extensions:扩展模块
Client 的创建,初始化一个client分为两步骤:
- 创建client
- 启动client
//创建 client
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);// 使用 Factory 方法
CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, retryPolicy);
// Fluent 风格
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(connectString).retryPolicy(retryPolicy).build();
//启动 client
client.start();
Fluent 风格API
// 同步版本
client.create().withMode(CreateMode.PERSISTENT).forPath(path, data);
// 异步版本
client.create().withMode(CreateMode.PERSISTENT).inBackground().forPath(path, data);
// 使用watch
client.getData().watched().forPath(path);
案例:
public class TestCurator {private CuratorFramework client = null;private String connectString = "192.168.1.100:2181";private RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);@Beforepublic void creatClient() {client = CuratorFrameworkFactory.builder().connectString(connectString).retryPolicy(retryPolicy).build();client.start();}@Testpublic void testSyncOp() throws Exception {String path = "/one";byte[] data = "testSyncOp".getBytes();client.create().withMode(CreateMode.PERSISTENT).forPath(path,data);byte[] actualData = client.getData().forPath(path);System.out.println(new String(actualData));client.delete().forPath(path);client.close();}@Testpublic void testAsyncOp() throws Exception {String path = "/two";final byte[] data = "testAsyncOp".getBytes();final CountDownLatch latch = new CountDownLatch(1);client.getCuratorListenable().addListener((CuratorFramework c, CuratorEvent event) -> {switch (event.getType()) {case CREATE:System.out.printf("znode '%s' created\n", event.getPath());c.getData().inBackground().forPath(event.getPath());break;case GET_DATA:System.out.printf("got the data of znode '%s'\n", event.getPath());System.out.println(new String(event.getData()));c.delete().inBackground().forPath(path);break;case DELETE:System.out.printf("znode '%s' deleted.\n", event.getPath());latch.countDown();break;}});client.create().withMode(CreateMode.PERSISTENT).inBackground().forPath(path, data);latch.await();client.close();}@Testpublic void testWatch() throws Exception {String path = "/three";byte[] data = {'3'};byte[] newData = {'4'};CountDownLatch latch = new CountDownLatch(1);client.getCuratorListenable().addListener(((CuratorFramework c, CuratorEvent event ) -> {switch (event.getType()) {case WATCHED:WatchedEvent we = event.getWatchedEvent();System.out.println("watched event:" + we);if (we.getType() == Watcher.Event.EventType.NodeDataChanged&& we.getPath().equals(path)) {System.out.println("got the event for the triggered watch");byte[] actualData = c.getData().forPath(path);System.out.println(new String(actualData));}latch.countDown();break;}}));client.create().withMode(CreateMode.PERSISTENT).forPath(path, data);byte[] actualData = client.getData().watched().forPath(path);System.out.println(new String(actualData));client.setData().forPath(path, newData);latch.await();client.delete().forPath(path);}@Testpublic void testCallbackAndWatch() throws Exception {String path = "/four";byte[] data = {'4'};byte[] newData = {'5'};CountDownLatch latch = new CountDownLatch(3);client.getCuratorListenable().addListener(((CuratorFramework c, CuratorEvent event ) -> {switch (event.getType()) {case CREATE:System.out.printf("znode '%s' created\n", event.getPath());System.out.println(new String(client.getData().watched().forPath(path)));client.setData().forPath(path, newData);latch.countDown();break;case WATCHED:WatchedEvent we = event.getWatchedEvent();System.out.println("watched event:" + we);if (we.getType() == Watcher.Event.EventType.NodeDataChanged&& we.getPath().equals(path)) {System.out.println("got the event for the triggered watch");byte[] actualData = c.getData().forPath(path);System.out.println(new String(actualData));}latch.countDown();break;}}));client.create().withMode(CreateMode.PERSISTENT).inBackground().forPath(path, data);latch.await();client.delete().forPath(path);}
}
参考链接:https://time.geekbang.org/course/intro/100034201
Apache Curator相关推荐
- Apache ZooKeeper - 使用Apache Curator操作ZK
文章目录 原生ZK API VS Curator Curator 概述 Maven依赖 会话创建 静态工厂方式创建会话 使用 fluent 风格创建会话 创建节点 protection 模式 ,规避僵 ...
- 【zookeeper】Apache curator优点介绍
文章目录 1. 简介 2. 项目组件 2.1 版本 2.2 项目组件 2.3 Maven依赖 3. 案例及功能说明 3.1 创建会话 3.1.1 重试策略 3.1.2 创建节点 3.1.3 删除节点 ...
- Apache Curator操作zookeeper的API使用
curator简介与客户端之间的异同点 常用的zookeeper java客户端: zookeeper原生Java API zkclient Apache curator ZooKeeper原生Jav ...
- 基于Apache Curator框架的ZooKeeper基本用法详解
简介 Apache Curator是一个比较完善的ZooKeeper客户端框架,通过封装的一套高级API 简化了ZooKeeper的操作.通过查看官方文档,可以发现Curator主要解决了三类问题: ...
- Apache Curator客户端的使用
当前已有的三种API客户端 zk原生API :不支持超时自动重连,不支持节点递归创建 zkclient:无文档,异常处理弱爆了(简单的抛出RuntimeException) Apache Curato ...
- Apache Curator实战
Apache Curator入门实战 Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,简化了Zookeep ...
- Dubbo 启动报java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy
1.## Dubbo 启动报java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy 2. Failed to subscribe ...
- Caused by: java.lang.NoClassDefFoundError: org/apache/curator/framework/CuratorFrameworkFactory
springboot整合dubbo,apache的报错: 添加依赖: <dependency><groupId>org.apache.curator</groupId&g ...
- Zookeeper 原生客户端、可视化工具 ZooInspector 、Apache Curator
目录 Zookeeper 原生客户端 Apache Curator 开源客户端 可视化客户端工具 ZooInspector Zookeeper 原生客户端 1.类似 Redis 有多种 Java 客户 ...
最新文章
- python【力扣LeetCode算法题库】69-x 的平方根
- css scale 缩放基准点
- BZOJ 3685 普通van Emde Boas树 权值线段树(zkw)
- 分区字段必须包含在主键字段_十年Java开发经验分享:24 个必须掌握的数据库面试问题...
- Spring Cloud云架构-Restful 基础架构
- 一些Iphone sqlite 的包装类
- Charles使用备注[1]
- 2015-02-10
- 专利附图绘制-VISIO线条图绘制入门
- 匹配滤波器的MATLAB实现
- python 力扣(LeetCode) 1818.绝对差值和
- matlab人机交互界面设计
- 数理统计——样本分布
- OPENWRT-LUCI开发总结-LUCI添加新页面总结
- 四阶段课堂总结解决问题
- 详解 GloVe 的原理和应用
- LYOI2018 Hzy's Planets
- Leetcode--Java--340. 至多包含 K 个不同字符的最长子串
- ramfs, rootfsinitramfs
- 网络IO Socket