找不到春季Kafka ConsumerFactory bean

时间:2019-02-14 20:12:22

标签: java spring-boot apache-kafka spring-kafka

我正在尝试使用spring-kaka-2.2.0spring-boot-2.1.0建立Kafka使用者,但是我遇到一个奇怪的问题,即未找到ConsumerFactory bean,但它仍然存在于配置中。 / p>

我在主题StudentProfessor中有两种消息,我正在尝试使用@KafkaListener@KafkaHandler来使用它们,但到目前为止还没有结束

这是我的带有导入语句的配置

package com.kafka.configuration;
import java.util.HashMap;
import java.util.Map;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ContainerProperties.AckMode;
import org.springframework.kafka.support.converter.BatchMessagingMessageConverter;
import org.springframework.kafka.support.converter.StringJsonMessageConverter;
import org.springframework.kafka.support.serializer.JsonDeserializer;

@Configuration
public class KafkaConsumerConfig {

@Value("${kafka.consumer.bootstrap-server}")
private String bootstrapServer;

@Bean("consumerConfigs")
public Map<String, Object> consumerConfigs() {
    Map<String, Object> props = new HashMap<>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
    props.put(ConsumerConfig.GROUP_ID_CONFIG, "test-3");
    props.put(JsonDeserializer.TYPE_MAPPINGS,
            "professor:com.kafka.model.Professor, student:com.kafka.model.Student");
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
    props.put(ConsumerConfig.CLIENT_ID_CONFIG, "test-3");
    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, "1");
    return props;
}

@Bean("consumerFactory")
public ConsumerFactory<String, Object> consumerFactory() {
    return new DefaultKafkaConsumerFactory<>(consumerConfigs());
}

@Bean("kafkaListenerContainerFactory")
@ConditionalOnMissingBean(name = "KafkaAutoConfiguration")
public ConcurrentKafkaListenerContainerFactory<String, Object> kafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<String, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    factory.setBatchListener(true);
    factory.setMessageConverter(new BatchMessagingMessageConverter(converter()));
    factory.getContainerProperties().setAckMode(AckMode.BATCH);
    return factory;
}

@Bean
public StringJsonMessageConverter converter() {
    return new StringJsonMessageConverter();
   }

}

服务

 @Service
 @KafkaListener(topics = "test-events",id = "kafkaListenerContainerFactory")
 public class KafkaConsumerService {

@KafkaHandler
public void student(List<Student> stu) {
    System.out.println(stu);
}

@KafkaHandler
public void professor(List<Professor> pro) {
    System.out.println(pro);
}

@KafkaHandler
public void listenDefault(List<Object> obj) {
    System.out.println(obj);
    }

 }

申请无法开始


Description:

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.

 The following candidates were found but could not be injected:
  - Bean method 'kafkaConsumerFactory' in 'KafkaAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.kafka.core.ConsumerFactory; SearchStrategy: all) found beans of type 'org.springframework.kafka.core.ConsumerFactory' consumerFactory
  - User-defined bean method 'consumerFactory' in 'KafkaConsumerConfig'


 Action:

 Consider revisiting the entries above or defining a bean of type 'org.springframework.kafka.core.ConsumerFactory' in your configuration.

但是,如果进行一些小的更改,则可以从@Bean方法上删除consumerFactory()并直接进行实例化,但是仍然不能从consumerConfigs() Map中获取属性。它正在尝试将默认映射到localhost:9092,但我正在尝试连接远程群集。

 factory.setConsumerFactory(new DefaultKafkaConsumerFactory<>(consumerConfigs()));

1 个答案:

答案 0 :(得分:1)

如果要覆盖启动的默认容器工厂,则必须将其命名为kafkaListenerContainerFactory,因为其条件是存在具有该名称的bean。

@Bean
@ConditionalOnMissingBean(name = "kafkaListenerContainerFactory")
public ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(

也就是说,不清楚为什么要建立自己的消费者工厂和容器工厂,因为您所做的一切都可以在application.yml(或.properties)中进行配置并通过添加{{1} }的消息转换器(引导程序会为您连接)。

您的@Bean没有用于Boot自动装配工厂的正确通用类型。