Zookeeper Learning Series: 3. Build a distributed master and slave system

Source: Internet
Author: User

Previously, I only understood that ZK can name and configure the service. Now I want to learn how it is used as a distributed system for building the master-slave mode.

Why is it Zoo? "Because the distributed system to be coordinated is a zoo ".

Zookeeper is a neutral service used to manage configuration information, name, provide distributed synchronization, and combine services. All these types of services are used in distributed applications. Every time you write these services, there will be a lot of bugs and competition. It is difficult to write these services, so they are usually ignored, which makes it difficult to manage applications when the application changes. Even if handled properly, different approaches to implementing these services make it difficult to manage the deployed applications.

The following code is a reference to the Java version of the document. It coordinates various independent PHP scripts through the service and allows them to agree to a leader (so called the leader election ). When the leader exits (or crashes), the worker can detect and select a new leader. In this way, we can understand how a general master-slave distributed system can be scheduled. ZK is a good thing.

First, you need to understand the node Creation Mode:

Persistent: persistence directory node. The data stored on this directory node will not be lost;

Persistent_sequential;

Ephemeral: temporary directory node. Once the client and server port of the node is created, that is, the session times out, the node will be automatically deleted;

Ephemeral_sequential: the node is automatically numbered.

 

Program Logic:

1) first create the root node/cluster, and create the self-built node with the prefix of/cluster/W-, and use the temporary auto-numbered node mode to create the node

2) Obtain and sort all the sub-nodes of the/cluster. If you find that you are the first node, you will be elected as the leader. Otherwise, you will be deemed as follower.

3) register the listening event. When the previous node in the/cluster changes, return to 2)

In this way, automatic election is implemented. When a node is unavailable after the timeout period, a new leader is automatically generated and an alert can be triggered based on the number of nodes.

Package zookeeper; import Org. apache. zookeeper. *; import Java. io. ioexception; import Java. util. collections; import Java. util. list;/*** created with intellij idea. ** @ author guanpu * Date: 14-10-22 * Time: * to change this template use file | Settings | file templates. */public class worker extends zookeeper implements runnable, Watcher {public static final string node_name = "/cluster"; Publ IC string znode; private Boolean leader; public worker (string connectstring, int sessiontimeout, Watcher watcher) throws ioexception {super (connectstring, sessiontimeout, Watcher);} public Boolean register () throws interruptedexception, keeperexception {If (this. exists (node_name, null) = NULL) {This. create (node_name, "test ". getbytes (), zoodefs. IDs. open_acl_unsafe, createmode. persistent);} Zn Ode = This. create (node_name + "/W-", null, zoodefs. IDs. open_acl_unsafe, createmode. ephemeral_sequential); znode = znode. replace (node_name + "/", ""); string node = watchprevious (); If (node. equals (znode) {system. out. println ("nobody here, I am leader"); Leader = true;} else {system. out. println ("I am watching");} return true;} private string watchprevious () throws interruptedexception, keepere Xception {list <string> works = This. getchildren (node_name, this); collections. sort (Works); system. out. println (Works); int I = 0; For (string work: Works) {If (znode. equals (work) {if (I> 0) {// This. getdata (node_name + "/" + works. get (I-1), this, null); Return works. get (I-1);} return works. get (0) ;}} return "" ;}@ override public void run () {try {This. register ();} catch (interruptede Xception e) {} catch (keeperexception e) {} while (true) {try {If (Leader) {system. out. println ("leading");} else {system. out. println ("following");} thread. sleep (1000);} catch (interruptedexception e) {}} public static void main (string [] ARGs) {try {string hostport = "10.16.73.22, 10.16.73.12, 10.16.73.13 "; new thread (new worker (hostport, 3000, null )). start ();} catch (ioexception e ){ }}@ Override public void process (watchedevent event) {string T = string. Format ("Hello event! Type = % s, stat = % s, Path = % s ", event. getType (), event. getstate (), event. getpath (); system. out. println (t); system. out. println ("Hello, my cluster ID is:" + znode); string node = ""; try {node = This. watchprevious ();} catch (interruptedexception e) {} catch (keeperexception e) {} If (node. equals (znode) {system. out. println ("process: Nobody here, I am leader"); Leader = true;} else {system. out. println ("process: I am watching ");}}}

Start at least three terminals to simulate leader crash. Use Ctrl + C or another method to exit the first script. There will be no changes at the beginning, and worker can continue to work. Later, Zookeeper finds a timeout and selects a new leader.

 

There are two problems with porting PHP to Java. The first one is watcher registration. When the first parent class initialization is incomplete, it cannot call itself as watcher. A watcher call with a null pointer will be reported.

Second question:

This. getdata (node_name + "/" + works. Get (I-1), this, null );

This does not take effect. The method comment triggers the watcher process when the node is changed or removed, but it is not triggered in the experiment, in Java, the automatic deletion of the system is not classified into these two operations?

The PHP version is normal as a legacy issue. To run the program properly, change it to list <string> works = This. getchildren (node_name, This). When the child node changes, execute the process method.

 

References:

Http://anykoro.sinaapp.com/2013/04/05/use apache-zookeeperdistribution to deploy the PHP application/

Zookeeper Learning Series: 3. Build a distributed master and slave system

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.