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