Spring 启动嵌入式kafka抛出错误BeanCreationException
Spring boot embedded kafka throws error BeanCreationException
我有一个具有以下配置的 kafka 属性的测试用例:
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {Application.class})
@ActiveProfiles("test")
@EnableConfigurationProperties
@EmbeddedKafka(controlledShutdown = true, topics = {"topic1", "topic2", "topic3"})
class Name {}
这是我在运行上面的测试用例时得到的构建错误:
Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedKafka': Invocation of init method failed; nested exception is java.lang.NoSuchFieldError: DEFAULT_SASL_ENABLED_MECHANISMS
Caused by: java.lang.NoSuchFieldError: DEFAULT_SASL_ENABLED_MECHANISMS
at kafka.server.Defaults$.<clinit>(KafkaConfig.scala:242)
at kafka.server.KafkaConfig$.<clinit>(KafkaConfig.scala:961)
at kafka.server.KafkaConfig.LogDirProp(KafkaConfig.scala)
at org.springframework.kafka.test.EmbeddedKafkaBroker.afterPropertiesSet(EmbeddedKafkaBroker.java:322)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790)
... 72 common frames omitted
这是具有 spring kafka 和 kafka-client 依赖项的 POM 文件,这可能会导致错误:
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.7.7</version>
<exclusions>
<exclusion>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<version>2.7.7</version>
<scope>test</scope>
</dependency>
解决方法:
请参考这个link:
https://docs.spring.io/spring-kafka/docs/current/reference/html/#update-deps
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<version>2.7.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.8.1</version>
<classifier>test</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.13</artifactId>
<version>2.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.13</artifactId>
<version>2.8.1</version>
<classifier>test</classifier>
<scope>test</scope>
</dependency>
```
spring-Kafka
版本与 Apache Kafka 客户端不兼容 3.0
。您还需要 2.8
和 Spring 启动 2.6
.
另一方面,即使您使用更新的代理,您也不需要更新的客户端。虽然这个故事是关于嵌入式测试的,所以我完全怀疑你是否需要担心任何客户端,除非它是从 Spring 到 Apache Kafka 的传递......
我有一个具有以下配置的 kafka 属性的测试用例:
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {Application.class})
@ActiveProfiles("test")
@EnableConfigurationProperties
@EmbeddedKafka(controlledShutdown = true, topics = {"topic1", "topic2", "topic3"})
class Name {}
这是我在运行上面的测试用例时得到的构建错误:
Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedKafka': Invocation of init method failed; nested exception is java.lang.NoSuchFieldError: DEFAULT_SASL_ENABLED_MECHANISMS
Caused by: java.lang.NoSuchFieldError: DEFAULT_SASL_ENABLED_MECHANISMS
at kafka.server.Defaults$.<clinit>(KafkaConfig.scala:242)
at kafka.server.KafkaConfig$.<clinit>(KafkaConfig.scala:961)
at kafka.server.KafkaConfig.LogDirProp(KafkaConfig.scala)
at org.springframework.kafka.test.EmbeddedKafkaBroker.afterPropertiesSet(EmbeddedKafkaBroker.java:322)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790)
... 72 common frames omitted
这是具有 spring kafka 和 kafka-client 依赖项的 POM 文件,这可能会导致错误:
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.7.7</version>
<exclusions>
<exclusion>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<version>2.7.7</version>
<scope>test</scope>
</dependency>
解决方法: 请参考这个link: https://docs.spring.io/spring-kafka/docs/current/reference/html/#update-deps
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<version>2.7.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.8.1</version>
<classifier>test</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.13</artifactId>
<version>2.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.13</artifactId>
<version>2.8.1</version>
<classifier>test</classifier>
<scope>test</scope>
</dependency>
```
spring-Kafka
版本与 Apache Kafka 客户端不兼容 3.0
。您还需要 2.8
和 Spring 启动 2.6
.
另一方面,即使您使用更新的代理,您也不需要更新的客户端。虽然这个故事是关于嵌入式测试的,所以我完全怀疑你是否需要担心任何客户端,除非它是从 Spring 到 Apache Kafka 的传递......