import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class CuratorTest {
private CuratorFramework client;
@Before
public void testConnection(){
//第一种方式
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//CuratorFramework client = CuratorFrameworkFactory.newClient("198.168.149.135:2181", 60 * 1000, 15 * 1000, retryPolicy);
//client.start();
//第二种方式
client = (CuratorFramework) CuratorFrameworkFactory.builder().connectString("198.168.149.135:2181")
.sessionTimeoutMs(60 * 1000)
.retryPolicy(retryPolicy).namespace("itheima").build();
client.start();
}
/**
* 创建节点:create 持久 临时 顺序 数据
* 1.基本创建
* 2.创建节点 带有数据
* 3.设置节点类型
* 4.创建多级节点
*
* */
@Test
public void testCreat() throws Exception {
//1.基本创建
String s = client.create().forPath("/app1");
System.out.println(s);
}
@Test
public void testCreat2() throws Exception {
//2.创建节点 带有数据
String s = client.create().forPath("/app2","hehe".getBytes());
System.out.println(s);
}
@Test
public void testCreat3() throws Exception {
//3.设置节点类型 默认类型:持久化
String s = client.create().withMode(CreateMode.EPHEMERAL).forPath("/app3");
System.out.println(s);
}
@Test
public void testCreat4() throws Exception {
//4.创建多级节点
//creatingParentsIfNeeded 如果父节点不存在就创建父节点
String s = client.create().creatingParentsIfNeeded().forPath("/app4/p1");
System.out.println(s);
}
@After
public void close(){
if (client!=null){
client.close();
}
}
}