使用 ZooKeeper 同步集群配置
用 ZooKeeper 同步集群配置,当需要修改所有节点配置时,将配置更新到 ZooKeeper 的一个节点,引起这个节点数据发生变化,其他所有需要同步配置的节点上的本地 Watcher 会立即发现节点状态的变化,并将最新数据更新到本地
把官方Demo 改了改,响应 Watcher 时用 ConfFileUtil 更新一下本地配置就可以了
view plaincopyprint?http://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.iyunv.com/assets/CODE_ico.pnghttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.iyunv.com/assets/ico_fork.svg
[*]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;
[*] args = Constant.ZKPEER;
[*] args = Constant.ZNODE;
[*] args = "";
[*] String hostPort = args;
[*] String znode = args;
[*] String filename = args;
[*] 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);
[*] }
[*]}
上面的 Executor 用来和 znode 建立和维护连接
DataMonitor 仍旧负责接收事件 处理结果,这个类基本没改。
触发 Watcher 时, DataMonitor 委托 Executor 来更新本地配置
完整代码见 GITHUB
更新节点时需注意一点
view plaincopyprint?http://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.iyunv.com/assets/CODE_ico.pnghttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.iyunv.com/assets/ico_fork.svg
[*]zk.setData(Constant.ZNODE, bytes, -1, null, null);
这个 byte[] 是有大小限制的,并且如果它超过了限制,ZooKeeper 也不会告诉你,官方文档说的是 1M,这个我没测试
当更新数据不成功且没有任何异常时,就需要检查这个数组是不是太长了
页:
[1]