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

时间:2022-02-23 07:02:02

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

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

6 个解决方案

#1


44  

To start ZooKeeper you have to execute ZooKeeperServerMain class.

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

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

您可以使用以下代码以嵌入模式启动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();

#2


55  

Netfix opensourced Curator a framework to make use of Zookeeper even more convenient. It has build in test server class. If you use Maven just add this to your project's pom.xml:

Netfix opensourced策展人使用Zookeeper的框架更加方便。它内置了测试服务器类。如果您使用Maven,只需将其添加到项目的pom.xml中:

<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-test</artifactId>
    <version>2.2.0-incubating</version>
    <scope>test</scope>
</dependency>

And here are the test essentials.

以下是测试要点。

TestingServer zkTestServer;

@Before
public void startZookeeper() throws Exception {
    zkTestServer = new TestingServer(2181);
    cli = CuratorFrameworkFactory.newClient(zkTestServer.getConnectString(), new RetryOneTime(2000));
}

@After
public void stopZookeeper() throws IOException {
    cli.close();
    zkTestServer.stop();
}

With cli creating any test data is very easy.

使用cli创建任何测试数据都非常容易。

cli.create().forPath("/a1", "testvalue".getBytes());

#3


12  

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

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

#4


3  

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

通过添加使用临时端口(由zkPort显示)并针对最新的ZK API进行更新来建立1的答案:

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

#5


2  

ServerConfig config = new ServerConfig();
config.parse(new String[] {port, dir});
ZooKeeperServerMain zk = new ZooKeeperServerMain();
zk.runFromConfig(config);

#6


0  

An updated version of GeoffBourne's answer.

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

#1


44  

To start ZooKeeper you have to execute ZooKeeperServerMain class.

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

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

您可以使用以下代码以嵌入模式启动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();

#2


55  

Netfix opensourced Curator a framework to make use of Zookeeper even more convenient. It has build in test server class. If you use Maven just add this to your project's pom.xml:

Netfix opensourced策展人使用Zookeeper的框架更加方便。它内置了测试服务器类。如果您使用Maven,只需将其添加到项目的pom.xml中:

<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-test</artifactId>
    <version>2.2.0-incubating</version>
    <scope>test</scope>
</dependency>

And here are the test essentials.

以下是测试要点。

TestingServer zkTestServer;

@Before
public void startZookeeper() throws Exception {
    zkTestServer = new TestingServer(2181);
    cli = CuratorFrameworkFactory.newClient(zkTestServer.getConnectString(), new RetryOneTime(2000));
}

@After
public void stopZookeeper() throws IOException {
    cli.close();
    zkTestServer.stop();
}

With cli creating any test data is very easy.

使用cli创建任何测试数据都非常容易。

cli.create().forPath("/a1", "testvalue".getBytes());

#3


12  

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

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

#4


3  

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

通过添加使用临时端口(由zkPort显示)并针对最新的ZK API进行更新来建立1的答案:

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

#5


2  

ServerConfig config = new ServerConfig();
config.parse(new String[] {port, dir});
ZooKeeperServerMain zk = new ZooKeeperServerMain();
zk.runFromConfig(config);

#6


0  

An updated version of GeoffBourne's answer.

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