问题描述
到目前为止,我已经在运行 Spring Boot 应用程序之前从命令行启动了 zookeeper 和 kafka 服务器,但现在我需要直接从代码中启动它们.
首先,我尝试在 main 方法中使用 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 应用程序中通过提供 4 个初始参数和一个线程运行程序来启动 Zookeeper API地方.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!