本文共 5544 字,大约阅读时间需要 18 分钟。
Apache Curator 是一个功能完善的ZooKeeper客户端框架,通过高级API简化了ZooKeeper的操作。它解决了以下三类问题:
Curator通过以下几个方面降低了ZooKeeper的复杂性:
首先,需要在项目的pom.xml中添加Curator相关依赖:
org.apache.curator curator-framework 4.0.0 org.apache.curator curator-recipes 4.0.0 org.apache.curator curator-x-discovery 4.0.0 org.apache.curator curator-test 4.0.0 test
创建连接实例并初始化:
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 TestApacheCurator { private static final String SERVER = "192.168.1.159:2100,192.168.1.159:2101,192.168.1.159:2102"; private final int SESSION_TIMEOUT = 30 * 1000; private final int CONNECTION_TIMEOUT = 3 * 1000; private CuratorFramework client = null; RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); @Before public void init() { client = CuratorFrameworkFactory.newClient(SERVER, SESSION_TIMEOUT, CONNECTION_TIMEOUT, retryPolicy); client.start(); }} @Testpublic void testCreate() throws Exception { // 创建永久节点 client.create().forPath("/curator", "/curator data".getBytes()); // 创建有序节点 client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).forPath("/curator_sequential", "/curator_sequential data".getBytes()); // 创建临时节点 client.create().withMode(CreateMode.EPHEMERAL).forPath("/curator/ephemeral", "/curator/ephemeral data".getBytes()); // 创建有序临时节点 client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/curator/ephemeral_path1", "/curator/ephemeral_path1 data".getBytes()); // 创建带保护的临时有序节点 client.create().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/curator/ephemeral_path2", "/curator/ephemeral_path2 data".getBytes());} @Testpublic void testCheck() throws Exception { // 检查节点是否存在 Stat stat1 = client.checkExists().forPath("/curator"); Stat stat2 = client.checkExists().forPath("/curator2"); System.out.println("'/curator'是否存在: " + (stat1 != null ? true : false)); System.out.println("'/curator2'是否存在: " + (stat2 != null ? true : false));} @Testpublic void testGetAndSet() throws Exception { // 获取节点数据 System.out.println(client.getChildren().forPath("/")); // 获取节点数据 System.out.println(new String(client.getData().forPath("/curator"))); // 设置节点数据 client.setData().forPath("/curator", "/curator modified data".getBytes());} @Testpublic void testSetDataAsync() throws Exception { // 创建监听器 CuratorListener listener = new CuratorListener() { @Override public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception { System.out.println(event.getPath()); } }; // 添加监听器 client.getCuratorListenable().addListener(listener); // 异步设置节点数据 client.setData().inBackground().forPath("/curator", "/curator modified data with Async".getBytes()); // 等待异步执行结果 Thread.sleep(10000);} @Testpublic void testDelete() throws Exception { // 创建测试节点 client.create().orSetData().creatingParentContainersIfNeeded().forPath("/curator/del_key1", "/curator/del_key1 data".getBytes()); client.create().orSetData().creatingParentContainersIfNeeded().forPath("/curator/del_key2", "/curator/del_key2 data".getBytes()); client.create().forPath("/curator/del_key2/test_key", "test_key data".getBytes()); // 删除节点 client.delete().forPath("/curator/del_key1"); // 级联删除子节点 client.delete().guaranteed().deletingChildrenIfNeeded().forPath("/curator/del_key2");} @Testpublic void testTransaction() throws Exception { // 定义基本操作 CuratorOp createOp = client.transactionOp().create().forPath("/curator/one_path", "some data".getBytes()); CuratorOp setDataOp = client.transactionOp().setData().forPath("/curator", "other data".getBytes()); CuratorOp deleteOp = client.transactionOp().delete().forPath("/curator"); // 事务执行 List results = client.transaction().forOperations(createOp, setDataOp, deleteOp); // 遍历结果 for (CuratorTransactionResult result : results) { System.out.println("执行结果是: " + result.getForPath() + "--" + result.getType()); }} @Testpublic void testNamespace() throws Exception { // 创建命名空间的连接实例 CuratorFramework client2 = CuratorFrameworkFactory.builder() .namespace("mydemo/v1") .connectString(SERVER) .sessionTimeoutMs(SESSION_TIMEOUT) .connectionTimeoutMs(CONNECTION_TIMEOUT) .retryPolicy(retryPolicy) .build(); client2.start(); // 创建带命名空间的节点 client2.create().orSetData().creatingParentContainersIfNeeded().forPath("/server1/method1", "some data".getBytes()); client2.close();} 通过以上代码示例,可以快速上手Apache Curator框架对ZooKeeper的操作。
转载地址:http://sdof.baihongyu.com/