Zookeeper (vii) Open source client

Source: Internet
Author: User
Tags constructor sessions sleep zookeeper zookeeper client

With the introduction of the above two blogs, friends should begin to use the zookeeper simply.

In this biased article, we will focus on the two open source zookeeper client products Zkclient and curator, and then look further at how to better use zookeeper. zkclient Zkclient is an open source zookeeper client on GitHub, developed by Datameer's engineers STEFANGROSCHUPF and Peter Voss. The zkclient is packaged on top of the Zookeeper native API interface and is an easier to use zookeeper client. At the same time, zkclient in the internal implementation of such as the session timeout re-connected/watcher repeatedly registered, and so on zookeeper the client's tedious details of the work is transparent to the developer. In this section, we will describe how to use zkclient this zookeeper client from the aspects of creating a session, creating a node, reading data, updating data, deleting nodes, and detecting the presence of nodes. Of course, because the underlying implementation is also a wrapper on the Zookeeper native API, this section does not have too many primitives to describe. Let's take a look at Zkclient's maven dependency:

<dependencies>
       <dependency>
              <groupId>org.apache.zookeeper</groupId>
               < artifactid>zookeeper<artifactid>
               <version>${zookeeper.version}</version>
       </ dependency>
       <dependency>
               <groupId>com.github.sgroschupf</groupId>
               < artifactid>zkclient</artifactid>
               <version>${zkclient.version}</version>
       </ Dependency>
</dependencies>
Create a sessionIn the above two sections, we have described how to complete the creation of a session by instantiating a zookeeper object. In this section, we'll show you how to use Zkclient to do the same thing. In Zkclient, there are 7 methods of construction:
Public zkclient (String serverstring) public
zkclient (string zkservers,int connectiontimeout) public
zkclient (String zkservers,int sessiontimeout,int connectiontimeout)
Public zkclient (String zkservers,int sessiontimeout,int connectiontimeout,zkserializer zkserializer) public
Zkclient (izkconnection connection) public
zkclient (izkconnection connection,int connectiontimeout)
public Zkclient (izkconnection zkconnection,int connectintimeout,zkserializer Zkserializer)

Zkclient Construction Method parameter description, table 5-11
Name of parameter Description
Zkservers Refers to the list of zookeeper servers, consisting of a comma-separated host:port string of English states, each representing a zookeeper machine, such as 192.168.1.1:2181,192.168.1.2:2181,192.168.1.3:2181
Sessiontimeout The session time-out, in milliseconds. Default is 30000ms
ConnectionTimeout Link creation time-out, in milliseconds. This parameter indicates that if a connection cannot be established with zookeeper during this time period, the connection is discarded and the exception is thrown directly
Connection Implementation class for the Izkconnection interface
Zkserializer Custom Serializer

Note that in the Zkclient construction method, many parameters are consistent with the parameters in the Zookeeper native construction method, so table 5-11 is just a brief introduction, which can be found in table 5-12.

While explaining the creation of a session using the Zookeeper native API, we mentioned that the creation of a zookeeper session is an asynchronous process. For this feature of the zookeeper client, developers need to wait for their own processing. Zkclient, by internal packaging, synchronizes this asynchronous session creation, which is very convenient for developers to use.

Next look at the Izkconnection interface. Org. Ioitec.zkclient,izkconnection interface is the most direct zookeeper native interface packaging, and zookeeper the most direct interaction layer, which contains the addition, deletion, change, check and so on some of the definition of the column interface. Zkclient provides two implementations of the Izkconnection interface by default, Zkconnection and Inmemoryconnection, which are the most commonly used implementations. Often developers do not need to transform the izkconnection, directly using zkconnection This implementation can accomplish most of the business contribution.

Finally, let's look at the biggest difference between the native construction methods of zkclient and zookeeper, which is that in Zkclient's constructor, the arguments to the Watcher object are no longer available. So, how does the client listen to related events on the server side? Don't worry, Zkclient introduces the listener that most Java programmers have used to implement watcher registration. It is worth mentioning that zkclient from the API level to support the registration of watcher listening, such usage is closer to the habits of Java engineers. The registration method for event monitoring will be explained in detail later.

