使用 ZooKeeper 同步集群配置
来源:互联网 发布:电商项目面试题java 编辑:程序博客网 时间:2024/04/30 00:26
用 ZooKeeper 同步集群配置,当需要修改所有节点配置时,将配置更新到 ZooKeeper 的一个节点,引起这个节点数据发生变化,
其他所有需要同步配置的节点上的本地 Watcher 会立即发现节点状态的变化,并将最新数据更新到本地
把官方Demo 改了改,响应 Watcher 时用 ConfFileUtil 更新一下本地配置就可以了
public class Executor implements Watcher, Runnable,DataMonitor.DataMonitorListener {String znode;DataMonitor dm;ZooKeeper zk;String filename;public Executor(String hostPort, String znode, String filename)throws KeeperException, IOException {this.filename = filename;zk = new ZooKeeper(hostPort, 3000, this);dm = new DataMonitor(zk, znode, this);}public static void main(String[] args) {args = new String[3];args[0] = Constant.ZKPEER;args[1] = Constant.ZNODE;args[2] = "";String hostPort = args[0];String znode = args[1];String filename = args[2];try {new Executor(hostPort, znode, filename).run();} catch (Exception e) {e.printStackTrace();}}public void process(WatchedEvent event) {dm.process(event);}public void run() {try {synchronized (this) {while (!dm.dead) {wait();}}} catch (InterruptedException e) {}}public void closing(int rc) {synchronized (this) {notifyAll();}}/** * 处理数据 */public void exists(byte[] data) {System.out.println("...Invoked:Executor.exists(byte[] data)...");ConfFileUtil.sync(data);}}
DataMonitor 仍旧负责接收事件 处理结果,这个类基本没改。
触发 Watcher 时, DataMonitor 委托 Executor 来更新本地配置
完整代码见 GITHUB
更新节点时需注意一点
zk.setData(Constant.ZNODE, bytes, -1, null, null);
这个 byte[] 是有大小限制的,并且如果它超过了限制,ZooKeeper 也不会告诉你,官方文档说的是 1M,这个我没测试
当更新数据不成功且没有任何异常时,就需要检查这个数组是不是太长了
0 0
- 使用 ZooKeeper 同步集群配置
- Hadoop使用Zookeeper集群配置
- 使用ZooKeeper实现配置同步
- 使用ZooKeeper实现配置同步
- 使用ZooKeeper实现配置同步
- 使用ZooKeeper实现配置同步
- zookeeper集群的配置及使用
- Ubuntu 安装Zookeeper并使用集群配置
- 使用ZooKeeper实现配置同步(转)
- ZooKeeper的集群配置(使用于单机伪集群)
- Zookeeper集群配置
- Zookeeper集群配置
- zookeeper集群安装配置
- ZooKeeper集群安装配置
- zookeeper集群安装配置
- Zookeeper集群配置部署
- zookeeper集群配置安装
- ZooKeeper集群配置
- (总结)Oracle 11g常用管理命令(用户、表空间、权限)
- Linux定时任务cron
- 总结一下ubuntu的chmod和chown命令
- PC版本游戏--cocos2d-x中文乱码解决办法(GBK到UTF-8编码)及其fatal error: iconv\iconv.h: No such file or directory问题
- CXF 拦截器预处理request、response消息
- 使用 ZooKeeper 同步集群配置
- 黑马程序员----java基础之面向对象二
- android IPC通信中的UID和PID识别 -》 clearCallingIdentity 的作用
- 无线路由对桥接和中继之间区别的理解
- 单向链表总处于排序
- IMP-00010: 不是有效的导出文件,头部验证失败。
- treap树模版
- 嵌套类与局部类
- 查看windows操作系统的默认编码