[英]How to know which kafka is being consumed from list?
I have a function for consuming Kafka.我有一个 function 用于消费 Kafka。 I consume multiple topics in the same function. But I don't know which topic is consumed
同一个function消费了多个topic,但是不知道消费的是哪个topic
@KafkaListener(topics = {"topic1","topic2"})
public void kafkaConsume(String message) {
//Print the kafka topic that is being consumed
return;
}
How do I print the kafka topic?如何打印kafka主题?
You can add @Headers parameter to get all the extra information you want within the message.您可以添加 @Headers 参数以获取消息中所需的所有额外信息。
For example:例如:
@KafkaListener(topics = "topicName")
public void listenWithHeaders(
@Payload String message,
@Header(KafkaHeaders.TOPIC) String topic) {
System.out.println("Received Message: " + message" + "from: " + topic);
}
You can get all this information in KafkaHeaders https://docs.spring.io/spring-kafka/api/org/springframework/kafka/support/KafkaHeaders.html您可以在 KafkaHeaders https://docs.spring.io/spring-kafka/api/org/springframework/kafka/support/KafkaHeaders.html中获取所有这些信息
Or alternatively, you can consume a ConsumerRecord<K, V> which has most of the information as well或者,您可以使用也包含大部分信息的 ConsumerRecord<K, V>
@KafkaListener(topics = "topicName")
public void listenConsumerRecord(ConsumerRecord<String, String> record) {
System.out.println("Received Message: " + record.value() + "from: " + record.topic());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.