package com.huey.dream.demo;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;/** * 使用Curator創(chuàng)建會話 * @author huey * @version 1.0 * @created 2015-3-1 */public class CarutorDemo { public static void main(String[] args) throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient( "192.168.1.109:2181", // 服務器列表 5000, // 會話超時時間,單位毫秒 3000, // 連接創(chuàng)建超時時間,單位毫秒 new ExponentialBackoffRetry(1000, 3) // 重試策略 ); client.start(); client.close(); } }2.使用鏈式風格的API接口創(chuàng)建會話package com.huey.dream.demo;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;/** * 使用鏈式風格的API接口創(chuàng)建會話 * @author huey * @version 1.0 * @created 2015-3-1 */public class CarutorDemo { public static void main(String[] args) throws Exception { CuratorFramework client = CuratorFrameworkFactory.builder() .connectString("192.168.1.109:2181") .sessionTimeoutMs(5000) .connectionTimeoutMs(3000) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .build(); client.start(); client.close(); } }二、創(chuàng)建節(jié)點package com.huey.dream.demo;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;/** * 使用Curator創(chuàng)建節(jié)點 * @author huey * @version 1.0 * @created 2015-3-1 */public class CarutorDemo { public static void main(String[] args) throws Exception { CuratorFramework client = CuratorFrameworkFactory.builder() .connectString("192.168.1.109:2181") .sessionTimeoutMs(5000) .connectionTimeoutMs(3000) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .build(); client.start(); client.create() .creatingParentsIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath("/zk-huey/cnode", "hello".getBytes()); client.close(); } }三、刪除節(jié)點package com.huey.dream.demo;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;/** * 使用Curator刪除節(jié)點 * @author huey * @version 1.0 * @created 2015-3-1 */public class CarutorDemo { public static void main(String[] args) throws Exception { CuratorFramework client = CuratorFrameworkFactory.builder() .connectString("192.168.1.109:2181") .sessionTimeoutMs(5000) .connectionTimeoutMs(3000) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .build(); client.start(); client.create() .creatingParentsIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath("/zk-huey/cnode", "hello".getBytes()); client.delete() .guaranteed() .deletingChildrenIfNeeded() .withVersion(-1) .forPath("/zk-huey"); client.close(); } }四、讀取節(jié)點數(shù)據(jù)package com.huey.dream.demo;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.apache.zookeeper.data.Stat;/** * 使用Curator讀取節(jié)點數(shù)據(jù) * @author huey * @version 1.0 * @created 2015-3-1 */public class CarutorDemo { public static void main(String[] args) throws Exception { CuratorFramework client = CuratorFrameworkFactory.builder() .connectString("192.168.1.109:2181") .sessionTimeoutMs(5000) .connectionTimeoutMs(3000) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .build(); client.start(); client.create() .creatingParentsIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath("/zk-huey/cnode", "hello".getBytes()); Stat stat = new Stat(); byte[] nodeData = client.getData() .storingStatIn(stat) .forPath("/zk-huey/cnode"); System.out.五、更新節(jié)點數(shù)據(jù)package com.huey.dream.demo;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.apache.zookeeper.data.Stat;/** * 使用Curator更新節(jié)點數(shù)據(jù) * @author huey * @version 1.0 * @created 2015-3-1 */public class CarutorDemo { public static void main(String[] args) throws Exception { CuratorFramework client = CuratorFrameworkFactory.builder() .connectString("192.168.1.109:2181") .sessionTimeoutMs(5000) .connectionTimeoutMs(3000) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .build(); client.start(); client.create() .creatingParentsIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath("/zk-huey/cnode", "hello".getBytes()); client.setData() .withVersion(-1) .forPath("/zk-huey/cnode", "world".getBytes()); client.close(); } }六、 獲取子節(jié)點列表package com.huey.dream.demo;import java.util.List;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;/** * 使用Curator * @author huey * @version 1.0 * @created 2015-3-1 */public class CarutorDemo { public static void main(String[] args) throws Exception { CuratorFramework client = CuratorFrameworkFactory.builder() .connectString("192.168.1.109:2181") .sessionTimeoutMs(5000) .connectionTimeoutMs(3000) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .build(); client.start(); client.create() .creatingParentsIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath("/zk-huey/cnode", "hello".getBytes()); List<String> children = client.getChildren().forPath("/zk-huey"); System.out.println("Children: " + children); client.close(); } }
新聞熱點
疑難解答