15. Managing zookeeper with Apache curator

Source: Internet
Author: User
Tags naming convention pack semaphore zookeeper zookeeper client maven central

Apache Zookeeper is a software tool to help solve complex problems, and it helps users to rescue them from complex implementations. however, Zookeeper only exposes the primitives, depending on how the user uses these primitives to solve the coordination problem in the Application. The community has developed a high-level framework on top of the zookeeper data model and its APIS. Apache curator is an advanced Packaging class library and framework that makes zookeeper very easy to Use.

Tips
Originally developed by netflix, curator is now an Apache Project. The project page is located in http://curator.apache.org/.

One curator component

Curator is an advanced class library for zookeeper, which makes processing zookeeper easier and extends the functionality of core Zookeeper. Curator is made up of the following parts at a high level:

    • The Client:curator client is a wrapper for the Java client of Zookeeper. It is a low-level API in the curator stack and abstracts the functionality of the Zookeeper Client.
    • The Framework:curator framework is an advanced API with advanced features, such as automatic connection management, operation retry, and so On. It simplifies the use of zookeeper to a large extent.
    • Recipe:curator Recipe provides implementations of zookeeper Recipe, which can be directly used in distributed applications to solve coordination PROBLEMS.
    • The Extensions:curator recipe package implements the common Recipe. To avoid the expansion of this package, use a separate expansion pack.

In addition to the previous components, curator also comes with some zookeeper useful tools. The curator stack looks like this:

Curator jars can be found in the MAVEN central Repository. Curator can be easily included in build scripts such as Maven,gradle,ivy,sbt.

Various maven artifacts are listed on the Http://mvnrepository.com/artifact/org.apache.curator.

Two curator Client

The curator client is a wrapper for the zookeeper Java Client. It makes it easier and less error-prone for clients to access Zookeeper.

The curator client provides the following features:

    • Connection Management: managing connections to zookeeper Servers
    • Operation Retry Utility: This is the mechanism for retrying the operation
    • Test Zookeeper Server: This is used to test the zookeeper server

The code snippet that uses the curator client to connect to the zookeeper server is MyCuratorClient.java as Follows:

public void myCuratorClient() throws Exception{  CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), 10000, 10000, null,new RetryOneTime(1));  client.start();  try  {    client.blockUntilConnectedOrTimedOut();    String path = client.getZooKeeper().create("/test_znode", "".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);  }  finally  {    client.close();  }}

CuratorZooKeeperClientThe construction method is used to connect to the zookeeper Server. It requires a connection string or a zookeeper host port pair list, session and connection timeout, an optional watcher object, and a retry policy to Use. The retry policy is the mechanism by which the client tries various retry mechanisms when retrying the Connection. In the previous example, a policy with a client that would only retry once was Used.

The curator client supports the following retry policies:

    • Boundedexponentialbackoffretry: retry the specified number of times by increasing the sleep time between retries until the maximum limit is retried
    • Exponentialbackoffretry: retry the specified number of times by increasing the sleep time between retries
    • Retryntimes: Retry n Times
    • Retryonetime: Retry only
    • Retryuntilelapsed: retry until the specified time has passed

Once the client is started, the blockUntilConnectedOrTimedOut method until the Zookeeper connection server succeeds or the connection times Out. After the connection is successful, create /testznode the Znode. getZooKeeper()method to return an instance of a connection to a managed zookeeper Server.

Note
Curator API documentation can be viewed in http://curator.apache.org/apidocs/index.html.

The curator client is a low-level API that provides an abstraction of the Administrator Client Api. Developers should use the curator framework instead of using classes directly in their applications CuratorZookeeperClient as a best practice.

Three curator frame

The curator framework (org.apache.curator.framework) is a high-level API that simplifies the use of zookeeper to a large extent. Some of the features it provides are as Follows:

    • Automatic connection Management: This feature automatically and transparently handles scenarios in which clients need to reestablish connection and/or retry operations with the zookeeper Server.
    • Simple and flexible api: use a set of modern and fluent interfaces to apply the zookeeper original Api.
    • Recipe: This feature implements the common zookeeper Recipe.

CuratorFrameworkUse CuratorFrameworkFactory to Assign. It provides a factory method and a constructor creation Instance. CuratorFrameworkthe instance is completely thread-safe. When using CuratorFramework development applications, developers should create and share an instance for each zookeeper cluster CuratorFramework . CuratorFrameworkuse the Fluent style Interface.

The following shows the CuratorFramework code example used by the zookeeper Client:

public void myCuratorFrameworkClient()throws Exception{  CuratorFramework client =  CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));  client.start();  try  {    String path = client.create().withMode(    CreateMode.PERSISTENT).forPath(    "/test_znode", "".getBytes());  }  finally  {    client.close();  }}

newClient()The factory method creates a new client instance, the default session timeout, and the default connection Timeout. It requires a connection string, which is the zookeeper Host-port pair list and the retry policy to Use.

CuratorFrameworkThere is a concept of a namespace. With this, you can set the namespace when you create an instance using the constructor method CuratorFramework . When one of the APIs is called, the framework pre-loads the namespace to all Paths:

CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();CuratorFramework client = builder.connectString (server.getConnectString()).namespace("MyApp").retryPolicy(new RetryOneTime(1)).build();client.create().forPath("/test_znode", data);

here, Although the name of the Znode is specified /test_znode , the actual znode created is /MyApp/test_znode .

The curator framework also provides a limited- CuratorTempFramework functionality framework interface named for temporary connections over unreliable networks, such as Wans. In this framework, if the session remains idle for a period of time, the zookeeper connection is Closed.

Four curator recipe

Curator offers a range of Ready-to-use recipe for Zookeeper. A detailed list and description of the recipe implemented by curator can be obtained from the http://curator.apache.org/curator-recipes/index.html project Page.

here, a brief introduction to Zookeeper's recipe:

  • Leader Election: Curator provides two algorithms for leading elections: leader lock (leader Latch) and leader selection (leader selector). The two algorithms select a "leader" among multiple competitors connected to the zookeeper Cluster.
    In a leader lock, if a group of N participants competes with the competition leader, one of n participants is randomly assigned as the leader, and in the leader selection, the leader is selected in the order in which the request to the Zookeeper server Arrives. When the leader relieves the leader, select another competitor of N participants in the Cluster.
  • Lock: Curator implements the following different types of distributed locks:
    • Shared re-entry locks: This type of lock provides globally synchronized full-distributed locks.
    • Shared Lock: This is a non-reentrant shared re-entry lock.
    • Shared re-entry Read/write Lock: This is a re-entrant read/write mutex that can be used across the JVM.
    • Shared Semaphores: This is a count semaphore (semaphore) that can be used across the JVM.
    • Multi-lock sharing: This is used to manage multiple locks as a single Entity. acquire()call gets all the Locks. If the call fails, all the obtained paths are Freed. release()call to release all managed locks.
  • Barrier (Barrier): This is the concrete realization of the barrier and the double barrier.
  • Counter: provides a mechanism to manage shared integers for shared counters. It also gives the mechanism of distributed atom increment, distributed Atom integer type and distributed Atom Value.
  • Cache: cache is implemented by Path cache, node cache and tree cache recipe, respectively saving the state change data of znode, local cache node and all locally cached child nodes of the ZK path.
  • Queues: This provides a distributed queue Implementation. The following different types of queues are supported:
    • Distributed Queues: This is a simple distributed queue in which entries placed in a queue are sorted in Fifo.
    • Distributed ID queue: This is the version of a distributed queue that allows some identifiers to be associated with queue Items.
    • Distributed Priority Queue: This is the implementation of the distributed priority queue for Zookeeper. internally, it uses a distributed queue, where priority can be assigned to the Project.
    • Distributed delay Queues: This is a variant of the distributed priority queue that uses time as a Priority. When an entry is added to the queue, a delay value is Given. Until the delay time is exceeded, the item will be sent to the Consumer.
    • Simple distributed queue: This is org.apache.zookeeper.recipes.queue.DistributedQueue part of the alternative implementation of the zookeeper distributed Queue.
  • Node: This provides a recipe for a persistent ephemeral node, which is a ephemeral node that tries to remain in zookeeper even if the connection and session Breaks.
Five curator Utility program

The Curator Class Library also provides some useful tools for zookeeper. Some of them are as follows:

    • Test Server: This is a testing that can be used on the local process zookeeper server
    • Test Cluster: This is an internal run of tests for zookeeper server ensemble
    • Zkpaths: provides a variety of static methods that use zookeeper Znode paths
    • Ensurepath: A utility that ensures that a specific Znode path is created before use
    • Blockingqueueconsumer: a queue consumer similar to Java BlockingQueue
    • Reaper: removing a path with no child nodes and a utility for nodes that do not have data
Six curator extensions

The curator Extension package includes additional recipe in addition to those contained in the recipe Package. The recipe in the expansion pack has curator-x-name a naming convention.

Curator currently offers the following expansion features:

    • Service Discovery: This is a system that uses zookeeper as the service discovery Mechanism.
    • Service Discovery Server: This is a curator service discovery that uses the rest service for Non-java and legacy Programs. It exposes restful Web services to register, delete, and query Services.
    • Curator RPC Proxy: This module implements a proxy that bridges Non-java environments with the curator framework and Recipe. It uses Apache Thrift to enable a large number of languages and environments to use curator functionality and unify zookeeper Cross-language/environment Usage.
    • Zkclient bridge: This extension is the bridge between curator and Zkclient (https://github.com/sgroschupf/zkclient). Applications written with zkclient are useful for using the Curator class library without changing existing Code. Zkclient Bridge is not packaged as part of the curator Distribution. It can be found in its own MAVEN central repository curator-x-zkclient-bridge .

So far, We've learned about the Curator class library and its various components. Curator implements a very good, reliable extension for the zookeeper api, abstracting many of the complexities of zookeeper. Developers are strongly encouraged to develop distributed applications using curator in the Java language zookeeper. Not only that, Curator's powerful features can also be used in applications written in languages other than Java.

15. Managing zookeeper with Apache curator

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.