Listing 5-17, creating a session using Zkclient

public class create_session_sample{public
      static void Main (string[] args) throws Ioexception,interruptedexception {
           Zkclient zkclient = new Zkclient ("domain1.book.zookeeper:2181",;
           System.out.println ("ZooKeeper session established.");  
      }
}

Run the program and the output is as follows:

ZooKeeper Session established

The above example shows how to use Zkclient to create a session. Create a node


The following series of interfaces are provided in zkclient to create nodes that developers can use to create various types of nodes:


Listing 5-18 Creating a node with Zkclient

Packege book.chapter
import org. IOItec.zkclient.ZkClient;

public class create_node_sample{public
      static void Main (string[] args) throws excpetion{
           zkclient zkclient = new Zkclient ("domain1.book.zookeeper:2181",);
           String Path = "/ZK-BOOK/C1";
           Zkclient.createpersistent (path,true); 
      }
}

In the above example program, we created the node using the Createpersisten interface of Zkclient and set the createparents parameter to True, indicating that the parent node needs to be created recursively. Obviously, the use of zkclient eliminates a lot of tedious work.

In addition, the Zkclient API also provides a way to support the asynchronous creation of nodes, given that the use of asynchronous methods is very similar to what is explained above, and is not described here.

About reading, modifying nodes, the operation is similar, below we mainly look at zookeeper another open-source account: curator. curator


Curator is a set of zookeeper client frameworks for Netflix open source, the author of Jordan Zimmerman. Like Ziclient, curator solves a lot of zookeeper client's very low-level detail development work, including connection reconnection, repeated registration watcher and Nodeexistsexceptino anomalies, has now become the top project of Apathe, is one of the most widely used ZooKeeper clients in the world, Patrick Hunt (the core submitter of ZooKeeper code) with the phrase "Guava is head Java What curator are to ZooKeeper" (curator for zookeeper, it can be said, like the guava toolset for the Java platform, the role of a huge) it is highly evaluated.

In addition to encapsulating the underlying details that some developers do not need to pay particular attention to, curator is packaged on the basis of the Zookeeper native API, providing a user-friendly and more readable Client API framework for fluent stitching.

In addition, curator provides an abstract package of zookeeper scenarios (Recipe such as shared Lock service/master election mechanism and distributed counters).

Before explaining the API, let's look at curator's maven dependencies:

<dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId> curator-framework</artifactid>
        <version>2.4.2</version>
</dependency>

Create a session



The process of creating sessions using the curator client is very different from the native API of zookeeper and zkclient two ways to create sessions. Specific as follows.


1. Use the Curatorframeworkfactory two static method of this factory class to create a client:


Static Curatorframework newclient (String connectionstring,retrypolicy retyrpolicy);

Static Curatorframework newclient (String conectstring,int sessiontimeoutms,int connectiontimeoutms,retrypolicy Retrypolicy);

2. Start the session by calling the start () method in Curatorframework.

Table 5-20 describes the parameters in the construction method.

Table 5-20

Name of parameter Description
ConnectString Refers to the list of zookeeper servers, consisting of a comma-separated host:port string of English states, each representing a zookeeper machine, for example, 192.168.1.1:2181,192.168.1.2:2181, 192.168.1.3:2181
Retrypolicy Retry policy, the default is mainly four implementations, namely exponential backoffretry,retryntimes,retyronetime,retryunitelapsed
Sessiontimeoutms The session time-out, in milliseconds. Default is 60000ms
Connectiontimeoutms The connection creation time-out, in milliseconds. Default is 15000ms

Creating a session using curator


Listing 5-21

Package Book.chapter
import org.apache.curator.RetryPolicy;
Import Org.apache.curator.framework.CuratorFramework;
Import org.apche.curator.framework.CuratorFrameworkFactory;
Import Org.apache.curator.retry.ExponentialBackoffRetry;

Use curator to create a zookeeper client public
class create_session_sample{
      retrypolicy retrypolicy = new Exponentialbackoffretry (1000,3);
      Curatorframework client = curatorframeworkfactory.newclient ("domain1.book.zookeeper:2181", 5000,3000,retryPolicy);
      Client.start ();
      Thread.Sleep (Integer.max_value);
}

In the example program above, we first created a retry policy named Exponentialbackoffretry, which is one of several retry policies provided by the curator by default, and is constructed as follows:

Exponentialbackoffretry (int basesleeptimems,int maxretries);

Exponetialbackoffretry (int basesleeptimems,int maxretries,int MAXSLEEPMS);

Exponetialbackoffretry Constructor method parameter description as shown in table 5-22

Table 5-22

Name of parameter Description
Basesleeptimems Initial sleep Time
Maxretries Maximum number of retries
Maxsleepms Max sleep Time
Use the Fluent API interface to create a session

The most significant design highlight of the API interface provided by curator is that it follows the fluent design style, which is very different from the Zookeeper native API as well as the Zkclient client. Listing 5-22 shows how to use the Fluent-style API interface to create a session.

Listing 5-22

Package book.chapter;

Import Org.apache.curator.RetryPolicy;
Import Org.apache.curator.framework.CuratorFramework;
Import org.apache.curator.framework.CuratorFrameworkFactory;
Import Org.apache.curator.retry.ExponentialBackoffRetry;
Use the fluent-style API interface to create a zookeeper client public
class create_session_sample_fluent{public
      static void main ( String[] args) throws exception{
            retrypolicy retrypolicy = new Exponentialbackoffretry (1000,3);
            Curatorframeworkfactory.builder (). ConnectString ("domain1.book.zookeeper:2181"). SESSIONTIMEOUTMS (5000). Retrypolicy (Retrypolicy). build ();
             Client.start ();
             Thread.Sleep (Integer.max_value);
      }
}
use curator to create a session with an isolated namespace

In order to achieve separate zookeeper and no-language isolation, a separate namespace is often assigned to each business, specifying a zookeeper root path. For example, the code snippet shown below defines a client's stand-alone namespace/base, and any operation of that client on a data node on zookeeper is based on that relative directory:

Curatorframeworkfactory.builder (). ConnectString ("domain1.book.zookeeper:2181"). SESSIONTIMEOUTMS (5000). Retrypolicy (Retrypolicy). Namespace ("base"). Build ();

We will follow up with more information about the client isolation namespace.

Create a node

Curator provides a range of fluent-style interfaces that developers can use to create various types of nodes by freely combining them.

Listing 5-23

Curatorframework
  --public Createbuilder Create ();
Createbuilder
  --public
protectaclcreatemodepathandbytesable<string>creatingparentsifneeded ();
Createmodable
  --public T withmode (createmode mode);
pathandbytesable<t>
  --public T forpath (String path,byte[] data) throws Exception;
  --public Tforpath (String path) throws Exception;

The above is a list of the most common creation node APIs, with scenarios to illustrate how to use these APIs.

Create a node with an empty initial content

Client.create (). Forpath (path);

Note that if you do not set the node properties, curator creates a persistent node by default and the content is empty by default. The client here refers to a curator client instance, the Curatorframework object instance, that was created and started by the session that was completed.

Create a node that comes with the initial content

Client.create (). Forpath (Path, "Init". GetBytes ());

You can also write the initial node content when you create the node. Unlike Zkclient, curator is still based on Zookeeper native API style, using byte[] as a method parameter.

Create a temporary node and automatically recursively create the parent node

Client.create (). creatingparentsifneeded (). Withmode (createmode.ephemeral). Forpaht (path);

This excuse is very useful, in the process of using zookeeper, developers often encounter nonodeexception exceptions, one of the possible reasons is to try to create a child node for a nonexistent parent node. As a result, developers have to judge the existence of the parent node before each node is created-a process that is tiring. After using curator, by invoking the Creatingparentsifneeded interface, curator is able to automatically recursively create all the required parent nodes.

It is also important to note that since all non-leaf nodes must be persistent nodes in zookeeper, after invoking this API, only the data node corresponding to the PAHT parameter is the temporary node, and its parent node is the persistent node.

Here's a practical example of how you can use these APIs in your code.

Listing 5-24

Package Book.chapter
import org.apache.curator.framework.CuratorFramework;
Import org.apache.curator.framework.CuratorFrameworkFactory;
Import Org.apache.curator.retry.ExponentialBackoffRetry;
Import Org.apache.zookeeper.CreateMode;

Use curator to create a node public
class create_node_sample{
      static String path = "/ZK-BOOK/C1";
      static Curatorframework client = Curatorframeworkfactory.builder (). ConnectString ("domain1.book.zookeeper:2181"). Sessiontimeoutms. Retrypolicy (New Exponentialbackoffretry (1000,3)). Build ();

