Apache ZooKeeper - Leader Election使用场景
文章目录
- 需求
- Leader Election
- Case
- Code
- 源码分析
需求
Leader Election
https://curator.apache.org/getting-started.html
Demo示例
Case
官方demo
https://github.com/apache/curator/tree/master/curator-examples/src/main/java/leader
Code
我们自己也写个demo 吧
<dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>5.0.0</version><exclusions><exclusion><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.5.8</version></dependency>
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.recipes.leader.LeaderSelectorListenerAdapter;
import org.apache.curator.retry.ExponentialBackoffRetry;import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;public class LeaderSelectorTest {private static final String CONNECT_STR="192.168.126.135:2181";private static RetryPolicy retryPolicy=new ExponentialBackoffRetry( 5*1000, 10 );private static CuratorFramework curatorFramework;private static CountDownLatch countDownLatch = new CountDownLatch(1);public static void main(String[] args) throws InterruptedException {String appName = System.getProperty("appName");CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(CONNECT_STR, retryPolicy);LeaderSelectorTest.curatorFramework = curatorFramework;curatorFramework.start();LeaderSelectorListener listener = new LeaderSelectorListenerAdapter(){// 选举 leader后的回调@Overridepublic void takeLeadership(CuratorFramework client) throws Exception{// this callback will get called when you are the leader// do whatever leader work you need to and only exit// this method when you want to relinquish leadershipSystem.out.println(" LEADER . 【"+appName +"】, Pre Warm Cache ");// 模拟业务耗时操作TimeUnit.SECONDS.sleep(5);}};LeaderSelector selector = new LeaderSelector(curatorFramework, "/cache_warmer_leader", listener);selector.autoRequeue(); // not required, but this is behavior that you will probably expectselector.start();countDownLatch.await();}
}
zk中的数据
源码分析
我们来看下 curator是怎么实现的?
Apache ZooKeeper - Leader Election使用场景相关推荐
- Apache Curator Leader Election
http://blog.csdn.net/collonn/article/details/43968655 用于Leader选举,也可以用Shared Reentrant Lock来实现. 如果需要集 ...
- Apache ZooKeeper - Leader 选举 如何保证分布式数据的一致性
文章目录 Pre 流程图 Leader 的协调过程 ZK 是如何实现的 广播模式 恢复模式 源码实现 小结 Pre Apache ZooKeeper - 选举Leader源码流程深度解析 在 ZooK ...
- Apache ZooKeeper - ZK的基本特性与节点应用场景一览
文章目录 概述 ZK产生的背景 安装 JDK依赖 下载 解压 修改配置文件 启动 / 停止 连接服务器 配置文件说明 ZK 核心概念 (文件系统数据结构+监听通知机制) 文件系统数据结构 6种Node ...
- ZooKeeper 典型的应用场景
Zookeeper 从设计模式角度来看,是一个基于观察者模式设计的分布式服务管理框架,它负责存储和管理大家都关心的数据,然后接受观察者的注册,一旦这些数据的状态发生变化,Zookeeper 就将负责通 ...
- Apache ZooKeeper - 使用源码启动ZK集群模式
文章目录 Pre 配置总览 端口说明 Node 1 [zoo1.cfg ] [myid] Node 2 [zoo2.cfg ] [myid] Node 3 [zoo3.cfg ] [myid] 启动集 ...
- zookeeper基本原理及适用场景 转:http://blog.chinaunix.net/uid-26748613-id-4536290.html
1.1 zookeeper简介 Zookeeper 是 Hadoop 生态系统中的协同实现,是Hadoop集群管理的一个必不可少的模块,它主要来控制集群中的数据,如它管理Hadoop集群中的NameN ...
- zookeeper学习03 使用场景
zookeeper实际应用场景 zookeeper能够实现哪些场景 1)订阅发布/配置中心 watcher机制 统一配置管理(disconf) 实现配置信息的集中式原理和数据的动态更新 实现配置中心有 ...
- JAVA EE Apache Zookeeper / Google Chubby
s http://zookeeper.apache.org/ Apache ZooKeeper is an effort to develop and maintain an open-source ...
- zookeeper 原理及使用场景2
数据发布订阅/ 配置中心 实现配置信息的集中式管理和数据的动态更新 实现配置中心有两种模式:push .pull. 长轮训 zookeeper采用的是推拉相结合的方式. 客户端向服务器端注册自己需要关 ...
最新文章
- ASP.NET 下载文件方式
- 做了5年Java,java读写锁使用
- 通俗说一下python和人工智能有什么关系
- win10电脑去掉快捷箭头_【搞机作战室】Win10系统电脑 常用快捷方式按键 (快捷键)介绍...
- ElementUI弹出新增窗口
- docker pull 下载一半_Docker 从入门到掉坑
- Linux 配置jdk
- 大厂内部资料 | Redis 性能优化的 13 条军规!
- ubuntu vscode_17. ubuntu设为服务器+vscode远程开发
- etcd nginx 容器_Etcd+confd实现动态修改nginx文件
- freeswitch安装步骤(源码编译安装)
- 前端测试之用户体验测试
- C++for循环经典九九乘法表打印
- android 蓝牙打印乱码,蓝牙打印机打印中文乱码
- python 获取微信语音消息_GitHub - advancer-debug/Python_wechat: Python实现微信自动回复及接入聊天机器人...
- arcgis构造工具没了_在arcgis中如何调出数据显示工具栏
- Halcon union_straight_contours_xld详解
- 华为钱包扫码云闪付_华为钱包、云闪付,NFC支付要这样使用!!!-已回复
- MySQL——Student、Course、Teacher、SC SQL查询练习题
- PPP协议身份验证PAP和CHAP