Apache Curator Leader Election

http://blog.csdn.net/collonn/article/details/43968655


用于Leader选举,也可以用Shared Reentrant Lock来实现。

如果需要集群中的固定的一台机器去做的事,就可以用此特性来实现,直到这台Leader死去,会产生新的Leader。

还有一种典型的场景,master-slave模式。也可以用Curator Leader Election轻松实现,包括充当maste的机器死掉后,会产生新的master,以及当新加入机器后,该机器会直接参与Leader竞争者。That's Awesome.

1.直接运行LLtest


[java]  view plain  copy
  1. package com.collonn.javaUtilMvn.zookeeper.curator.LeaderElection;  
  2.   
  3. import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NLClientCreate;  
  4. import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NLClientDelete;  
  5. import com.collonn.javaUtilMvn.zookeeper.curator.NodeCache.NLClientUpdate;  
  6.   
  7. public class LLTest {  
  8.     public static void main(String[] args) throws Exception {  
  9.         LeaderListener.main(null);  
  10.         LeaderListener2.main(null);  
  11.         LeaderListener3.main(null);  
  12.         LeaderListener4.main(null);  
  13.     }  
  14. }  


[java]  view plain  copy
  1. package com.collonn.javaUtilMvn.zookeeper.curator.LeaderElection;  
  2.   
  3. import org.apache.curator.RetryPolicy;  
  4. import org.apache.curator.framework.CuratorFramework;  
  5. import org.apache.curator.framework.CuratorFrameworkFactory;  
  6. import org.apache.curator.framework.recipes.cache.ChildData;  
  7. import org.apache.curator.framework.recipes.cache.PathChildrenCache;  
  8. import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;  
  9. import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;  
  10. import org.apache.curator.framework.recipes.leader.LeaderSelector;  
  11. import org.apache.curator.framework.recipes.leader.LeaderSelectorListener;  
  12. import org.apache.curator.framework.state.ConnectionState;  
  13. import org.apache.curator.retry.ExponentialBackoffRetry;  
  14. import org.apache.curator.utils.EnsurePath;  
  15.   
  16. import java.util.List;  
  17.   
  18. public class LeaderListener {  
  19.     public static final String C_PATH = "/TestLeader";  
  20.     public static final String CHARSET = "UTF-8";  
  21.     public static final String APP_NAME = "app1";  
  22.   
  23.     public static void main(String[] args) {  
  24.         try {  
  25.             new Thread(new Runnable() {  
  26.                 @Override  
  27.                 public void run() {  
  28.                     try{  
  29.                         String zookeeperConnectionString = "127.0.0.1:2181";  
  30.                         RetryPolicy retryPolicy = new ExponentialBackoffRetry(10003);  
  31.                         CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);  
  32.                         client.start();  
  33.   
  34.                         //ensure path of /test  
  35.                         new EnsurePath(C_PATH).ensure(client.getZookeeperClient());  
  36.   
  37.                         final LeaderSelector leaderSelector = new LeaderSelector(client, C_PATH, new LeaderSelectorListener() {  
  38.                             @Override  
  39.                             public void takeLeadership(CuratorFramework client) throws Exception {  
  40.                                 try {  
  41.                                     int timeMilliSeconds = 6000;  
  42.                                     System.out.println("======" + APP_NAME + " take leader ship, will do some task, will hold time milli seconds=" + timeMilliSeconds);  
  43.   
  44.                                     //once you take the leader ship  
  45.                                     //and you want hold the leader ship during the whole life of APP1  
  46.                                     //you should use Thread.sleep(Integer.MAX_VALUE)  
  47.                                     //once tha APP1 dead, the other APP (participants) will reElect an new leader  
  48.                                     for(int i = 0; i < 6; i++){  
  49.                                         System.out.println("===" + APP_NAME + " sleep " + i);  
  50.                                         Thread.sleep(1000);  
  51.                                     }  
  52.                                 }catch (Exception e){  
  53.                                     e.printStackTrace();  
  54.                                 }  
  55.                             }  
  56.   
  57.                             @Override  
  58.                             public void stateChanged(CuratorFramework client, ConnectionState newState) {  
  59.   
  60.                             }  
  61.                         });  
  62.   
  63.                         leaderSelector.start();  
  64.   
  65.                         Thread.sleep(Integer.MAX_VALUE);  
  66.                         client.close();  
  67.                     }catch (Exception e){  
  68.                         e.printStackTrace();  
  69.                     }  
  70.                 }  
  71.             }).start();  
  72.         }catch (Exception e){  
  73.             e.printStackTrace();  
  74.         }  
  75.     }  
  76. }  


