我有一个工作中的原型 Spring Boot 应用程序,它侦听 Kafka 队列。除了在 application.yml
中的配置外,所有需要的只是使用 @KafkaListener
注释的 MessageListener
实现。
现在,我正在引入 Spring Integration,并已配置这些 bean:
@Bean
public KafkaMessageDrivenChannelAdapter<String, String>
adapter(KafkaMessageListenerContainer<String, String> container) {
KafkaMessageDrivenChannelAdapter<String, String> kafkaMessageDrivenChannelAdapter =
new KafkaMessageDrivenChannelAdapter<>(container);
kafkaMessageDrivenChannelAdapter.setOutputChannel(receiver());
return kafkaMessageDrivenChannelAdapter;
}
@Bean
public KafkaMessageListenerContainer<String, String> container() throws Exception {
ContainerProperties properties = new ContainerProperties(this.topic);
// set more properties
return new KafkaMessageListenerContainer<>(consumerFactory(), properties);
}
@Bean
public ConsumerFactory<String, String> consumerFactory() {
Map<String, Object> props = ...; // set proerties
return new DefaultKafkaConsumerFactory<>(props);
}
应用程序无法启动,并抛出以下错误:
Parameter 1 of method kafkaListenerContainerFactory in org.springframework.boot.autoconfigure.kafka.KafkaAnnotationDrivenConfiguration required a bean of type 'org.springframework.kafka.core.ConsumerFactory' that could not be found.
- Bean method 'kafkaConsumerFactory' in 'KafkaAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.kafka.core.ConsumerFactory; SearchStrategy: all) found bean 'consumerFactory'
Action:
Consider revisiting the conditions above or defining a bean of type 'org.springframework.kafka.core.ConsumerFactory' in your configuration.
尽管我已定义了一个ConsumerFactory
bean,但这仍然存在。
在调试模式下运行,很明显Boot正在加载一个KafkaListenerEndpointContainer
bean来监听代理:
[org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1] o.a.k.c.c.internals.AbstractCoordinator : Discovered coordinator localhost:9092 (id: 2147483999 rack: null) for group my_group.
然后:
KafkaAnnotationDrivenConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.kafka.annotation.EnableKafka'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
KafkaAnnotationDrivenConfiguration#kafkaListenerContainerFactory matched:
- @ConditionalOnMissingBean (names: kafkaListenerContainerFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)
KafkaAnnotationDrivenConfiguration#kafkaListenerContainerFactoryConfigurer matched:
- @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.kafka.ConcurrentKafkaListenerContainerFactoryConfigurer; SearchStrategy: all) did not find any beans (OnBeanCondition)
KafkaAnnotationDrivenConfiguration.EnableKafkaConfiguration matched:
- @ConditionalOnMissingBean (names: org.springframework.kafka.config.internalKafkaListenerAnnotationProcessor; SearchStrategy: all) did not find any beans (OnBeanCondition)
KafkaAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.kafka.core.KafkaTemplate'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
KafkaAutoConfiguration#kafkaProducerFactory matched:
- @ConditionalOnMissingBean (types: org.springframework.kafka.core.ProducerFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)
KafkaAutoConfiguration#kafkaProducerListener matched:
- @ConditionalOnMissingBean (types: org.springframework.kafka.support.ProducerListener; SearchStrategy: all) did not find any beans (OnBeanCondition)
KafkaAutoConfiguration#kafkaTemplate matched:
- @ConditionalOnMissingBean (types: org.springframework.kafka.core.KafkaTemplate; SearchStrategy: all) did not find any beans (OnBeanCondition)
我认为发生的情况是Spring Boot\Kafka自动配置与Spring Integration\Kafka设置冲突了。正确解决这个问题的方法是什么?
谢谢