bigywater.tech
ZooKeeper · 50

动态重配置 reconfig

ZooKeeper 3.5 之后可以在不重启全集群的情况下增删成员(reconfig)。这篇开启动态配置,实际执行 config、reconfig -add、reconfig -remove,看版本号、新节点同步和被移除节点的状态。

Reconfig is disabled
默认配置下执行 reconfig 的错误
version = zxid
config 的版本号就是提交这次配置的事务 zxid
639 ms
reconfig -add 后新节点启动到成为 follower 的时间

默认是禁用的

实测 · 本机执行输出
  reconfig -remove 3 → KeeperErrorCode = Reconfig is disabled

动态配置与 config 命令

实测 · 本机执行输出
  server.1=127.0.0.1:7400:7500:participant;127.0.0.1:7300
  server.2=127.0.0.1:7401:7501:participant;127.0.0.1:7301
  server.3=127.0.0.1:7402:7502:participant;127.0.0.1:7302
  version=100000000

动态配置要求把 clientPort 从静态配置里移到成员行里(;127.0.0.1:7300),并且静态配置里不能再有 clientPortAddress(我一开始漏了这个,节点直接报「Invalid config」退出)。我还用 -Dzookeeper.skipACL=yes 跳过了 reconfig 的权限检查,生产环境应当用超级用户或 ACL(第 71 篇)。

加入节点

实测 · 本机执行输出
  reconfig -add → 
  Committed new configuration:
  server.1=127.0.0.1:7400:7500:participant;127.0.0.1:7300
  server.2=127.0.0.1:7401:7501:participant;127.0.0.1:7301
  server.3=127.0.0.1:7402:7502:participant;127.0.0.1:7302
  server.4=127.0.0.1:7403:7503:participant;127.0.0.1:7303
  version=100000036
  新节点 z50-4 Mode = follower(615 ms 后);znode 数 = 55 | 其它节点 znode 数 = 55
  现在的成员(config): 100000036 127.0.0.1:7301,127.0.0.1:7300,127.0.0.1:7303,127.0.0.1:7302

移除节点

实测 · 本机执行输出
  reconfig -remove 1 → 
  Committed new configuration:
  server.2=127.0.0.1:7401:7501:participant;127.0.0.1:7301
  server.3=127.0.0.1:7402:7502:participant;127.0.0.1:7302
  server.4=127.0.0.1:7403:7503:participant;127.0.0.1:7303
  version=10000003a
  被移除的 1 号节点 Mode = follower | 进程还在: True
  其它节点: {'z50-2': 'leader', 'z50-3': 'follower', 'z50-4': 'follower'}
  leader 的 zk_synced_followers = 3 | 被移除节点自己看到的 config: server.2=127.0.0.1:7401:7501:participant;127.0.0.1:7301 | server.3=127.0.0.1:7402:7502:participant;127.0.0.1:7302 | server.4=127.0.0.1:7403:7503:participant;127.0.0.1:7303 | version=10000003a

分步演示

动态重配置
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 / ZooKeeper 3.9.6(brew 安装的 jar,用 java -cp 直接启动自己的实例,OpenJDK 17 Temurin,JVM 堆 128 MiB)/ 本机回环端口 7300+,数据在临时目录;默认 tickTime=2000;计时用 time.perf_counter,数字每次运行会有小幅抖动。动态配置伪集群(3 + 1);reconfig 用官方 ZooKeeperMain;skipACL=yes
    • 没有验证:权限配置下的 reconfig、同时增加多个节点(第 51 篇)、观察者与 participant 互相转换、reconfig 失败(比如破坏多数派)时的错误、被移除节点长期运行的后果
    京ICP备2026067502号-1京公网安备11010502063487号