Curator框架使用链式编程风格,易读性更强,使用工厂方法创建连接对象。
1.使用CuratorFrameworkFactory的两个静态工厂方法(参数不同)来实现
参数1:connectString,连接信息
参数2:RetryPolicy,重试连接策略,有四种实现
ExponentialBackoffRetry、RetryNTimes、RetryOneTimes、RetryUntilElapsed
参数3:sessionTimeoutMs会话超时时间,默认为60s
参数4:connectionTimeoutMs连接超时时间,默认为15s
注意:对于retryPolicy策略通过一个接口来让用户自定义实现
1 import java.util.List; 2 import java.util.concurrent.ExecutorService; 3 import java.util.concurrent.Executors; 4 5 import org.apache.curator.RetryPolicy; 6 import org.apache.curator.framework.CuratorFramework; 7 import org.apache.curator.framework.CuratorFrameworkFactory; 8 import org.apache.curator.framework.api.BackgroundCallback; 9 import org.apache.curator.framework.api.CuratorEvent; 10 import org.apache.curator.retry.ExponentialBackoffRetry; 11 import org.apache.zookeeper.CreateMode; 12 import org.apache.zookeeper.ZooKeeper.States; 13 import org.apache.zookeeper.data.Stat; 14 15 public class CuratorBase { 16 17 /** zookeeper地址 */ 18 static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; 19 /** session超时时间 */ 20 static final int SESSION_OUTTIME = 5000;//ms 21 22 public static void main(String[] args) throws Exception { 23 24 //1 重试策略:初试时间为1s 重试10次 25 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); 26 //2 通过工厂创建连接 27 CuratorFramework cf = CuratorFrameworkFactory.builder() 28 .connectString(CONNECT_ADDR) 29 .sessionTimeoutMs(SESSION_OUTTIME) 30 .retryPolicy(retryPolicy) 31 // .namespace("super") 32 .build(); 33 //3 开启连接 34 cf.start(); 35 36 // System.out.println(States.CONNECTED); 37 // System.out.println(cf.getState()); 38 39 // 新加、删除 40 /** 41 //4 建立节点 指定节点类型(不加withMode默认为持久类型节点)、路径、数据内容 42 cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c1","c1内容".getBytes()); 43 //5 删除节点 44 cf.delete().guaranteed().deletingChildrenIfNeeded().forPath("/super"); 45 */ 46 47 // 读取、修改 48 /** 49 //创建节点 50 // cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c1","c1内容".getBytes()); 51 // cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c2","c2内容".getBytes()); 52 //读取节点 53 // String ret1 = new String(cf.getData().forPath("/super/c2")); 54 // System.out.println(ret1); 55 //修改节点 56 // cf.setData().forPath("/super/c2", "修改c2内容".getBytes()); 57 // String ret2 = new String(cf.getData().forPath("/super/c2")); 58 // System.out.println(ret2); 59 */ 60 61 // 绑定回调函数 62 /** 63 ExecutorService pool = Executors.newCachedThreadPool(); 64 cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT) 65 .inBackground(new BackgroundCallback() { 66 @Override 67 public void processResult(CuratorFramework cf, CuratorEvent ce) throws Exception { 68 System.out.println("code:" + ce.getResultCode()); 69 System.out.println("type:" + ce.getType()); 70 System.out.println("线程为:" + Thread.currentThread().getName()); 71 } 72 }, pool) 73 .forPath("/super/c3","c3内容".getBytes()); 74 Thread.sleep(Integer.MAX_VALUE); 75 */ 76 77 78 // 读取子节点getChildren方法 和 判断节点是否存在checkExists方法 79 /** 80 List<String> list = cf.getChildren().forPath("/super"); 81 for(String p : list){ 82 System.out.println(p); 83 } 84 85 Stat stat = cf.checkExists().forPath("/super/c3"); 86 System.out.println(stat); 87 88 Thread.sleep(2000); 89 cf.delete().guaranteed().deletingChildrenIfNeeded().forPath("/super"); 90 */ 91 92 93 //cf.delete().guaranteed().deletingChildrenIfNeeded().forPath("/super"); 94 95 } 96 }