For leader elections, it can also be implemented with shared reentrant lock.
If you need a fixed machine in the cluster to do something, you can use this feature to achieve, until the leader die, will produce new leader.
1. Run Lltest directly
Package Com.collonn.javautilmvn.zookeeper.curator.leaderelection;import Com.collonn.javautilmvn.zookeeper.curator.nodecache.nlclientcreate;import Com.collonn.javautilmvn.zookeeper.curator.nodecache.nlclientdelete;import Com.collonn.javautilmvn.zookeeper.curator.nodecache.nlclientupdate;public class Lltest {public static void main (string[] args) throws Exception { leaderlistener.main (null); Leaderlistener2.main (null); Leaderlistener3.main (null); Leaderlistener4.main (null);} }
Package Com.collonn.javautilmvn.zookeeper.curator.leaderelection;import Org.apache.curator.retrypolicy;import Org.apache.curator.framework.curatorframework;import org.apache.curator.framework.CuratorFrameworkFactory; Import Org.apache.curator.framework.recipes.cache.childdata;import Org.apache.curator.framework.recipes.cache.pathchildrencache;import Org.apache.curator.framework.recipes.cache.pathchildrencacheevent;import Org.apache.curator.framework.recipes.cache.pathchildrencachelistener;import Org.apache.curator.framework.recipes.leader.leaderselector;import Org.apache.curator.framework.recipes.leader.leaderselectorlistener;import Org.apache.curator.framework.state.connectionstate;import Org.apache.curator.retry.ExponentialBackoffRetry; Import Org.apache.curator.utils.ensurepath;import Java.util.list;public class Leaderlistener {public static final Stri ng C_path = "/testleader"; public static final String CHARSET = "UTF-8"; public static final String app_name = "App1"; PubLic static void Main (string[] args) {try {new Thread (new Runnable () {@Override public void Run () {try{String zookeeperconnectionstring = "127.0.0.1:2 181 "; Retrypolicy retrypolicy = new Exponentialbackoffretry (1000, 3); Curatorframework client = curatorframeworkfactory.newclient (zookeeperconnectionstring, Retrypolicy); Client.start (); Ensure path of/test new Ensurepath (C_path). ensure (Client.getzookeeperclient ()); Final Leaderselector leaderselector = new Leaderselector (client, C_path, new Leaderselectorlistener () { @Override public void Takeleadership (Curatorframework client) throws Exception {try {int timemilliseconds = 6000; System.out.println ("======" + app_name + "Take leader ship, would do some task, 'll hold Time Milli seconds= "+ timemilliseconds); Once the leader ship//and your want hold the leader ship during the whole Li Fe of APP1//you should use Thread.Sleep (Integer.max_value) Once tha APP1 dead, the other APP (participants) would reelect an new leader F or (int i = 0; i < 6; i++) {System.out.println ("= = =" + app_name + "sleep" + i); Thread.Sleep (1000); }}catch (Exception e) {e.printstacktrace (); }} @Override public void StateChanged (Curatorframework client, ConnectionState newstate) {} }); Leaderselector.start (); Thread.Sleep (Integer.max_value); Client.close (); }catch (Exception e) {e.printstacktrace (); }}). Start (); }catch (Exception e) {e.printstacktrace (); } }}
Package Com.collonn.javautilmvn.zookeeper.curator.leaderelection;import Org.apache.curator.retrypolicy;import Org.apache.curator.framework.curatorframework;import org.apache.curator.framework.CuratorFrameworkFactory; Import Org.apache.curator.framework.recipes.leader.leaderselector;import Org.apache.curator.framework.recipes.leader.leaderselectorlistener;import Org.apache.curator.framework.state.connectionstate;import Org.apache.curator.retry.ExponentialBackoffRetry; Import Org.apache.curator.utils.ensurepath;public class LeaderListener2 {public static final String C_path = "/testlea Der "; public static final String CHARSET = "UTF-8"; public static final String app_name = "APP2"; public static void Main (string[] args) {try {new Thread (new Runnable () {@Override public void Run () {try{String zookeeperconnectionstring = "127.0.0. 1:2,181 "; Retrypolicy Retrypolicy = New Exponentialbackoffretry (1000, 3); Curatorframework client = curatorframeworkfactory.newclient (zookeeperconnectionstring, Retrypolicy); Client.start (); Ensure path of/test new Ensurepath (C_path). ensure (Client.getzookeeperclient ()); Final Leaderselector leaderselector = new Leaderselector (client, C_path, new Leaderselectorlistener () { @Override public void Takeleadership (Curatorframework client) throws Exception {try {int timemilliseconds = 6000; System.out.println ("======" + app_name + "Take leader ship, would do some task, would hold time milli sec onds= "+ timemilliseconds); for (int i = 0; i < 6; i++) {System.out.println ("= = =" + App_namE + "Sleep" + i); Thread.Sleep (1000); }}catch (Exception e) {e.printstacktrace (); }} @Override Public void StateChanged (Curatorframework client, ConnectionState newstate) {} }); Leaderselector.start (); Thread.Sleep (Integer.max_value); Client.close (); }catch (Exception e) {e.printstacktrace (); }}). Start (); }catch (Exception e) {e.printstacktrace (); } }}
Package Com.collonn.javautilmvn.zookeeper.curator.leaderelection;import Org.apache.curator.retrypolicy;import Org.apache.curator.framework.curatorframework;import org.apache.curator.framework.CuratorFrameworkFactory; Import Org.apache.curator.framework.recipes.leader.leaderselector;import Org.apache.curator.framework.recipes.leader.leaderselectorlistener;import Org.apache.curator.framework.state.connectionstate;import Org.apache.curator.retry.ExponentialBackoffRetry; Import Org.apache.curator.utils.ensurepath;public class LeaderListener3 {public static final String C_path = "/testlea Der "; public static final String CHARSET = "UTF-8"; public static final String app_name = "APP3"; public static void Main (string[] args) {try {new Thread (new Runnable () {@Override public void Run () {try{String zookeeperconnectionstring = "127.0.0. 1:2,181 "; Retrypolicy Retrypolicy = New Exponentialbackoffretry (1000, 3); Curatorframework client = curatorframeworkfactory.newclient (zookeeperconnectionstring, Retrypolicy); Client.start (); Ensure path of/test new Ensurepath (C_path). ensure (Client.getzookeeperclient ()); Final Leaderselector leaderselector = new Leaderselector (client, C_path, new Leaderselectorlistener () { @Override public void Takeleadership (Curatorframework client) throws Exception {try {int timemilliseconds = 6000; System.out.println ("======" + app_name + "Take leader ship, would do some task, would hold time milli sec onds= "+ timemilliseconds); for (int i = 0; i < 6; i++) {System.out.println ("= = =" + App_namE + "Sleep" + i); Thread.Sleep (1000); }}catch (Exception e) {e.printstacktrace (); }} @Override Public void StateChanged (Curatorframework client, ConnectionState newstate) {} }); Leaderselector.start (); Thread.Sleep (Integer.max_value); Client.close (); }catch (Exception e) {e.printstacktrace (); }}). Start (); }catch (Exception e) {e.printstacktrace (); } }}
Package Com.collonn.javautilmvn.zookeeper.curator.leaderelection;import Org.apache.curator.retrypolicy;import Org.apache.curator.framework.curatorframework;import org.apache.curator.framework.CuratorFrameworkFactory; Import Org.apache.curator.framework.recipes.leader.leaderselector;import Org.apache.curator.framework.recipes.leader.leaderselectorlistener;import Org.apache.curator.framework.state.connectionstate;import Org.apache.curator.retry.ExponentialBackoffRetry; Import Org.apache.curator.utils.ensurepath;public class LeaderListener4 {public static final String C_path = "/testlea Der "; public static final String CHARSET = "UTF-8"; public static final String app_name = "APP4"; public static void Main (string[] args) {try {new Thread (new Runnable () {@Override public void Run () {try{String zookeeperconnectionstring = "127.0.0. 1:2,181 "; Retrypolicy Retrypolicy = New Exponentialbackoffretry (1000, 3); Curatorframework client = curatorframeworkfactory.newclient (zookeeperconnectionstring, Retrypolicy); Client.start (); Ensure path of/test new Ensurepath (C_path). ensure (Client.getzookeeperclient ()); Final Leaderselector leaderselector = new Leaderselector (client, C_path, new Leaderselectorlistener () { @Override public void Takeleadership (Curatorframework client) throws Exception {try {int timemilliseconds = 6000; System.out.println ("======" + app_name + "Take leader ship, would do some task, would hold time milli sec onds= "+ timemilliseconds); for (int i = 0; i < 6; i++) {System.out.println ("= = =" + App_namE + "Sleep" + i); Thread.Sleep (1000); }}catch (Exception e) {e.printstacktrace (); }} @Override Public void StateChanged (Curatorframework client, ConnectionState newstate) {} }); Leaderselector.start (); Thread.Sleep (Integer.max_value); Client.close (); }catch (Exception e) {e.printstacktrace (); }}). Start (); }catch (Exception e) {e.printstacktrace (); } }}
Apache curator Leader Election