【发布时间】:2019-04-03 13:52:15
【问题描述】:
昨天我问了this question,我正在尝试遵循我找到的其他一些答案,但它们让我无处可去。我真的不明白如何从代码中正确设置 Zookeeper 和 Kafka 服务器。到目前为止我所做的是:
Properties prop = new Properties();
prop.setProperty("dataDir","C:\\kafka_2.12-2.2.0\\config\\zookeeper.properties");
prop.setProperty("bootstrap.servers", "localhost:2181");
QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig();
try {
quorumConfiguration.parseProperties(prop);
} catch(Exception e) {
throw new RuntimeException(e);
}
ZooKeeperServerMain zookeeper = new ZooKeeperServerMain();
final ServerConfig configuration = new ServerConfig();
configuration.readFrom(quorumConfiguration);
new Thread() {
public void run() {
try {
zookeeper.runFromConfig(configuration);
} catch (IOException e) {
}
}
}.start();
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("acks", "all");
props.put("retries", 0);
props.put("batch.size", 16384);
props.put("linger.ms", 1);
props.put("buffer.memory", 33554432);
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);
for (int i = 0; i < 100; i++)
producer.send(new ProducerRecord<String, String>("info", Integer.toString(i), Integer.toString(i)));
producer.close();
我找不到比这更多的东西了。
【问题讨论】:
-
你有什么问题?
-
@howie 我想直接从java代码启动zookeeper和kafka而不使用cmd行
-
那么你想在你的服务器中嵌入一个 zookeeper 和 kafka 吗?
-
基本上是的,我正在使用 localhost,直到现在我一直从命令行启动它们
-
你正在尝试做的事情是零意义的。按预期分别启动 Zookeeper 和 Kafka。
标签: java apache-kafka