Spring Kafka无法找到Kafka属性Bean。

huangapple go评论82阅读模式
英文:

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 &#39;org.springframework.kafka:spring-kafka&#39;

I have my KafkaConfig class and Listener Class setup:

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


@Bean
public ConsumerFactory&lt;String, String&gt; consumerFactory() {
Map&lt;String, Object&gt; props = new HashMap&lt;&gt;();
props.put(
    ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
    cfg.getProperty(&quot;server&quot;));
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&lt;&gt;(props);
}

@Bean
public ConcurrentKafkaListenerContainerFactory&lt;String, String&gt; kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory&lt;String, String&gt; factory = new ConcurrentKafkaListenerContainerFactory();
factory.setConsumerFactory(consumerFactory());
return factory;
}
}

KafkaConsumer.java

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

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

When i am trying to run my application, i am getting below exception:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name &#39;org.springframework.boot.autoconfigure.kafka.KafkaAnnotationDrivenConfiguration&#39;: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;spring.kafka-org.springframework.boot.autoconfigure.kafka.KafkaProperties&#39;: 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=&quot;io.confluent&quot;, name=&quot;kafka-avro-serializer&quot;, version=&quot;5.5.1&quot;)

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(&quot;org.springframework.boot&quot;) version &quot;2.2.2.RELEASE&quot;
    id(&quot;io.spring.dependency-management&quot;) version &quot;1.0.9.RELEASE&quot;
}

Then running a gradle clean

huangapple
  • 本文由 发表于 2020年10月22日 20:04:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64481838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定