zookeeper的curd
package com.zookeeper.demo;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryOneTime;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
/**
* Created by chenli on 2019/11/24.
*/
public class ZookeeperApiDemo {
public final static String CONNECTION_STR="192.168.133.129:12181,192.168.133.130:12181,192.168.133.131:12181";
public static void main(String[] args) throws Exception {
CuratorFramework curatorFramework= CuratorFrameworkFactory.builder().connectString(CONNECTION_STR)
.sessionTimeoutMs(5000).
retryPolicy(new RetryOneTime(1)).build();
curatorFramework.start();
//create(curatorFramework);
// update(curatorFramework);
//select(curatorFramework);
delete(curatorFramework);
}
//删除节点
private static void delete(CuratorFramework curatorFramework) throws Exception {
Stat stat=new Stat();
String value=new String(curatorFramework.getData().storingStatIn(stat).forPath("/data/curd"));
System.out.println(stat.getAversion());
curatorFramework.delete().withVersion(stat.getVersion()).forPath("/data/curd");
}
//查询节点
private static void select(CuratorFramework curatorFramework) throws Exception {
System.out.println(new String(curatorFramework.getData().forPath("/data/curd")));
}
//更新节点
private static void update(CuratorFramework curatorFramework) throws Exception {
curatorFramework.setData().forPath("/data/curd","sb".getBytes());
}
//创建节点
private static void create(CuratorFramework curatorFramework) throws Exception {
//CreateMode.PERSISTENT 创建一个持久化节点
curatorFramework.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT)
.forPath("/data/curd","create".getBytes());
}
}
pom依赖
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.11.0</version>
</dependency>