Detailed explanation of Apache zookeeper usage examples

This article covers the relevant knowledge of detailed examples of Apache zookeeper usage methods. Next, let's take a look at the specific content.

brief introduction

Apache zookeeper is developed from the zookeeper subproject of Apache Hadoop and has now become the top project of Apache. Zookeeper provides efficient, reliable and easy-to-use collaborative services for distributed systems. It can provide a considerable number of services for distributed applications, such as unified naming services, configuration management, state synchronization and group services. Zookeeper has a simple interface. Developers don't have to worry too much about the synchronization and consistency problems that are difficult to deal with in distributed system programming. You can use the off the shelf services provided by zookeeper to realize the configuration management, group management, leader selection and other functions of distributed system.

Original English Address: http://zookeeper.apache.org/doc/current/javaExample.html

A simple zookeeper watch client

In order to introduce the basic usage of zookeeper Java API, this article will show you how to implement a simple zookeeper client step by step. The zookeeper client will monitor a zookeeper node znode you specify. When the monitored node changes, the client will start or stop a program.

Basic Requirements

The client has four basic requirements:

(1) Parameters carried by the client:

(2) Zookeeper service address.

(3) Name of the monitored znode node.

(4) Executable program and its parameters

The client will get the data of the monitored znode node and start the executable program you specified. If the monitored znode node changes, the client will retrieve its contents and start the executable program you specified again. If the monitored znode node disappears, the client will kill the executable.

Programming

Generally speaking, the zookeeper application is divided into two parts. One part maintains the connection with the server, and the other part monitors the data of the znode node. In this program, the executor class is responsible for maintaining the zookeeper connection, and the Datamonitor class monitors the data in the zookeeper directory tree. At the same time, the executor includes the main thread and the main execution logic of the program. It is responsible for a small amount of user interaction and interaction with the executable program, which accepts the parameters you pass in to it, And it will stop or restart according to the state change of the monitored znode node.

Executor class

The executor object is the most basic "container" of this routine, including zookeeper object and Datamonitor object.

Recall that the task of the executor is to start and stop the executable program you specified on the command line according to the event triggered by the state change of the znode node in the zookeeper. In the above code, you can see that when the executor class instantiates the zookeeper object in its constructor, it passes its own reference to the zookeeper constructor as a watch parameter, It also passes its own reference as a datamonitorlistener parameter to the Datamonitor constructor. The executor itself implements the following interfaces:

The watcher interface is defined in the zookeeper Java API. Zookeeper uses it to communicate with the "container" (the "container" here is similar to the above executor class). Watcher only supports one method, process(), which zookeeper uses to handle events that the main thread may be interested in, such as the state of zookeeper connection or session. In this example, the "container" executor simply passes events down to Datamonitor, How to handle events is determined by Datamonitor. This article only briefly describes how to use watcher. Generally, the executor or an object similar to the executor has a connection with the zookeeper server, but it can pass events to other objects, and other objects handle the event.

Datamonitorlistener interface itself is not a part of zookeeper API. It is completely a user-defined interface, which can be said to be specially designed for this program. The Datamonitor object uses this interface to communicate with the container (i.e. the executor class). The datamonitorlistener interface is as follows:

This interface is defined in Datamonitor. The executor class implements this interface when executor When exists () is called, the executor decides whether to start or stop the application specified in advance (recall that the zookeeper client will kill the executable when znode disappears).

When the executor When closing () is called, the executor will decide whether to close itself according to the permanent disappearance of the zookeeper connection.

You may have guessed that the Datamonitor object calls these methods according to the change of zookeeper state?

The following is the implementation of datamonitorlistener in the executor class Exists() and datamonitorlistener Code for closing():

Datamonitor class

Datamonitor class is the core of zookeeper logic in this program. It is almost asynchronous and event driven. The Datamonitor constructor is as follows:

Call zookeeper Exists() checks whether the specified znode exists, sets monitoring, and passes its own reference as a callback object. In a sense, the real processing flow will be caused when the watch is triggered.

When zookeeper When the exists() operation is completed on the server side, the zookeeper API will call completion callback on the client side:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>