Is it possible to start a zookeeper server instance in process, say for unit tests?

后端 未结 6 1111
难免孤独
难免孤独 2020-11-30 22:04

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

6条回答
  •  悲&欢浪女
    2020-11-30 22:37

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

提交回复
热议问题