zookeeper java api

1. Establish a zookeeper session Zookeeper (String connect , int timeout, Watcher watcher)

1: Watcher: an object used to accept session events, this object needs to be created by ourselves. The client needs to implement the watcher interface to monitor the session with zookeeper.

 

2. If the zookeeper service consists of at least three servers, the failure of one server will not cause service interruption. The client will also receive the disconnected event soon, and then the syncConnected event.

 

3. When zookeeper is started, you can connect to it by telnet, and then check the running status and status of the server through the stat and dump commands.

 

4. Obtain management rights: We use zookeeper to implement a simple group leader election method. All machines will try to create the /master node, but only one will succeed, and this successful machine will become the master node. Handle two key exceptions: ConnectionLossException and InterruptedException

 

6. Asynchronous acquisition of management rights: 

void create(string path , byte[] data , List<ACL> acl,CreateMode createmode,AsyncCallback cb,Object ctx)

(1) After this method is called, it usually returns before the create request is sent to the server. When the server receives the result of the create request, the method of the callback object will be executed, and the context parameters will also be passed to the method of the callback object.

(2) Callback function processing: Because only one single thread handles all callback calls, the use of synchronous methods is generally avoided.

 

7. Set metadata (collaboration data): You need to set three permanent directories /workers , /task , /assgin

 

8. Register slave nodes: Each slave node will create a temporary znode node under /workers. It is necessary to store the status information of the slave node into the znode node representing the slave node, and then you can check the znode to get the status of the slave node.

( When setting the state, use the synchronous detection method to avoid connection loss, which is a reissue operation )

 

9. Task queuing: We will add child nodes to the /tasks node to indicate the commands that need to be executed. Ordered nodes will be used, which has two benefits, first, the sequence number specifies the order in which tasks are queued, and second, a unique path based on the sequence number can be created.

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326581392&siteId=291194637