1.新建一个maven项目,导入如下依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cd</groupId>
<artifactId>maven-zookeeper</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.7</version>
</dependency>
<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>4.2.0</version>
</dependency>
</dependencies>
</project>
2.使用Curator框架编写如下常用代码
public class CuratorTest {
private CuratorFramework client;
@Before
public void testConnect(){
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
client = CuratorFrameworkFactory.builder().connectString("192.168.57.139:2181")
.sessionTimeoutMs(60 * 1000)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy).namespace("cdbaba").build();
client.start();
}
@Test
public void testCreate() throws Exception {
String path = client.create().forPath("/app1");
System.out.println(path);
}
@Test
public void testCreate2() throws Exception {
String path = client.create().forPath("/app2","haha".getBytes());
System.out.println(path);
}
@Test
public void testCreate3() throws Exception {
String path = client.create().withMode(CreateMode.EPHEMERAL).forPath("/app3","haha".getBytes());
System.out.println(path);
}
@Test
public void testCreate4() throws Exception {
String path = client.create().creatingParentsIfNeeded().forPath("/app4/p1");
System.out.println(path);
}
@Test
public void testGet1() throws Exception {
byte[] data = client.getData().forPath("/app1");
System.out.println(new String(data));
}
@Test
public void testGet2() throws Exception {
List<String> data = client.getChildren().forPath("/");
System.out.println(data);
}
@Test
public void testGet3() throws Exception {
Stat stat = new Stat();
System.out.println(stat);
client.getData().storingStatIn(stat).forPath("/app1");
System.out.println(stat);
}
@Test
public void testSet() throws Exception {
client.setData().forPath("/app1","itcast".getBytes());
}
@Test
public void testSetForVersion() throws Exception {
Stat stat = new Stat();
client.getData().storingStatIn(stat).forPath("/app1");
int version = stat.getVersion();
System.out.println(version);
client.setData().withVersion(version).forPath("/app1","hehe".getBytes());
}
@Test
public void testDelete() throws Exception {
client.delete().forPath("/app1");
}
@Test
public void testDelete2() throws Exception {
client.delete().deletingChildrenIfNeeded().forPath("/app4");
}
@Test
public void testDelete3() throws Exception {
client.delete().guaranteed().forPath("/app2");
}
@Test
public void testDelete4() throws Exception {
client.delete().guaranteed().inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
System.out.println("我被删除了");
System.out.println(curatorEvent);
}
}).forPath("/app2");
}
|