是否可以在进程中启动 Zookeeper 服务器实例,例如用于单元测试?

发布于 2025-01-05 20:17:35 字数 72 浏览 1 评论 0原文

调用 org.apache.zookeeper.server.quorum.QuorumPeerMain.main() 不起作用。

Calling org.apache.zookeeper.server.quorum.QuorumPeerMain.main() isn't working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

陪你到最终 2025-01-12 20:17:35

Netfix 开源 Curator 一个可以使用的框架Zookeeper就更方便了。它内置了测试服务器类。只需将此测试依赖项添加到您的项目描述符中,无论是 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"));

Netfix opensourced Curator a framework to make use of Zookeeper even more convenient. It has build in test server class. Just add this test dependency to your project descriptor be it maven, gradle or else:

org.apache.curator:curator-framework:4.0.1
org.apache.curator:curator-test:4.0.1

And here are the test essentials.

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();
}

With cli creating any test data is very easy (requires the curator-framework dependency).

cli.create()
   .creatingParentsIfNeeded()
   .forPath("/a1", "testvalue".getBytes("UTF-8"));
权谋诡计 2025-01-12 20:17:35

要启动 ZooKeeper,您必须执行 ZooKeeperServerMain 类。

您可以使用以下代码以嵌入模式启动 ZooKeeper

Properties startupProperties = ...

QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig();
try {
    quorumConfiguration.parseProperties(startupProperties);
} catch(Exception e) {
    throw new RuntimeException(e);
}

zooKeeperServer = new ZooKeeperServerMain();
final ServerConfig configuration = new ServerConfig();
configuration.readFrom(quorumConfiguration);

new Thread() {
    public void run() {
        try {
            zooKeeperServer.runFromConfig(configuration);
        } catch (IOException e) {
            log.error("ZooKeeper Failed", e);
        }
    }
}.start();

To start ZooKeeper you have to execute ZooKeeperServerMain class.

You can use following code to start ZooKeeper in embedded mode.

Properties startupProperties = ...

QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig();
try {
    quorumConfiguration.parseProperties(startupProperties);
} catch(Exception e) {
    throw new RuntimeException(e);
}

zooKeeperServer = new ZooKeeperServerMain();
final ServerConfig configuration = new ServerConfig();
configuration.readFrom(quorumConfiguration);

new Thread() {
    public void run() {
        try {
            zooKeeperServer.runFromConfig(configuration);
        } catch (IOException e) {
            log.error("ZooKeeper Failed", e);
        }
    }
}.start();
沙沙粒小 2025-01-12 20:17:35

你可以使用这样的东西。

int clientPort = 21818; // none-standard
int numConnections = 5000;
int tickTime = 2000;
String dataDirectory = System.getProperty("java.io.tmpdir");

File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile();

ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime);
NIOServerCnxn.Factory standaloneServerFactory = new NIOServerCnxn.Factory(new InetSocketAddress(clientPort), numConnections);

standaloneServerFactory.startup(server); // start the server.

要关闭它,只需调用standaloneServerFactory.shutdown()

You can use something like this.

int clientPort = 21818; // none-standard
int numConnections = 5000;
int tickTime = 2000;
String dataDirectory = System.getProperty("java.io.tmpdir");

File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile();

ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime);
NIOServerCnxn.Factory standaloneServerFactory = new NIOServerCnxn.Factory(new InetSocketAddress(clientPort), numConnections);

standaloneServerFactory.startup(server); // start the server.

And to shut it down just call standaloneServerFactory.shutdown()

再浓的妆也掩不了殇 2025-01-12 20:17:35

通过添加临时端口的使用(由 zkPort 所示),以 1 的答案为基础并更新了最新的 ZK API:

int tickTime = 2000;
int numConnections = 5000;
String dataDirectory = System.getProperty("java.io.tmpdir");

File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile();

ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime);
standaloneServerFactory = ServerCnxnFactory.createFactory(0, numConnections);
int zkPort = standaloneServerFactory.getLocalPort();

standaloneServerFactory.startup(server);

Building on 1's answer by adding the use of an ephemeral port (shown by zkPort) and updated for latest ZK API:

int tickTime = 2000;
int numConnections = 5000;
String dataDirectory = System.getProperty("java.io.tmpdir");

File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile();

ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime);
standaloneServerFactory = ServerCnxnFactory.createFactory(0, numConnections);
int zkPort = standaloneServerFactory.getLocalPort();

standaloneServerFactory.startup(server);
未蓝澄海的烟 2025-01-12 20:17:35
ServerConfig config = new ServerConfig();
config.parse(new String[] {port, dir});
ZooKeeperServerMain zk = new ZooKeeperServerMain();
zk.runFromConfig(config);
ServerConfig config = new ServerConfig();
config.parse(new String[] {port, dir});
ZooKeeperServerMain zk = new ZooKeeperServerMain();
zk.runFromConfig(config);
浅浅 2025-01-12 20:17:35

GeoffBourne 答案的更新版本。

    int clientPort = 2199; // not standard
    int numConnections = 5000;
    int tickTime = 2000;
    String dataDirectory = System.getProperty("java.io.tmpdir");

    File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile();

    ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime);
    ServerCnxnFactory factory = new NIOServerCnxnFactory();
    factory.configure(new InetSocketAddress(clientPort), numConnections);

    factory.startup(server); // start the server.

    // ...shutdown some time later
    factory.shutdown();

An updated version of GeoffBourne's answer.

    int clientPort = 2199; // not standard
    int numConnections = 5000;
    int tickTime = 2000;
    String dataDirectory = System.getProperty("java.io.tmpdir");

    File dir = new File(dataDirectory, "zookeeper").getAbsoluteFile();

    ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTime);
    ServerCnxnFactory factory = new NIOServerCnxnFactory();
    factory.configure(new InetSocketAddress(clientPort), numConnections);

    factory.startup(server); // start the server.

    // ...shutdown some time later
    factory.shutdown();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文