44

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

4

6 回答 6

61

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"));
于 2013-10-21T13:26:03.927 回答
47

要开始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();
于 2012-02-15T13:36:57.890 回答
12

你可以使用这样的东西。

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

于 2012-02-15T00:36:16.197 回答
4

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);
于 2013-05-08T17:30:33.867 回答
2
ServerConfig config = new ServerConfig();
config.parse(new String[] {port, dir});
ZooKeeperServerMain zk = new ZooKeeperServerMain();
zk.runFromConfig(config);
于 2013-04-09T13:18:08.783 回答
0

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();
于 2016-09-14T20:16:59.987 回答