Curator Maven dependencies, creating sessions.

Source: Internet
Author: User
Tags sessions zookeeper zookeeper client

Curator solves many zookeeper client very low-level detail development work, including connection reconnection, repeated registration watcher and nodeexistsexception Anomaly, and so on, has become the Apache top project, is one of the most widely used zookeeper clients in the world, Patrick Hunt (the core submitter of zookeeper code) is a "guava is to Java what curator be to zookeeper" (curator for zookeeper, it can be said that, like the guava toolset for the Java platform, the role of a large) is highly appraised.

In addition to encapsulating the low-level details that some developers do not need to pay special attention to, curator is packaged on the basis of the Zookeeper native API, providing a more easy-to-use and readable fluent-style client API framework.

In addition, the curator provides an abstract encapsulation of zookeeper various scenarios (Recipe, such as shared Lock service, master election mechanism, and distributed counters). maven Dependencies

<dependency>

<groupId>org.apache.curator</groupId>

<artifactId>curator.framework</artifactId>

<version>2.4.2</version>

</dependency> Create Session

The steps are as follows.
Use the two static methods of Curatorframeworkfactory this factory class to create a client:

static Curatorframework newclient (String connectstring, Retrypolicy retrypolicy);

Static Curatorframework newclient (String connectstring, int sessiontimeoutms, int connectiontimeoutms, Retrypolicy Retrypolicy);
Start the session by calling the start () method in Curatorframework.

The following table describes the parameters in the construction method.

Name of parameter Description
ConnectString Refers to the list of zookeeper servers, consisting of a host:port string separated by an English state comma, 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. There are four main implementations of default, namely Exponentialbackoffretry, Retryntimes, Retryonetime, retryuntilelapsed
Sessiontimeoutms The session timeout in milliseconds. The default is 000ms
Connectiontimeoutms Connection creation timeout, in milliseconds, default is 000ms

On the retry policy, curator uses an interface retrypolicy to enable the user to implement a custom retry policy. In Retrypolicy to allow the user to implement a custom retry policy. A method is defined in the Retrypolicy interface:

boolean allowretry (int retrycount, long elapsedtimems, Retrysleeper sleeper)

The Retrypolicy interface parameters are illustrated in the following table.

Name of parameter Description
RetryCount The number of times the retry has been retried. If this is the first retry, then the parameter is 0
Elapsedtimems The time, in milliseconds, that has been spent starting with the first retry
Sleeper Used for sleep-specific time. Curator recommends that you do not use Thread.Sleep for action
To create a session using curator

Use curator to create a zookeeper client

public class Create_session_sample {

public static void Main (string[] args) throws Exception {

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 above example program, we first created a retry policy named Exponentialbackoffretry, which is one of several retry policies provided by default curator, which is constructed as follows: Exponentialbackoffretry (int basesleeptimems, int maxretries); exponentialbackoffretry (int basesleeptimems, int maxretries, int maxsleepms);

The Exponentialbackoffretry constructor parameter description is shown in the following table.

Name of parameter Description
Basesleeptimems Initial sleep Time
Maxretries Maximum number of retries
Maxsleepms Maximum sleep time

The Exponentialbackoffretry retry policy is designed as follows.

Given an initial sleep time basesleeptimems, combining the number of retries on this basis, the following formula is used to calculate the current time to sleep:

Current Sleep time = Basesleeptimems * Math.max (1, Random.nextint (1 << (RetryCount + 1) )

As you can see, as the number of retries increases, the calculated sleep time becomes larger. If the sleep time is within the range of the Maxsleepms, the sleep time is used, otherwise the MAXSLEEPMS is used. In addition, the Maxretries parameter controls the maximum number of retries to avoid unlimited retries.

As you can see from the example program above, the Curatorframeworkfactory factory essentially does not complete the creation of a session after creating a client Curatorframework instance. Instead, you need to invoke the Curatorframework start () method to complete the creation of the session. to create a session using the Fluent API interface

The biggest bright spot in the design of the API interface provided by curator is that it follows the fluent design style, which is a very different place from the Zookeeper native API and the Zkclient client.

Use the Fluent 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);

Curatorframework client = 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

To achieve isolation between different zookeeper services, a separate namespace is often assigned to each business, specifying a zookeeper root path. For example, the code snippet shown below defines a client with a separate namespace of/base, and any operation of the 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 ();

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.