Spring Kafka 官方提供了一种基于消息类型的消费模式,通过类上的 @KafkaListener
注解配合方法上的 @KafkaHandler
,实现多态消息的自动路由处理。其典型实现方式如下:
1 |
|
根据 Spring Kafka 官方文档 @KafkaListener on a Class 的说明:
When messages are delivered, the converted message payload type is used to determine which method to call.
接收到消息后,会使用转换后的消息类型来决定调用哪个方法。
开发者可以基于此特性,轻松实现从简单文本消息到复杂领域事件的各种消息处理场景。