英文:
Spring Kafka cannot find Kafka Properties Bean
问题
我正在尝试在我的项目中使用 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]
英文:
I am trying to use Spring Kafka in my project where is spring boot version is 2.2.2.RELEASE, kafka version is 2.3.x.
implementation 'org.springframework.kafka:spring-kafka'
I have my KafkaConfig class and Listener Class setup:
@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);
}
}
When i am trying to run my application, i am getting below exception:
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]
答案1
得分: 4
我发现解决方法是禁用KafkaAutoConfiguration.class,
@SpringBootApplication(exclude = KafkaAutoConfiguration.class)
英文:
I found the solution to this is to disable KafkaAutoConfiguration.class,
@SpringBootApplication(exclude = KafkaAutoConfiguration.class)
答案2
得分: 1
以下是翻译好的部分:
我也遇到了同样的情况,原因是因为我使用了以下库:
implementation(group="io.confluent", name="kafka-avro-serializer", version="5.5.1")
该库自带了其自己的 kafka-clients
,版本为 5.5.1-ccs
,这被引入并导致了兼容性问题。
我通过使用Spring依赖管理来解决这个问题:
plugins {
id("org.springframework.boot") version "2.2.2.RELEASE"
id("io.spring.dependency-management") version "1.0.9.RELEASE"
}
然后运行 gradle clean
英文:
I had the same happen to me, the reason was because I had the following library:
implementation(group="io.confluent", name="kafka-avro-serializer", version="5.5.1")
Which comes with its own kafka-clients
with version 5.5.1-ccs
which was being picked up and causing compatibility issues.
I solved this by using Spring Dependency Management:
plugins {
id("org.springframework.boot") version "2.2.2.RELEASE"
id("io.spring.dependency-management") version "1.0.9.RELEASE"
}
Then running a gradle clean
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论