[java]  view plain  copy
  1. package com.collonn.javaUtilMvn.zookeeper.curator.LeaderElection;  
  2.   
  3. import org.apache.curator.RetryPolicy;  
  4. import org.apache.curator.framework.CuratorFramework;  
  5. import org.apache.curator.framework.CuratorFrameworkFactory;  
  6. import org.apache.curator.framework.recipes.leader.LeaderSelector;  
  7. import org.apache.curator.framework.recipes.leader.LeaderSelectorListener;  
  8. import org.apache.curator.framework.state.ConnectionState;  
  9. import org.apache.curator.retry.ExponentialBackoffRetry;  
  10. import org.apache.curator.utils.EnsurePath;  
  11.   
  12. public class LeaderListener2 {  
  13.     public static final String C_PATH = "/TestLeader";  
  14.     public static final String CHARSET = "UTF-8";  
  15.     public static final String APP_NAME = "app2";  
  16.   
  17.     public static void main(String[] args) {  
  18.         try {  
  19.             new Thread(new Runnable() {  
  20.                 @Override  
  21.                 public void run() {  
  22.                     try{  
  23.                         String zookeeperConnectionString = "127.0.0.1:2181";  
  24.                         RetryPolicy retryPolicy = new ExponentialBackoffRetry(10003);  
  25.                         CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);  
  26.                         client.start();  
  27.   
  28.                         //ensure path of /test  
  29.                         new EnsurePath(C_PATH).ensure(client.getZookeeperClient());  
  30.   
  31.                         final LeaderSelector leaderSelector = new LeaderSelector(client, C_PATH, new LeaderSelectorListener() {  
  32.                             @Override  
  33.                             public void takeLeadership(CuratorFramework client) throws Exception {  
  34.                                 try {  
  35.                                     int timeMilliSeconds = 6000;  
  36.                                     System.out.println("======" + APP_NAME + " take leader ship, will do some task, will hold time milli seconds=" + timeMilliSeconds);  
  37.   
  38.                                     for(int i = 0; i < 6; i++){  
  39.                                         System.out.println("===" + APP_NAME + " sleep " + i);  
  40.                                         Thread.sleep(1000);  
  41.                                     }  
  42.                                 }catch (Exception e){  
  43.                                     e.printStackTrace();  
  44.                                 }  
  45.                             }  
  46.   
  47.                             @Override  
  48.                             public void stateChanged(CuratorFramework client, ConnectionState newState) {  
  49.   
  50.                             }  
  51.                         });  
  52.   
  53.                         leaderSelector.start();  
  54.   
  55.                         Thread.sleep(Integer.MAX_VALUE);  
  56.                         client.close();  
  57.                     }catch (Exception e){  
  58.                         e.printStackTrace();  
  59.                     }  
  60.                 }  
  61.             }).start();  
  62.         }catch (Exception e){  
  63.             e.printStackTrace();  
  64.         }  
  65.     }  
  66. }  