      public static void Main (string[] args) throws exception{
           Client.start ();
           Client.create (). creatingparentsifneeded (). Withmode (createmode.ephemeral). Forpath (Path, "Init". GetBytes ());
       }
}

Delete a node

In curator, you can delete a specified node by using the following API:

Listing 5-25

Curatorframework
  --pulbic Deletebuilder Delete ();
versionable<t>
   --public T withversion (int version);
Deletebuilder
  --public deletebuilderbase guaranteed ();
pathandbytesable<t>
 --public T forpath (String path,byte[] data) throws Exception;
  --public T Forpath (String path) throws Exception;

The above is a list of the most commonly used delete node APIs, with scenarios to illustrate how to use these APIs.

Delete a node

Client.delete (). Forpath (path);

Note that with this interface, only leaf nodes can be deleted.

Delete a node and recursively delete all of its child nodes

Client.delete (). deletingchildrenifneeded (). Forpath (path);

Deletes a node, forcing the specified version to be deleted

Client.delete (). Withversion (Version). Forpath (path);

Delete a node, forcing a guarantee to delete

Client.delete (). Guaranteed (). Forpath (path);

Note that the guaranteed () interface is a safeguard, so long as the client session is valid, curator will continue to delete in the background, knowing that the node deletion succeeds.

Here's a practical example of how to use these APIs in your code.

Checklist 5-26 curator Remove node API sample

Package book.chapter;
Import Org.apache.curator.framework.CuratorFramework;
Import org.apache.curator.framework.CuratorFrameworkFactory;
Import Org.apache.curator.retry.ExponentialBackoffRetry;
Import Org.apache.zookeeper.CreateMode;
Import Org.apache.zookeeper.data.Stat;

Use curator to delete the node public
class del_data_sample{
      static String path = "/ZK-BOOK/C1";
      static Curatorframework client = Curatorframeworkfactory.builder (). ConnectString ("domain1.book.zookeeper:22181"). Sessiontimeoutms. Retrypolicy (New Exponentialbackoffretry (1000,3)). Build ();
    public static void Main (string[] args) throws exception{
        Client.start ();
        Client.create (). creatingparentsifneeded (). Withmode (createmode.ephemeral). Forpaht (Path, "Init". GetBytes ());
         Stat stat = new stat ();
         Client.getdata (). Storingstatin (STAT). Forpath (path);
          Client.delete (). deletingchildrenifneeded (). Withversion (Stat.getversion ()). Forpath (path);
}
}

The above program is a simple node deletion instance. Here is the key to guaranteed () this method. As noted in the official documentation for this interface, during the zookeeper client use process, you may encounter a problem where the client performs a delete node operation, but causes the delete operation to fail due to some network reasons. For this exception, in some scenarios is fatal, such as "Master election"-in this scenario, the zookeeper client is usually created by the node to delete the implementation. In response to this problem, curator introduces a retry mechanism: if we call the Guaranteed () method, then when the client encounters these network exceptions, it will record the deletion of this failure, as long as the client session is valid, then it will retry in the background repeatedly, Know that the node was deleted successfully. With this approach, you can ensure that the node deletion operation will take effect.

Reading data

Here's a look at how to get the data content of a node through the curator interface.

Listing 5-27

Curatorframework

--public Getdatabuilder getData ();

Statable<t>

--public T Storingstatin (stat stat);

Pathable<t>

--public T Forpath (String path) throws Exception;

The above is a list of the most common API interfaces for reading data node content, with scenarios to illustrate how to use these APIs.

Reads the data contents of a node

Client.getdata (). Forpath (path);

Note that the return value after the interface call is byte[].

Reads the data contents of a node and obtains the stat for that node

Client.getdata (). Storingstatin (STAT). Forpath (path);

Curator stores the latest node state information returned by the server by passing in an old stat variable.

Here's a practical example of how to use these APIs in your code.

Listing 5-28 curator reading data API, instance

Paclage Bppl/cja[ter;
Import Org.apache.curator.framework.CuratorFramework;
Import org.apche.curator.framewor.CuratorFrameworkFactory;
Import Org.apche.curator.retry.ExponentialBackofRetyr;
Import Org.apache.zookeeper.CreateMode;
Import Org.apache.zookeeper.data.Stat;

Use Curatro to get data content public
class get_data_sample{
     static String path = "/zk-book";
     static Curatorframework client = Curatorframeworkfactory.builder (). ConnectString ("domain1.book.zookeeper:2181"). Sessiontimeoutms. Retrypolicy (New Exponentialbackoffretry (1000,3)). Build ();
      public static main (string[] args) throws exception{
           Client.start ();
           Client.create (). creatingparentsifneeded (). Withmode (createmode.ephemeral). Forpath (Path, "Init". GetBytes ());
            Stat stat = new stat ();
            System.out.println (New String (Client.getdate (). Storingstatin (). Forpath (path)));
       }
}

Update Data

In curator, the data for the specified node can be updated with the following APIs.

Checklist 5-29 curator Update Data API

Curatorframework

--public Setdatebuilder setData ();

Versionable<t>

--public T withversion (int version);

Pathandbytesable<t>

--public T Forpath (String path,byte[] data) throws Exception;

Pubic T Forpath (String path) throws Exception;

The above is a list of the most commonly used update data APIs, following specific scenarios to illustrate how to use these APIs.

Update the data content of a node

Client.setdata (). Forpath (path);

When the interface is called, a stat object is returned.

Updates the data content of a node, forcing the specified version to be updated

Client.setdata (). Withversion (Version). Forpath (path);

Note that the Withversion interface is used to implement CAS (Compare and Swap), and version (release information) is usually obtained from an old stat object.

Here's a practical example of how to use these APIs in your code.

Listing 5-30 curator Update Data API instance

Package book.chapter;
Import Org.apache.curator.framework.CuratorFramework;
Improt org.apache.curator.framework.CuratorFrameworkFactory;
Import Org.apache.curatro.retyr.ExponentialBackoffRetry;
Import Org.apache.zookeeper.CreateMode;

Import Org.apache.zookeeper.data.Stat;
     Use curator to update data content public class set_data_sample{static String Path = "/zk-book"; static Curatorframework client = Curatorframeworkfactory.builder (). ConnectString ("domain1.book.zookeeper:2181").
     Sessiontimeoutms. Retrypolicy (New Exponnentialbackoffretry (1000,3)). Build ();
           public static void Main (string[] args) throws exception{Client.start ();
           Client.create (). creatingparentsifneeded (). Withmode (createmode.ephemeral). Forpath (Path, "Init". GetBytes ());
           Stat stat = new stat ();
           Client.getdata (). Storingstatin (STAT). Forpath (path); System.out.println ("Success set node for:" +path+ ", new version:" +client.setdata (). Withversion (Stat.getversion ()). Forpath (Path).GetVersion ());
            try{client.setdata (). Withversion (Stat.getversion ()). Forpath (path);
            }catch (Exception e) {System.out.printlin ("Fail set node due to" + e.getmessage ()); }
     }
}

The example program above demonstrates how to use the Curator API to update the content of a zookeeper data node. The program has been updated two times before and after, using the latest stat variables for the first time. The update was successful, and the second time an expired stat variable was used for the update operation, throwing an exception: Keepererrorcode = Badversion.


General usage of curator Let's talk about this here, let's look at the asynchronous interfaces of curator, master elections, distributed locks, and distributed counters. The following are more advanced and very common content. In particular, Zookee's distributed lock is more widely used than Redis's distributed lock.

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.