1

我有一个具有以下配置的 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>

解决方法:请参考这个链接: 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>
    ```
4

1 回答 1

0

spring-Kafka版本与 Apache Kafka 客户端不兼容3.0。您仍然需要2.8Spring Boot 2.6

另一方面,即使您使用较新的代理,您也不需要较新的客户端。虽然这个故事是关于嵌入式测试的,所以我完全怀疑你需要担心任何客户端,除非它是从 Spring 为 Apache Kafka 传递的东西……</p>

于 2021-10-01T21:31:34.190 回答