Distributed service framework zookeeper

1. Zookeeper data structure: A data structure with a hierarchical relationship, very similar to a standard file system



 
 

1: Each subdirectory item (such as NameService, Configuration, GroupMembers, Apps is called znode),

This znode is uniquely identified by its path, such as Server1, the znode is identified as /NameService/Server1

2: (Temporary node) Directory nodes of type EPHEMERA cannot have subnode directories

3: Znodes are versioned, and the data stored in each znode can have multiple versions. Multiple copies of data can be stored in one access path

4: The znode can be a temporary node. Once the client who created the znode loses contact with the server, the znode will also be deleted automatically

5: The directory name of the znode can be automatically numbered. If App1 already exists, if you create it again, it will be automatically named App2

6: The znode can be monitored, including the modification of the data stored in this directory node, the change of the sub-node directory, etc. Once the change is made, the client who sets the monitoring can be notified

 

2. Basic operation

// Create a connection to the server
 ZooKeeper zk = new ZooKeeper("localhost:" + CLIENT_PORT,
        ClientBase.CONNECTION_TIMEOUT, new Watcher() {
            // monitor all triggered events
            public void process(WatchedEvent event) {
                System.out.println("Triggered" + event.getType() + "Event!");
            }
        });
 // create a directory node
 zk.create("/testRootPath", "testRootData".getBytes(), Ids.OPEN_ACL_UNSAFE,
   CreateMode.PERSISTENT);
 // create a subdirectory node
 zk.create("/testRootPath/testChildPathOne", "testChildDataOne".getBytes(),
   Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
 System.out.println(new String(zk.getData("/testRootPath",false,null)));
 // Get the list of subdirectory nodes
 System.out.println(zk.getChildren("/testRootPath",true));
 // Modify the subdirectory node data
 zk.setData("/testRootPath/testChildPathOne","modifyChildDataOne".getBytes(),-1);
 System.out.println("Directory node status: ["+zk.exists("/testRootPath",true)+"]");
 // create another subdirectory node
 zk.create("/testRootPath/testChildPathTwo", "testChildDataTwo".getBytes(),
   Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
 System.out.println(new String(zk.getData("/testRootPath/testChildPathTwo",true,null)));
 // delete subdirectory node
 zk.delete("/testRootPath/testChildPathTwo",-1);
 zk.delete("/testRootPath/testChildPathOne",-1);
 // delete parent directory node
 zk.delete("/testRootPath",-1);
 // close the connection
 zk.close();

 The output is as follows:

The None event has been fired!
 testRootData
 [testChildPathOne]
Directory Node Status: [5,5,1281804532336,1281804532336,0,1,0,0,12,1,6]
The NodeChildrenChanged event has been fired!
 testChildDataTwo
The NodeDeleted event has been fired!
The NodeDeleted event has been fired!

 

 

 3. Zookeeper application scenarios

1. Unified naming service: It is an ideal choice to use a tree-shaped name structure. The tree-shaped name structure is a hierarchical directory structure, which is friendly to people and will not repeat.

 

2. Configuration management: Save the configuration information in a directory node of Zookeeper, and then monitor the status of the configuration information for all application machines that need to be modified. Once the configuration information changes, each application machine will receive a notification from Zookeeper. Then get the new configuration information from Zookeeper and apply it to the system

 

3. Cluster management: Zookeeper can not only help you maintain the service status of the machines in the current cluster, but also help you choose a "manager" to manage the cluster

 

4. Shared locks (Locks)

 The implementation method is to create an EPHEMERAL_SEQUENTIAL directory node, and then call  the getChildren method to obtain whether the smallest directory node in the current directory node list is the directory node created by itself, and if it is created by itself, then it gets This lock, if not, then it calls the  exists ( String  path, boolean watch ) method and monitors the changes in the directory node list on Zookeeper until the node created by itself is the directory node with the smallest number in the list, so as to obtain the lock and release the lock very quickly. Simple, just delete the directory node it created by itself earlier.

 

5. Queue management

  1. When the members of a queue are all gathered, the queue is available, otherwise it will wait for all members to arrive. This is a synchronous queue. The idea of ​​​​implementing the synchronization queue with Zookeeper is as follows:

    Create a parent directory /synchronizing, each member monitors whether the flag (Set Watch) bit directory /synchronizing/start exists, and then each member joins this queue. The way to join the queue is to create a temporary directory node of /synchronizing/member_i, Then each member gets/synchronizing all directory nodes of the directory, ie member_i. Determine whether the value of i is already the number of members, if it is less than the number of members, wait for the appearance of /synchronizing/start, if it is equal, create /synchronizing/start.

  2. Queues are enqueued and dequeued in a FIFO manner, such as implementing the producer and consumer models. The idea of ​​​​implementation is also very simple, that is, create a subdirectory /queue_i of type SEQUENTIAL in a specific directory, so as to ensure that all members are numbered when they join the queue, and the getChildren( ) method can return all current Elements in the queue, and then consume the smallest one of them, which guarantees FIFO.

 

 

Guess you like

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