springboot集成kafka @SendTo对于转发单条消息和转发批量消息的两种情况

//当转发单条消息时,可以采用如下方式
@KafkaListener(groupId = "a-group", topics = "a-topic")
@SendTo("b-topic")
public String consumeA(ConsumerRecord<String, String> consumerRecord) {
	return consumerRecord.value();
}

@KafkaListener(groupId = "b-group", topics = "b-topic")
public void consumeB(ConsumerRecord<String, String> consumerRecord) {
}
//当转发批量消息时,可以采用如下方式
@KafkaListener(groupId = "a-group", topics = "a-topic")
@SendTo("b-topic")
public List<String> consumeA(List<ConsumerRecord<String, String>> consumerRecordList) {
	return consumerRecordList.stream().map(ConsumerRecord::value).collect(Collectors.toList());
}

@KafkaListener(groupId = "b-group", topics = "b-topic")
public void consumeB(List<ConsumerRecord<String, String>> consumerRecordList) {
}

你可能感兴趣的:(spring,boot,kafka,后端)