https://www.jianshu.com/p/db65b64f38aa 简介 Curator是Netflix公司开源的一套zookeeper客户端框架,解决了很多Zookeeper客户端非常底层的细节开发工作,包括连接重连、反复注册Watcher和NodeExistsException异常等等。Patrixck Hunt(Zookeeper)以一句“Guava is to Java that Curator to Zookeeper”给Curator予高度评价。 Curator的maven依赖
[AppleScript] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
<
!
-- 对zookeeper的底层api的一些封装 -->
<
dependency
>
<
groupId
>
org.apache.curator
<
/
groupId
>
<
artifactId
>
curator
-
framework
<
/
artifactId
>
<
version
>
2.1
2.0
<
/
version
>
<
/
dependency
>
<
!
-- 封装了一些高级特性,如:Cache事件监听、选举、分布式锁、分布式Barrier -->
<
dependency
>
<
groupId
>
org.apache.curator
<
/
groupId
>
<
artifactId
>
curator
-
recipes
<
/
artifactId
>
<
version
>
2.1
2.0
<
/
version
>
<
/
dependency
>
基本API 创建会话 使用静态工程方法创建
[AppleScript] 纯文本查看 复制代码
?
1
2
3
RetryPolicy retryPolicy
=
new
ExponentialBackoffRetry
(
1000
,
3
)
;
CuratorFramework client
=
CuratorFrameworkFactory.newClient
(
"192.168.128.129:2181"
,
5000
,
5000
,
retryPolicy
)
;
其中RetryPolicy为重试策略,第一个参数为baseSleepTimeMs初始的sleep时间,用于计算之后的每次重试的sleep时间。第二个参数为maxRetries,最大重试次数。使用Fluent风格api创建
[AppleScript] 纯文本查看 复制代码
?
1
2
3
4
5
6
7
8
9
RetryPolicy retryPolicy
=
new
ExponentialBackoffRetry
(
1000
,
3
)
;
CuratorFramework client
=
CuratorFrameworkFactory.builder
(
)
.connectString
(
"192.168.128.129:2181"
)
.sessionTimeoutMs
(
5000
)
/
/
会话超时时间
.connectionTimeoutMs
(
5000
)
/
/
连接超时时间
.retryPolicy
(
retryPolicy
)
.namespace
(
"base"
)
/
/
包含隔离名称
.build
(
)
;
client.start
(
)
;
创建数据节点
[AppleScript] 纯文本查看 复制代码
?
1
2
3
client.create
(
)
.creatingParentContainersIfNeeded
(
)
/
/
递归创建所需父节点
.withMode
(
CreateMode.PERSISTENT
)
/
/
创建类型为持久节点
.forPath
(
"/nodeA"
,
"init"
.getBytes
(
)
)
;
/
/
目录及内容
删除数据节点
[AppleScript] 纯文本查看 复制代码
?
1
2
3
4
5
client.
delete
(
)
.guaranteed
(
)
/
/
强制保证删除
.deletingChildrenIfNeeded
(
)
/
/
递归删除子节点
.withVersion
(
10086
)
/
/
指定删除的版本号
.forPath
(
"/nodeA"
)
;
读取数据节点 读数据,返回值为byte[]
[AppleScript] 纯文本查看 复制代码
?
1
2
byte[] bytes
=
client.getData
(
)
.forPath
(
"/nodeA"
)
;
System.out.println
(
new
String
(
bytes
)
)
;
读stat
[AppleScript] 纯文本查看 复制代码
?
1
2
3
4
Stat stat
=
new
Stat
(
)
;
client.getData
(
)
.storingStatIn
(
stat
)
.forPath
(
"/nodeA"
)
;
修改数据节点
[AppleScript] 纯文本查看 复制代码
?
1
2
3
client.setData
(
)
.withVersion
(
10086
)
/
/
指定版本修改
.forPath
(
"/nodeA"
,
"data"
.getBytes
(
)
)
;
事务
[AppleScript] 纯文本查看 复制代码
?
1
2
3
4
5
6
7
client.inTransaction
(
)
.check
(
)
.forPath
(
"/nodeA"
)
.
and
(
)
.create
(
)
.withMode
(
CreateMode.EPHEMERAL
)
.forPath
(
"/nodeB"
,
"init"
.getBytes
(
)
)
.
and
(
)
.create
(
)
.withMode
(
CreateMode.EPHEMERAL
)
.forPath
(
"/nodeC"
,
"init"
.getBytes
(
)
)
.
and
(
)
.commit
(
)
;
其他
[AppleScript] 纯文本查看 复制代码
?
1
2
3
client.checkExists
(
)
/
/
检查是否存在
.forPath
(
"/nodeA"
)
;
client.getChildren
(
)
.forPath
(
"/nodeA"
)
;
/
/
获取子节点的路径
异步回调 异步创建节点
[AppleScript] 纯文本查看 复制代码
?
1
2
3
4
5
6
7
8
Executor executor
=
Executors.newFixedThreadPool
(
2
)
;
client.create
(
)
.creatingParentsIfNeeded
(
)
.withMode
(
CreateMode.EPHEMERAL
)
.inBackground
(
(
curatorFramework
,
curatorEvent
)
-
>
{
System.out.println
(
String.format
(
"eventType:%s,resultCode:%s"
,
curatorEvent.getType
(
)
,
curatorEvent.getResultCode
(
)
)
)
;
}
,
executor
)
.forPath
(
"path"
)
;
注: 本文参考了博客 https://zjcscut.github.io/2018/12/16/zookeeper-curator-usage/#Zookeeper%E5%AE%A2%E6%88%B7%E7%AB%AFCurator%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3