是否有可能在进程中启动一个zookeeper服务器实例,比如unit testing?

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

要启动ZooKeeper你必须执行ZooKeeperServerMain类。

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

Netfix开辟了一个框架,使Zookeeper更加方便。 它build立在testing服务器类。 如果您使用Maven,只需将其添加到您的项目的pom.xml

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

这里是testing要领。

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

使用cli创build任何testing数据是非常容易的。

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

你可以使用这样的东西。

 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. 

closures它只需调用standaloneServerFactory.shutdown()

通过添加使用短暂端口(由zkPort )并根据最新的ZK API更新的答案build立在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); 
 ServerConfig config = new ServerConfig(); config.parse(new String[] {port, dir}); ZooKeeperServerMain zk = new ZooKeeperServerMain(); zk.runFromConfig(config); 

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