我正在尝试使用spring-kafka将kafka与我的Spring boot(v2.0.6.RELEASE)应用程序集成。现在,我想拥有一个消费者和一个生产者。我让制作人工作得很好,我可以看到通过控制台使用者发送到主题的消息。我无法使用消费者代码,当kafka主题中出现新消息时,也不会被调用。
这是我的kafka配置类:
@Configuration
@EnableKafka
public class KafkaConfiguration {
@Bean
public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, <path to my custom serializer>);
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
return props;
}
@Bean
public Map<String, Object> consumerConfigs(){
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, <path to my custom deserializer>);
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
// props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
return props;
}
@Bean
public ConsumerFactory<String, List<MyMessage>> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(consumerConfigs());
}
@Bean
public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, List<MyMessage>>> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, List<MyMessage>> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
return factory;
}
@Bean
public ProducerFactory<String, List<MyMessage>> producerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfigs());
}
@Bean
public KafkaTemplate<String, List<MyMessage>> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
}
这是我的pom依赖项:
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
以及消费者代码:
@Slf4j
public class MyMessageConsumer {
@KafkaListener(topicPattern = "test_topic")
public void receive(List<MyMessage> myMessages){
log.info("Received payload {}", myMessages);
}
}
我正在计算机上运行kafka,正如我所说-我可以看到消息出现在控制台使用者中。我基于Spring的使用者无法接收消息的原因可能是什么?
编辑: 这是反序列化器类:
public class TagEventDeserializer implements Deserializer<List<MyMessage>> {
@Override
public void configure(Map map, boolean b) {
}
@Override
public List<MyMessage> deserialize(String s, byte[] bytes) {
ObjectMapper objectMapper = new ObjectMapper();
List<MyMessage> myMessages = null;
try{
myMessages = objectMapper.readValue(bytes , List.class);
} catch (Exception e){
e.printStackTrace();
}
return myMessages;
}
@Override
public void close() {
}
}
答案 0 :(得分:3)
由于缺少构造型注释,春季未扫描您的班级MyMessageConsumer
,因此,在此类中添加一个@Component
即可解决您的问题:
@Slf4j
@Component
public class MyMessageConsumer {
@KafkaListener(topicPattern = "test_topic")
public void receive(List<MyMessage> myMessages){
log.info("Received payload {}", myMessages);
}
}
我认为在@KafkaListner
批注中指定消费者组是个好主意