启动嵌入式Zookeeper时如何避免"RuntimeException: My id 0 not in the peer list"?

How to avoid "RuntimeException: My id 0 not in the peer list" when starting embedded Zookeeper?

我正在尝试使用 QuorumPeerMain.runFromConfig 启动一个 zookeeper 节点 在 BeforeAll 方法中,以便我可以在测试中使用它 (它嵌入在同一个 JVM 中)。我越来越 Invalid configuration, only one server specified (ignoring) 其次是 java.lang.RuntimeException: My id 0 not in the peer list.

我的配置是:

dataDir=/tmp/zookeeper
clientPort=2181
maxClientCnxns=0
server.0=127.0.0.1:2888:3888

应该引用节点本身的最后一行在 "Invalid configuration" 错误消息之后被丢弃,然后 zookeeper 抛出异常,因为它的 id 不在列表中。如果 zookeeper 没有丢弃它,它的 id 0 就会出现在列表中。如何避免此 RuntimeException?

或者,是否有更好的方法从 BeforeAll 运行 zookeeper?

试用 Apache Curator 测试框架。使用起来更舒服。有关详细信息,请参阅:TestingServer javadoc。

您可以在github

中找到测试示例

如配置中所示,ID 必须是唯一的且介于 1 到 255 之间。您设置的 ID 为 0 不在此集合中,这会导致此错误。

文档:https://zookeeper.apache.org/doc/r3.1.2/zookeeperAdmin.html#sc_zkMulitServerSetup