[java]  view plain  copy
  1. package com.collonn.javaUtilMvn.zookeeper.curator.LeaderElection;  
  2.   
  3. import org.apache.curator.RetryPolicy;  
  4. import org.apache.curator.framework.CuratorFramework;  
  5. import org.apache.curator.framework.CuratorFrameworkFactory;  
  6. import org.apache.curator.framework.recipes.leader.LeaderSelector;  
  7. import org.apache.curator.framework.recipes.leader.LeaderSelectorListener;  
  8. import org.apache.curator.framework.state.ConnectionState;  
  9. import org.apache.curator.retry.ExponentialBackoffRetry;  
  10. import org.apache.curator.utils.EnsurePath;  
  11.   
  12. public class LeaderListener3 {  
  13.     public static final String C_PATH = "/TestLeader";  
  14.     public static final String CHARSET = "UTF-8";  
  15.     public static final String APP_NAME = "app3";  
  16.   
  17.     public static void main(String[] args) {  
  18.         try {  
  19.             new Thread(new Runnable() {  
  20.                 @Override  
  21.                 public void run() {  
  22.                     try{  
  23.                         String zookeeperConnectionString = "127.0.0.1:2181";  
  24.                         RetryPolicy retryPolicy = new ExponentialBackoffRetry(10003);  
  25.                         CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);  
  26.                         client.start();  
  27.   
  28.                         //ensure path of /test  
  29.                         new EnsurePath(C_PATH).ensure(client.getZookeeperClient());  
  30.   
  31.                         final LeaderSelector leaderSelector = new LeaderSelector(client, C_PATH, new LeaderSelectorListener() {  
  32.                             @Override  
  33.                             public void takeLeadership(CuratorFramework client) throws Exception {  
  34.                                 try {  
  35.                                     int timeMilliSeconds = 6000;  
  36.                                     System.out.println("======" + APP_NAME + " take leader ship, will do some task, will hold time milli seconds=" + timeMilliSeconds);  
  37.   
  38.                                     for(int i = 0; i < 6; i++){  
  39.                                         System.out.println("===" + APP_NAME + " sleep " + i);  
  40.                                         Thread.sleep(1000);  
  41.                                     }  
  42.                                 }catch (Exception e){  
  43.                                     e.printStackTrace();  
  44.                                 }  
  45.                             }  
  46.   
  47.                             @Override  
  48.                             public void stateChanged(CuratorFramework client, ConnectionState newState) {  
  49.   
  50.                             }  
  51.                         });  
  52.   
  53.                         leaderSelector.start();  
  54.   
  55.                         Thread.sleep(Integer.MAX_VALUE);  
  56.                         client.close();  
  57.                     }catch (Exception e){  
  58.                         e.printStackTrace();  
  59.                     }  
  60.                 }  
  61.             }).start();  
  62.         }catch (Exception e){  
  63.             e.printStackTrace();  
  64.         }  
  65.     }  
  66. }  


[java]  view plain  copy
  1. package com.collonn.javaUtilMvn.zookeeper.curator.LeaderElection;  
  2.   
  3. import org.apache.curator.RetryPolicy;  
  4. import org.apache.curator.framework.CuratorFramework;  
  5. import org.apache.curator.framework.CuratorFrameworkFactory;  
  6. import org.apache.curator.framework.recipes.leader.LeaderSelector;  
  7. import org.apache.curator.framework.recipes.leader.LeaderSelectorListener;  
  8. import org.apache.curator.framework.state.ConnectionState;  
  9. import org.apache.curator.retry.ExponentialBackoffRetry;  
  10. import org.apache.curator.utils.EnsurePath;  
  11.   
  12. public class LeaderListener4 {  
  13.     public static final String C_PATH = "/TestLeader";  
  14.     public static final String CHARSET = "UTF-8";  
  15.     public static final String APP_NAME = "app4";  
  16.   
  17.     public static void main(String[] args) {  
  18.         try {  
  19.             new Thread(new Runnable() {  
  20.                 @Override  
  21.                 public void run() {  
  22.                     try{  
  23.                         String zookeeperConnectionString = "127.0.0.1:2181";  
  24.                         RetryPolicy retryPolicy = new ExponentialBackoffRetry(10003);  
  25.                         CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);  
  26.                         client.start();  
  27.   
  28.                         //ensure path of /test  
  29.                         new EnsurePath(C_PATH).ensure(client.getZookeeperClient());  
  30.   
  31.                         final LeaderSelector leaderSelector = new LeaderSelector(client, C_PATH, new LeaderSelectorListener() {  
  32.                             @Override  
  33.                             public void takeLeadership(CuratorFramework client) throws Exception {  
  34.                                 try {  
  35.                                     int timeMilliSeconds = 6000;  
  36.                                     System.out.println("======" + APP_NAME + " take leader ship, will do some task, will hold time milli seconds=" + timeMilliSeconds);  
  37.   
  38.                                     for(int i = 0; i < 6; i++){  
  39.                                         System.out.println("===" + APP_NAME + " sleep " + i);  
  40.                                         Thread.sleep(1000);  
  41.                                     }  
  42.                                 }catch (Exception e){  
  43.                                     e.printStackTrace();  
  44.                                 }  
  45.                             }  
  46.   
  47.                             @Override  
  48.                             public void stateChanged(CuratorFramework client, ConnectionState newState) {  
  49.   
  50.                             }  
  51.                         });  
  52.   
  53.                         leaderSelector.start();  
  54.   
  55.                         Thread.sleep(Integer.MAX_VALUE);  
  56.                         client.close();  
  57.                     }catch (Exception e){  
  58.                         e.printStackTrace();  
  59.                     }  
  60.                 }  
  61.             }).start();  
  62.         }catch (Exception e){  
  63.             e.printStackTrace();  
  64.         }  
  65.     }  
  66. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值