八、curator recipes之选举主节点LeaderSelector
简介
前面我们看到LeaderLatch对于选举的实现:https://www.cnblogs.com/lay2017/p/10264300.html
节点在加入选举以后,除非程序结束或者close()退出选举,否则加点自加入选举以后将持续持有或者保持对主节点的竞争。
recipes的另外一个实现Leader Election则不同,被选为主节点的节点任务如果执行完就会放弃主节点,然后由剩下的节点进行主节点竞争。如果你希望已经执行完的主节点再次加入主节点选举那么你需要调用autoRequeue()方法去自动加入。
官方文档:http://curator.apache.org/curator-recipes/leader-election.html
javaDoc:http://curator.apache.org/apidocs/org/apache/curator/framework/recipes/leader/LeaderSelector.html
代码示例
以下代码你可以通过是否调用autoRequeue来看看console的输出有什么不同
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; public class LeaderElectionDemo {
private static CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(3000, 3));
private static String path = "/mutex/path/0001";
static {
client.start();
} public static void main(String[] args) throws InterruptedException {
startThread0();
Thread.sleep(10);
startThread1();
Thread.sleep(50000);
client.close();
} public static void startThread0() {
new Thread(() -> {
LeaderSelector selector = new LeaderSelector(client, path, new LeaderSelectorListener() {
@Override
public void takeLeadership(CuratorFramework curatorFramework) throws Exception {
System.out.println("thread0 is leader");
} @Override
public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) {
System.out.println("thread0 state changed,state is " + connectionState.isConnected());
}
});
selector.autoRequeue();
selector.start();
}).start();
} public static void startThread1() {
new Thread(() -> {
LeaderSelector selector = new LeaderSelector(client, path, new LeaderSelectorListener() {
@Override
public void takeLeadership(CuratorFramework curatorFramework) throws Exception {
System.out.println("thread1 is leader");
} @Override
public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) {
System.out.println("thread1 state changed,state is " + connectionState.isConnected());
}
});
selector.autoRequeue();
selector.start();
}).start();
}
}
八、curator recipes之选举主节点LeaderSelector的更多相关文章
- 五、curator recipes之选举主节点Leader Latch
简介 在分布式计算中,主节点选举是为了把某个进程作为主节点来控制其它节点的过程.在选举结束之前,我们不知道哪个节点会成为主节点.curator对于主节点选举有两种实现方式,本文示例演示Latch的实现 ...
- org.apache.curator:master选举和分布式锁
1. master选举(LeaderSelector) 1)LeaderSelector构造函数 在leaderPath上建立分布式锁:mutex = new InterProcessMutex(cl ...
- 十八、curator recipes之DistributedDelayQueue
简介 curator实现了类似DelayQueue的分布式延迟队列 官方文档:http://curator.apache.org/curator-recipes/distributed-delay-q ...
- 二十一、curator recipes之TreeCache
简介 curator的TreeCache允许对某个路径的数据和路径变更以及其下所有子孙节点的数据和路径变更进行监听. 官方文档:http://curator.apache.org/curator-re ...
- 二十、curator recipes之NodeCache
简介 Curator的NodeCache允许你监听一个节点,当节点数据更改或者节点被删除的时候将会触发监听. 官方文档:http://curator.apache.org/curator-recipe ...
- 十九、curator recipes之PathChildrenCache
简介 curator可以监听路径下子节点的变更操作,如创建节点,删除节点 官方文档:http://curator.apache.org/curator-recipes/path-cache.html ...
- 十七、curator recipes之DistributedPriorityQueue
简介 官方文档:http://curator.apache.org/curator-recipes/distributed-priority-queue.html javaDoc:http://cur ...
- 十六、curator recipes之DistributedIdQueue
简介 curator实现了一种分布式ID队列,也是遵循FIFO原则,比普通队列新增的一个点是ID队列可以根据ID对队列元素进行操作,比如移除该元素. 官方文档:http://curator.apach ...
- 十五、curator recipes之DistributedQueue
简介 curator实现了先入先出的分布式消息队列,它采用的是zookeeper的持久化有序节点. 官方文档:http://curator.apache.org/curator-recipes/dis ...
随机推荐
- “全栈2019”Java第九十章:内部类可以向上或向下转型吗?
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- Hibernate 框架学习
什么是hibernate框架? 1.它是应用在javaee 三层架构中的dao层 它的底层就是JDBC 它对JDBC进行了封装,好处就是不用写jdbc的代码,和sql语句,它是一个开源的轻量级框架,现 ...
- PRINT语句详解
首先摘自联机丛书中的PRINT语法如下: PRINT msg_str | @local_variable | string_exp 以上可看出PRINT语句的输出参数均为字符或字符串的常量或变量 - ...
- centos7安装nginx(自定义安装文件夹)
一.安装所需要的依赖软件 1.gcc:nginx编译依赖gcc环境 #yum install gcc-c++ 2.pcre:(Perl Compatible Regular Expressions)是 ...
- BZOJ 1412--狼和羊的故事(最小割)
1412: [ZJOI2009]狼和羊的故事 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3316 Solved: 1664[Submit][St ...
- logstash-out-mongodb实现elasticsearch到Mongodb的数据同步
本文主要实现将Elasticsearch中的索引数据Index同步到Mongodb中的集合collection中. 0.前提 1)已经安装好源数据库:elasticsearch V2.X; 2)已经安 ...
- Enum枚举法
java enum(枚举)使用详解 + 总结 enum 的全称为 enumeration, 是 JDK 1.5 中引入的新特性,存放在 java.lang 包中. 下面是我在使用 enum 过程 ...
- 利用python 学习数据分析 (学习二)
内容学习自: Python for Data Analysis, 2nd Edition 就是这本 纯英文学的很累,对不对取决于百度翻译了 前情提要: 各种方法贴: https://w ...
- PHP和javascript中url编码解码详解
在实际开发中,我们可能会遇到路径编码解码的问题,下面总结了一下: PHP中: 1.urlencode(编码),urldecode(解码) $a = urlencode('http://www.baid ...
- BootStrap Modal 点击空白时自动关闭
本文为大家讲解的是如何禁用 BootStrap Modal 点击空白时自动关闭的方法,感兴趣的同学参考下. 方法如下 $('#myModal').modal({backdrop: 'static', ...