Zookeeper curator Framework (crud/transaction/election/Lock) use

Source: Internet
Author: User
Tags zookeeper
The curator framework is best used by the most popular zookeeper clients.

It has the following three advantages

1 A set of very friendly Operation API;

2. Provide some advanced features (including, but not limited to, the packaging mentioned in the previous article)

3. Easy to test

MAVEN relies on the following

[HTML] view plain copy print? <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes< /artifactid> <version>2.5.0</version> </dependency>

<dependency>  
    <groupId>org.apache.curator</groupId>  
    <artifactid>curator-recipes </artifactId>  
    <version>2.5.0</version>  
</dependency>  


According to the official document and package structure, you can easily see the curator function divided into two categories, one is the zookeeper of some basic command of the package, such as adding and deleting changes. Is his framework module, one of his advanced features, the Recipes module.

First, the framework module

Curator provides a fluent set of operating APIs. This is quite popular in many scripting languages.

For example, he created the client code like this [Java] view plain copy print? Curatorframework client = builder.connectstring ("192.168.11.56:2180")              .sessiontimeoutms (30000)               .connectiontimeoutms (30000)               .canbereadonly (false)               .retrypolicy (New exponentialbackoffretry (1000, integer.max_value))               .namespace (namespace)              .defaultdata (null)               .build ();     Client.start ();    

Curatorframework client = builder.connectstring ("192.168.11.56:2180")  
        . Sessiontimeoutms (30000)  
        . Connectiontimeoutms (30000)  
        . Canbereadonly (False)  
        . Retrypolicy (New Exponentialbackoffretry (1000, Integer.max_value)  
        . Namespace (namespace).  
        defaultdata (NULL)  
        . Build ();  
Client.start ();  


All the way to the end, this is the so-called fluent style.

We'll look at the additions and deletions.[Java] View Plain copy print? public class crudexamples {         private static  curatorframework client = clientfactory.newclient ();          private static final String PATH =  "/crud";              public static void main (String[] args)  {             try {                  client.start ();                        Client.create (). Forpath (path,  "I love messi". GetBytes ());                       byte[] bs = client.getdata (). Forpath (PATH);              &NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("New node, data:"  + new string (BS));                        Client.setdata (). Forpath (path,  "I love football". GetBytes ());                       //  Due to the data obtained in background mode, BS may be null                  byte[] bs2 = client.getdata (). watched (). Inbackground (). ForPath (PATH);                   SYSTEM.OUT.PRINTLN ("Modified data is"  + new string (bs2 != null ? bs2 :  New byte[0]);     &Nbsp;                client.delete (). Forpath (PATH);                  Stat stat = client.checkexists (). Forpath (PATH);                      // stat is a mapping of all properties of Zonde,  Stat=null indicates that the node does not exist.                   SYSTEM.OUT.PRINTLN (stat);             } catch   (exception e)  {                  e.printstacktrace ();              } finally {                 &nbsp closeableutils.closequietly (client);             }          }    }    

public class Crudexamples {private static Curatorframework client = Clientfactory.newclient ();  
  
    private static final String PATH = "/crud";  
  
            public static void Main (string[] args) {try {client.start ();  
  
            Client.create (). Forpath (PATH, "I love Messi". GetBytes ());  
            byte[] bs = Client.getdata (). Forpath (PATH);  
  
            SYSTEM.OUT.PRINTLN ("New node, data:" + new String (BS));  
  
            Client.setdata (). Forpath (PATH, "I love Football". GetBytes ()); Because it is the data obtained in background mode, BS at this time may be null byte[] BS2 = Client.getdata (). watched (). Inbackground (). Forpath (PATH  
            );  
  
            SYSTEM.OUT.PRINTLN ("Modified data is" + New String (BS2! = null? Bs2:new byte[0]));  
            Client.delete (). Forpath (PATH);  
  
            Stat stat = client.checkexists (). Forpath (PATH);  
            Stat is a mapping of all Zonde properties, and Stat=null indicates that the node does not exist.  
        SYSTEM.OUT.PRINTLN (STAT); } catch (Exception e) {e.printstacktrace ();  
        } finally {closeableutils.closequietly (client);   }  
    }  
}


Common interfaces are

Create () increases

Delete (): delete

Checkexists (): Determine if there is

SetData (): Change

GetData (): Check

All of these methods end with Forpath (), supplemented by watch (listening), Withmode (specified mode), and Inbackground (run in the background).


In addition, curator supports transactions, and a set of CRUD operations is in the same life. The code is as follows

/**   *  Transaction Operations    *    *  @author  shencl   */   public class transactionexamples {       private static  Curatorframework client = clientfactory.newclient ();           public static void main (String[] args)  {            try {                client.start ();                //  Open Transactions                 Curatortransaction transaction = client.intransaction ();                   Collection<CuratorTransactionResult>  Results = trAnsaction.create ()   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.