Curator教程(一)快速入門

來源:互聯網
上載者:User

Curator是Netflix公司開源的一個Zookeeper用戶端,與Zookeeper提供的原生用戶端相比,Curator的抽象層次更高,簡化了Zookeeper用戶端編程。

它包含以下幾個組件:

Component description
Recipes Implementations of some of the common ZooKeeper “recipes”. The implementations are built on top of the Curator Framework.
Framework The Curator Framework is a high-level API that greatly simplifies using ZooKeeper. It adds many features that build on ZooKeeper and handles the complexity of managing connections to the ZooKeeper cluster and retrying operations.
Utilities Various utilities that are useful when using ZooKeeper.
Client A replacement for the bundled ZooKeeper class that takes care of some low-level housekeeping and provides some useful utilities.
Errors How Curator deals with errors, connection issues, recoverable exceptions, etc.
Extensions The curator-recipes package implements the common recipes that are described in the ZooKeeper documentation. To avoid bloating that package, recipes/applications that have a vertical appeal will be put in separate “extension” packages using the naming convention curator-x-name.
環境配置 JDK 1.7 Zookeeper 3.4.8 Curator 2.11.1 Maven 3.3 快速入門 Maven依賴
<properties>    <java.version>1.7</java.version>    <curator.version>2.11.1</curator.version></properties><dependencies>    <dependency>        <groupId>org.apache.zookeeper</groupId>        <artifactId>zookeeper</artifactId>        <version>3.4.8</version>        <exclusions>            <exclusion>                <artifactId>slf4j-log4j12</artifactId>                <groupId>org.slf4j</groupId>            </exclusion>            <exclusion>                <artifactId>log4j</artifactId>                <groupId>log4j</groupId>            </exclusion>        </exclusions>    </dependency>    <dependency>        <groupId>org.apache.curator</groupId>        <artifactId>curator-framework</artifactId>        <version>${curator.version}</version>    </dependency>    <dependency>        <groupId>org.apache.curator</groupId>        <artifactId>curator-client</artifactId>        <version>${curator.version}</version>        <exclusions>            <exclusion>                <groupId>com.google.guava</groupId>                <artifactId>guava</artifactId>            </exclusion>        </exclusions>    </dependency>    <dependency>        <groupId>com.google.guava</groupId>        <artifactId>guava</artifactId>        <version>18.0</version>    </dependency></dependencies>
1.建立Curator 串連執行個體
String address = "localhost:2181";CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3));

注意:一個Zookeeper叢集只需要構造一個CuratorFramework 執行個體對象即可。

CuratorFramework 使用之前必須先調用

client.start();

完成一系列操作後,調用client.close();方法,可以使用try-finally語句:

CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3));try{    client.start();    ...}finally {    if(client!=null)        client.close();}
2.建立節點

a. 建立永久性節點

client.create()      .creatingParentContainersIfNeeded()      .withMode(CreateMode.PERSISTENT)      .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)      .forPath(path, "hello, zk".getBytes());

b. 建立臨時節點

client.create().withMode(CreateMode.EPHEMERAL).forPath(path, "hello".getBytes());  
3.擷取節點值
byte[] buf = client.getData().forPath(path);System.out.println("get data path:"+path+", data:"+new String(buf));
4.設定節點值
client.setData().inBackground().forPath(path, "ricky".getBytes());
5.checkExists
Stat stat = client.checkExists().forPath(path);if(stat==null){    System.out.println("exec create path:"+path);}else {    System.out.println("exec getData");}
6.刪除節點
client.delete().deletingChildrenIfNeeded().forPath("/pandora");


點此下載完整代碼:https://github.com/TiFG/zookeeper-samples 參考

Getting Started:http://curator.apache.org/getting-started.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.