问题描述
直到现在,我已经在运行Spring Boot应用程序之前从命令行启动了zookeeper和kafka服务器,但是现在我需要直接从代码中启动它们.
第一件事是,我尝试在主要方法中使用ProcessBuilder:
Until now I've started zookeeper and kafka server from command line before running my Spring Boot application, but now I need to start them directly from code.
First thing is, I've tried using ProcessBuilder inside main method:
Process process = new ProcessBuilder("C:\\kafka_2.12-2.2.0\\bin\\windows\\zookeeper-server-start.bat",
"C:\\kafka_2.12-2.2.0\\config\\zookeeper.properties").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
但是这似乎不起作用,因为控制台上什么都没打印,并且一段时间后应用程序抛出TimeOutException.
But this seems not working, since nothing is printed on the console and after a while the application throws a TimeOutException.
第二,我想让Zookeeper启动后运行kafka服务器.一个人怎么能做到这一点?
Second, I would like to have the kafka server to run after Zookeeper has started; how can one achieve this?
推荐答案
您将必须使用 ZookeeperExecutor
从您的" java应用程序中启动Zookeeper API,方法是在其中提供4个初始参数和一个线程运行程序地方.ZooKeeper API文档中给出了一个示例 https://zookeeper.apache.org/doc/r3.4.13/javaExample.html 和 https://www.programcreek.com/java-api-examples/?api=org.apache.zookeeper.server.ZooKeeperServerMain
You will have to use ZookeeperExecutor
to start Zookeeper API from "your" java application by giving 4 initial arguments and a thread runner in place. One example is given on ZooKeeper API documentationhttps://zookeeper.apache.org/doc/r3.4.13/javaExample.htmlandhttps://www.programcreek.com/java-api-examples/?api=org.apache.zookeeper.server.ZooKeeperServerMain
这篇关于Spring Boot从Java启动Zookeeper和Kafka Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!