Zookeeper Learning (3) - Common Application Scenarios of Zookeeper

(1) Configuration management

       Centralized configuration management is very common in application clusters. Generally, commercial companies will implement a centralized configuration management center to meet the needs of different application clusters to share their respective configurations, and to notify the cluster when configuration changes are made. of each machine.

Method to realize:

       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, and then obtain new information from Zookeeper. The configuration information is applied to the system.

 

(2) Cluster management 

       In an application cluster, we often need to let each machine know which machines in the cluster (or a dependent cluster) are alive, and when the cluster machine is down, the network is disconnected, etc., it can be used quickly without manual intervention. Notification to every machine.

Method to realize:

       Create a directory node of type EPHEMERAL on Zookeeper, and then each Server calls the getChildren(String path, boolean watch) method on the parent directory node where they created the directory node and sets watch to true. Since it is an EPHEMERAL directory node, when it is created If the server dies, the directory node will also be deleted, so Children will change. At this time, the Watch on getChildren will be called, so other servers will know that a certain server has died. Adding a new server is the same principle.

 

(3) 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 of 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.

void getLock() throws KeeperException, InterruptedException{
        List<String> list = zk.getChildren(root, false);
        String[] nodes = list.toArray(new String[list.size()]);
        Arrays.sort(nodes);
        if(myZnode.equals(root+"/"+nodes[0])){
            doAction();
        }
        else{
            waitForLock(nodes[0]);
        }
    }
    void waitForLock(String lower) throws InterruptedException, KeeperException {
        Stat stat = zk.exists(root + "/" + lower,true);
        if(stat != null){
            mutex.wait();
        }
        else{
            getLock();
        }
    }

 

 

Guess you like

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