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

Java 是否可以在过程中启动zookeeper服务器实例,例如用于单元测试?,java,unit-testing,apache-zookeeper,Java,Unit Testing,Apache Zookeeper,调用org.apache.zookeeper.server.quorum.QuorumPeerMain.main()不起作用。您可以使用类似的方法 int clientPort = 21818; // none-standard int numConnections = 5000; int tickTime = 2000; String dataDirectory = System.getProperty("java.io.tmpdir"); File dir = new File(dataD

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

您可以使用类似的方法

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

即可启动
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();
通过添加临时端口(如
zkPort
所示)并针对最新的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);
Netfix开源了一个框架,使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框架
依赖项)


杰夫伯恩答案的更新版本

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

此外,curator框架附带了一个内存中的zookeeper实现,用于单元测试。curator确实附带了一个测试zookeeper,但它不在内存中。它将数据存储在磁盘上的临时目录中。这是进行单元测试的方法。使用QuorumPeerMain仅适用于多服务器(群集)Zookeeper。这在BeforeClass的上下文中很有用,但在测试之间关闭它(例如,清除节点)是非常重要的——我放弃了,转而使用基于管理员的解决方案。(THEAD.Stuts/Cutin in the Stand将停止JUnitRunner)您知道如何在C++项目中设置这一点吗?Serup,ZooKever是java PraseSee,您能举一个启动属性的例子来设置吗?您不能用ZooKever运行这个方法,因为它会阻止ZK.RunFixFunFigg(CONFIG)之后的代码,即代码。您可能无法以这种方式启动zoo keeper服务器,因为它会阻止所有后续代码在standaloneServerFactory.startup(服务器)之后执行;对我来说,如果没有使用Curator 2.10版的cli.start(),它就无法工作。0@MichaelB谢谢你!我还需要先调用
cli.start()
,然后它就开始工作了。嗯哼,策展人可以用来监控一组动物园管理员吗?如果其中一个动物园管理员实例出现故障,它可以用来自动重启吗?
cli.create()
   .creatingParentsIfNeeded()
   .forPath("/a1", "testvalue".getBytes("UTF-8"));
    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();