java 是否可以在进程中启动 Zookeeper 服务器实例,例如用于单元测试
pengyingh
阅读:779
2023-05-31 16:42:55
评论:0
调用 org.apache.zookeeper.server.quorum.QuorumPeerMain.main() 不起作用。
请您参考如下方法:
Netfix 开源 Curator一个让使用 Zookeeper 更加方便的框架。它内置了测试服务器类。只需将此 test 依赖项添加到您的项目描述符中,无论是 maven、gradle 还是其他:
org.apache.curator:curator-framework:4.0.1
org.apache.curator:curator-test:4.0.1
这里是测试要点。
TestingServer zkTestServer;
CuratorFramework cli;
@Before
public void startZookeeper() throws Exception {
zkTestServer = new TestingServer(2181);
cli = CuratorFrameworkFactory.newClient(zkTestServer.getConnectString(), new RetryOneTime(2000));
cli.start();
}
@After
public void stopZookeeper() throws IOException {
cli.close();
zkTestServer.stop();
}
使用 cli
创建任何测试数据都非常容易(需要 curator-framework
依赖项)。
cli.create()
.creatingParentsIfNeeded()
.forPath("/a1", "testvalue".getBytes("UTF-8"));
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。