1.创建连接
public void test(){
// 1.第一种方式
// 连接策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
/**
* Create a new client
*
* @param connectString list of servers to connect to;zk server 地址和端口号
* @param sessionTimeoutMs session timeout 会话超时时间
* @param connectionTimeoutMs connection timeout 连接超时时间
* @param retryPolicy retry policy to use 重试策略
* @return client
*/
String connectString = "192.168.10.102:2181,192.168.10.103:2181,192.168.10.103:2181";
/*CuratorFramework client = CuratorFrameworkFactory.newClient(connectString,60*1000,15*1000,retryPolicy);
client.start();*/
// 2.第二种方式 推荐
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(connectString)
.sessionTimeoutMs(60 * 1000).connectionTimeoutMs(15 * 100)
.retryPolicy(retryPolicy)
.namespace("duck").build();
client.start();
}
2.创建节点
/**
* 创建节点:create 持久 临时 顺序
* 1. 基本创建
* 2. 创建节点 带有数据
* 3. 设置节点的类型
* 4. 创建多级节点
*/
@Test
public void testCreate() throws Exception {
// 1.创建基本节点
// 如果创建节点没有指定数据,则默认当前客户端的IP作为数据存储
String path = client.create().forPath("/app1");
System.out.println(path);
// 2.创建节点 带有数据
client.create().forPath("/app2","hahah".getBytes());
// 3.设置节点的类型
client.create().withMode(CreateMode.EPHEMERAL).forPath("/app3");
// 4.创建多级节点
// creatingParentsIfNeeded():如果父节点不存在,则创建父节点
client.create().creatingParentsIfNeeded().forPath("/app4/p1");
}
3.查询节点
/**
* 查询节点
* 1.查询节点:get
* 2.查询子节点:ls
* 3.查询节点状态信息:ls -s
*/
@Test
public void testGet() throws Exception {
// 1.查询节点
byte[] bytes = client.getData().forPath("/app1");
System.out.println(new String(bytes));
// 2. 查询子节点
List<String> path = client.getChildren().forPath("/app4");
System.out.println(path);
// 3.查询节点状态信息:ls -s
Stat stat = new Stat();
System.out.println(stat);
client.getData().storingStatIn(stat).forPath("/app1");
System.out.println(stat);
}
4.修改节点数据
/**
* 修改数据
* 1.修改数据
* 2.根据版本修改
* @throws Exception
*/
@Test
public void testUpdate() throws Exception {
// 1.修改数据
byte[] bytes = client.getData().forPath("/app1");
System.out.println(new String(bytes));
client.setData().forPath("/app1","test".getBytes());
byte[] bytes1 = client.getData().forPath("/app1");
System.out.println(new String(bytes1));
// 2.根据版本号修改
Stat stat = new Stat();
client.getData().storingStatIn(stat).forPath("/app1");
// version是查询出来的,目的是为了被让其他客户端或者线程所干扰
int version = stat.getVersion();
client.setData().withVersion(version).forPath("/app1","hehe".getBytes());
}
5.删除节点
/**
* 删除节点
* 1.删除单个节点
* 2.删除带有子节点的节点
* 3.必须成功的删除
* 4.回调
*/
@Test
public void testDelete() throws Exception {
// 1.删除单个节点
client.delete().forPath("/app1");
// 2.删除带有子节点的节点
client.delete().deletingChildrenIfNeeded().forPath("/app4");
// 3. 必须成功的删除
client.delete().guaranteed().forPath("/app2");
// 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("/app2");
}
Maven依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<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>
<!--日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>