[英]Unable to Connect from Java to Kafka running in Docker
嘗試使用 Debezium 將 stream MySql 數據庫到 Kafka。
所以,在 Docker 容器中,我已經啟動了 Zookeeper、Kafka、MySQL 數據庫、MySQL 命令行和 Kafka Connect。
當我在 MySQL 命令行中運行任何 DML 命令時,我可以在觀察者 window 中看到更改事件,該觀察者是在 docker 中啟動的。 所以目前一切看起來都很好。 請在下面找到相同的內容。
現在我正在嘗試使用 Java 代碼中的更改事件,只要我在 MySQL 命令行中執行任何 DML 命令,就可以在觀察程序 window 中看到這些更改事件。 請在下面找到消費者。
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:29092");
properties.put(ConsumerConfig.GROUP_ID_CONFIG, "my-first-consumer-group");
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
Consumer<String, String> consumer = new KafkaConsumer<>(properties);
ArrayList<String> topics = new ArrayList<>();
topics.add("dbserver1.inventory.customers");
consumer.subscribe(topics);
while (true) {
ConsumerRecords<String, String> records = consumer.poll(1L);
for (ConsumerRecord<String, String> record : records) {
System.out.println("Message received: " + record.value());
}
consumer.commitAsync();
}
無法使用來自上述消費者的數據更改事件。 請讓我知道,如果有什么需要做的。
我通過如下更改 BOOTSTRAP_SERVERS_CONFIG 的屬性解決了這個問題,現在工作正常。 能夠消費數據的變化。
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
並使用數據更改
while (true) {
ConsumerRecords<String, String> records = consumer.poll(1000L);
for (ConsumerRecord<String, String> record : records) {
String payload = record.value();
if(payload.contains("payload")) {
JsonObject root = (JsonObject) new JsonParser().parse(payload);
root = root.getAsJsonObject("payload").getAsJsonObject("after");
}
}
consumer.commitAsync();
}
它對我有用。 :)
有效和無效之間的區別在於您訪問的偵聽器端口。
所以要么:
localhost
(了解更多)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.