任何人都可以给我提供一个春季启动kafka的小例子,在那里我们可以在一个侦听器类中使用多个主题。
答案 0 :(得分:0)
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,例如:
@Component
@ConfigurationProperties(prefix = "my.kafka.conf")
@Data //=> lombok
public class ConsumerConfigurationProperties {
private String groupId;
private List<String> topics;
}
答案 1 :(得分:0)
对于消费者组的消费者,您可以使用以下内容:
@KafkaListener(topics =“ topic1,”) 公共无效的监听(@Payload KafkaBinding记录,MessageHeaders标头)引发ExecutionException,InterruptedException { ………… ………….. }
对于充当分配者的消费者,您可以使用以下内容:
@KafkaListener(id =“ foo”,topicPartitions = {@TopicPartition(topic =“ myTopic”,partitions = {“ 1”})}) 公共无效的监听(@Payload KafkaBinding记录,MessageHeaders标头)引发ExecutionException,InterruptedException { ………… ………….. }