gpt4 book ai didi

java - Spring Kafka 找不到 Kafka Properties Bean

转载 作者:行者123 更新时间:2023-12-02 19:09:45 26 4
gpt4 key购买 nike

我正在尝试在我的项目中使用 Spring Kafka,其中 spring boot 版本是 2.2.2.RELEASE,kafka 版本是 2.3.x。

    implementation 'org.springframework.kafka:spring-kafka'

我有我的 KafkaConfig 类和 Listener 类设置:

@EnableKafka
@Configuration
public class KafkaConfig {
Config cfg = new Config();


@Bean
public ConsumerFactory<String, String> consumerFactory() {
Map<String, Object> props = new HashMap<>();
props.put(
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
cfg.getProperty("server"));
props.put(
ConsumerConfig.GROUP_ID_CONFIG,
UUID.randomUUID());
props.put(
ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
StringDeserializer.class);
props.put(
ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
StringDeserializer.class);
return new DefaultKafkaConsumerFactory<>(props);
}

@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory();
factory.setConsumerFactory(consumerFactory());
return factory;
}
}

KafkaConsumer.java

@Service
public class KafkaConsumer {
private final Logger LOG = LoggerFactory
.getLogger(KafkaConsumer.class);

@KafkaListener(topics = "anomaly-topic")
void listener(String message) {
LOG.info("Listener [{}]", message);
}
}

当我尝试运行我的应用程序时,出现以下异常:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.kafka.KafkaAnnotationDrivenConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.kafka-org.springframework.boot.autoconfigure.kafka.KafkaProperties': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.kafka.KafkaProperties]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/kafka/common/requests/IsolationLevel
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]

最佳答案

我发现解决这个问题的方法是禁用 KafkaAutoConfiguration.class,

@SpringBootApplication(exclude = KafkaAutoConfiguration.class)

关于java - Spring Kafka 找不到 Kafka Properties Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64481838/

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