本文介绍了spring boot kafka 在一个监听器中使用多个主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谁能给我一个 spring boot kafka 的小例子,我们可以在一个监听器类中使用多个主题.
Can anyone provide me a small example in spring boot kafka where we can consume multiple topics in one single listener class.
推荐答案
application.yml
my:
kafka:
conf:
groupId: myId
topics: topic1,topicN
你的听众:
@KafkaListener(groupId = "${my.kafka.conf.groupId}", topics = "#{'${my.kafka.conf.topics}'.split(',')}")
public void storeTopicsDataToMongo(
@Header(KafkaHeaders.RECEIVED_TOPIC) String topic,
@Header(required = false, name= KafkaHeaders.RECEIVED_MESSAGE_KEY) String key,
@Payload(required = false) String record)
{
log.trace(format("Received topic[%s] key[%s] payload[%s]", topic, key, record));
//your code
}
或者您可以探索 @ConfigurationProperties
并自己创建 bean,例如:
or you can explore the @ConfigurationProperties
and create the beans yourself, something like:
@Component
@ConfigurationProperties(prefix = "my.kafka.conf")
@Data //=> lombok
public class ConsumerConfigurationProperties {
private String groupId;
private List<String> topics;
}
这篇关于spring boot kafka 在一个监听器中使用多个主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!