Zookeeper--java Client

Source: Internet
Author: User
Tags zookeeper client

I. Zookeeper INTRODUCTION

A coordinated service system for managing distributed Applications

Two. Zookeeper Application scenario

There are also many introductions on the internet, see http://blog.csdn.net/xinguan1267/article/details/38422149

This article mainly introduces the Java-based client development

Three. Based on Java client combat

3.1Client

  Create a connection to the server   needs ( ip+ port number on the service side) (Session Expiration Time) (Watcher Monitor Registration)   zookeeper zk =  new zookeeper ("10.154.156.180:2181",          3000,  new watcher ()  {             / /  monitors all triggered events public void process (watchedevent event)  {// todo auto-generated  method stubsystem.out.println ("has triggered"  + event.gettype ()  +  "Event! ");  }   }); //  Create a directory node  /**  * createmode:  *  PERSISTENT  (persistent, relative to ephemeral, does not disappear with the disconnection of the client)   *persistent_sequential (persistent and sequential)    *EPHEMERAL  (short, life cycle dependent on client session)   *EPHEMERAL_SEQUENTIAL   (short, sequential)   */ zk.create ("/testrootpath",  "Testrootdata". GetBytes (),  ids.open_acl_unsafe, Createmode.persistent);   // Create a subdirectory node  zk.create ("/testrootpath/testchildpathone",  "Testchilddataone". GetBytes (), Ids.open_acl_ unsafe,createmode.persistent);   system.out.println (New string (Zk.getdata ("/testRootPath", false, NULL));   //  Remove the subdirectory node list  system.out.println (Zk.getchildren ("/testrootpath", true));    //  Create another subdirectory node  zk.create ("/testrootpath/testchildpathtwo",  "Testchilddatatwo" . GetBytes (),  ids.open_acl_unsafe,createmode.persistent);    system.out.println ( Zk.getchildren ("/testrootpath", true));  //  modifies the subdirectory node data  zk.setdata ("/testrootpath/ Testchildpathone "," Hahahahaha ". GetBytes (), -1);     byte[] datas = zk.getdata ("/testrootpath/testchildpathone",  true, null);  string str = new string ( Datas, "Utf-8");  system.out.println (str);    //Delete entire subdirectory    -1 represents version number,- 1 is to remove all versions of  zk.delete ("/testrootpath/testchilDpathone ",  -1);   system.out.println (Zk.getchildren ("/testrootpath ", True);   System.out.println (str);

Some examples can be found in the official website api:http://zookeeper.apache.org/doc/r3.2.2/api/org/apache/zookeeper/zookeeper.html#create% 28java.lang.string,%20byte%5b%5d,%20java.util.list,%20org.apache.zookeeper.createmode%29

3.2 curator

The Curator Framework provides an advanced set of APIs that simplifies the operation of zookeeper. It adds a lot of features developed using zookeeper to handle complex connection management and retry mechanisms for zookeeper clusters. These features include:

      • Encapsulates the connection processing between the zookeeper client and the zookeeper server;

      • Provides a set of fluent style of Operation API;

      • Abstract encapsulation of zookeeper various application scenarios (recipe, such as shared Lock service, cluster leader election mechanism) is provided.

  • The fluent style can be viewed in one of my blog (builder mode), I look at ES search source found that also used the fluent style.

  • Here's how to create an object using curator

  • public static curatorframework  Createwithoptions (string connectionstring, retrypolicy retrypolicy, int  CONNECTIONTIMEOUTMS, INT SESSIONTIMEOUTMS)  {                 return curatorframeworkfactory.builder (). ConnectString ( connectionString)                  .retrypolicy (Retrypolicy)                  .connectiontimeoutms (CONNECTIONTIMEOUTMS)                  .sessiontimeoutms (SESSIONTIMEOUTMS)                  .build ();     } 

The calling code is as follows:

Client = Createwithoptions ("10.154.156.180:2181", New Exponentialbackoffretry (3), +, +); Client.start ();


If you need to create a new directory node is still a fluent style

Client.create (). Forpath ("/curator", New Byte[0]), Client.create (). Withmode (createmode.persistent). ForPath ("/ Curator/childone ", new byte[0]);


Of course create ZK can also not use fluent style

public static Curatorframework Createsimple (String connectionString) {exponentialbackoffretry retrypolicy                = new Exponentialbackoffretry (1000, 3); Return Curatorframeworkfactory.newclient (connectionString, retrypolicy);}


Other ways to explain:

    • Create (): Initiates a create operation. You can combine other methods (such as mode or background) and end with the Forpath () method

    • Delete (): Initiates a delete operation. You can combine other methods (version or background) and end with the Forpath () method

    • Checkexists (): Initiates an action to check whether the Znode exists. You can combine other methods (watch or background) and end with the Forpath () method

    • GetData (): Initiates an operation to get Znode data. You can combine other methods (watch, background, or get stat) and end with the Forpath () method

    • SetData (): Initiates an operation to set Znode data. You can combine other methods (version or background) and end with the Forpath () method

    • GetChildren (): Initiates an operation to get the Znode child node. You can combine other methods (watch, background, or get stat) and end with the Forpath () method

    • Intransaction (): Initiates a zookeeper transaction. You can combine create, setData, check, and/or delete as an action, and commit () commit


Zookeeper--java Client

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.