[英]Start Zookeeper and Kafka through java code
Yesterday I asked this question and I'm trying to follow some other answers I've found but they're leading me nowhere. 昨天我问了这个问题 ,我试图遵循我找到的其他答案,但它们无济于事。 I really can't understand how to properly set up Zookeeper and then Kafka server from code. 我真的不明白如何从代码中正确设置Zookeeper,然后再设置Kafka服务器。 What I did so far was this: 到目前为止,我所做的是:
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();
I couldn't find anything more than this. 除此以外,我什么也找不到。
Using the *-server-start
commands would be highly recommended... They setup the classpath correctly. 强烈建议使用*-server-start
命令...它们可以正确设置类路径。
Otherwise, looks like you are trying to write just simple tests, and libraries exist already for Embedded Kafka. 否则,看起来您正在尝试编写简单的测试,并且嵌入式Kafka的库已经存在。 eg https://github.com/embeddedkafka/embedded-kafka or using Docker 例如https://github.com/embeddedkafka/embedded-kafka或使用Docker
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.