Curator Framework

Framework 比较简单,就是一些CRUD,Watch :

package curator.framework;

import java.util.List;

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;

public class FrameworkDemo {

	public static void main(String[] args) {
		connection1();
	}

	private static void connection1() {
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
		CuratorFramework client = CuratorFrameworkFactory.builder()
				.connectString("127.0.0.1:2181")
				.sessionTimeoutMs(5000)
				.connectionTimeoutMs(10000)
				.retryPolicy(retryPolicy)
				.namespace("text").build();
		client.start();
		try {
			byte[] b = null;
			// 创建
			client.create().withMode(CreateMode.EPHEMERAL).forPath("/xx",new String("").getBytes());
			// 得到值
			client.getData().forPath("");
			// 得到子节点 List<String>
			List<String> ls = client.getChildren().forPath("");
			// 删除
			client.delete().forPath("");
			// 修改
			client.setData().forPath("", b);
		} catch (Exception e) {
		}
		client.close();
	}

	private static void connection2() {
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
		CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
		client.start();
		client.close();
	}
	
	
	
}

相关推荐