gpt4 book ai didi

java - Spring kafka @KafkaListener 没有被调用

转载 作者:行者123 更新时间:2023-12-01 17:45:28 24 4
gpt4 key购买 nike

我正在尝试使用 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() {

}
}

最佳答案

因为您的类 MyMessageConsumer 由于缺少构造型注释而未被 Spring 扫描,因此向此类添加 @Component 例如将解决您的问题:

@Slf4j
@Component
public class MyMessageConsumer {

@KafkaListener(topicPattern = "test_topic")
public void receive(List<MyMessage> myMessages){
log.info("Received payload {}", myMessages);
}
}

我认为在 @KafkaListner 注释中指定消费者组是个好主意

关于java - Spring kafka @KafkaListener 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55882423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com