A zookeeper-based leader/follower switching demo was created.

Source: Internet
Author: User

A leader election Class Library Demo is written based on zookeeper. The scenario is as follows:

 

Program1. 4 can be deployed on one server, multiple servers, or multiple threads in a process.

 

Running effect:

During initialization (4 fully open)

 

After the fourth (that is, the leader at this time) is disabled

After the process of a follower role is disabled

Disable the process of the leader role.

Code download

 

Use of leaderelectionlibrary:

Class program: ielectionchangednotify {static void main (string [] ARGs) {Program program = new program (); electionclient. registerpolicy (Program); electionclient. startservice (); console. readkey (); electionclient. stopservice ();} public void role y (role newrole) {console. writeline ("New Role:" + newrole );}}

 

In fact, it is just to register the notification object, enable the election service, and close the election Service (of course, there are some configuration items, see below ).

 

Configuration:

<appSettings>    <add key="LeaderElection_Zookeeper" value="127.0.0.1:2181"/>    <add key="LeaderElection_Zookeeper_MembersPath" value="/root/election/demoapp/members"/>    <add key="LeaderElection_Zookeeper_LeaderPath" value="/root/election/demoapp/leader"/>    <add key="LeaderElection_ServerIdentity" value="server1"/></appSettings>

 

In terms of election principles, refer to the cluster section in this URL: https://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/

Currently, Zookeeper's Session Timeout has not been configured and hard-coded for 3 seconds. This number of seconds is the time interval that zookeeper perceives to notify each client after the program is closed inexplicably, you can set a shorter value for a LAN.

 

Code download

 

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.