【问题标题】:Spring Boot Cannot Find EmbeddedKafkaBroker Bean(Without using SpringbootTest)Spring Boot 找不到 EmbeddedKafkaBroker Bean(不使用 SpringbootTest)
【发布时间】:2020-05-03 03:03:06
【问题描述】:

我想将嵌入式 Kafka 用于 Spring 启动应用程序。我可以使用嵌入式 Kafka 进行 Junit 测试,但是在尝试在主应用程序中使用时,嵌入式 Kafka 对象没有被识别。

当尝试加载 Spring Boot 应用程序时,嵌入式 kafka 对象没有被自动装配。这是针对非测试流程的。

@SpringBootApplication
@DirtiesContext
@EmbeddedKafka(topics = "TEST_TOPIC.P2.R2", partitions = 1, controlledShutdown = false, brokerProperties = {
        "listeners=PLAINTEXT://localhost:9092", "port=9092" })
public class MockKafkaProducerApplication {

    public static void main(String[] args) throws Exception {
        System.out.println("Starting Spring boot Application");
        SpringApplication.run(MockKafkaProducerApplication.class, args);

    }

}


@ActiveProfiles("kafka_test")
@Configuration
public class KafkaConsumerTestBase {
    private Logger LOGGER = LoggerFactory.getLogger(KafkaConsumerTestBase.class);

    @Autowired
    protected EmbeddedKafkaBroker embeddedKafka;


    @Value("${spring.embedded.kafka.brokers}")
    private String brokerAddress;

    @Autowired
    protected KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry;

    @Autowired
    protected KafkaTemplate<String, String> senderTemplate;

....... ........... }

com.dell.pde.kafka.KafkaConsumerTestBase 中的字段 embeddedKafka 需要一个无法找到的“org.springframework.kafka.test.EmbeddedKafkaBroker”类型的 bean。

注入点有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true)

【问题讨论】:

    标签: spring-boot apache-kafka embedded-kafka


    【解决方案1】:

    @interface EmbeddedKafka 用于测试目的。 如果你检查 public class EmbeddedKafkaCondition你可以看看spring test是怎么跑的:

    public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
        Optional<AnnotatedElement> element = context.getElement();
        if (element.isPresent() && !this.springTestContext((AnnotatedElement)element.get())) {
            EmbeddedKafka embedded = (EmbeddedKafka)AnnotatedElementUtils.findMergedAnnotation((AnnotatedElement)element.get(), EmbeddedKafka.class);
            if (embedded != null) {
                EmbeddedKafkaBroker broker = this.getBrokerFromStore(context);
                if (broker == null) {
                    broker = this.createBroker(embedded);
                    BROKERS.set(broker);
                    this.getStore(context).put("embedded-kafka", broker);
                }
            }
        }
    
        return ConditionEvaluationResult.enabled("");
    }
    
    private boolean springTestContext(AnnotatedElement annotatedElement) {
        return AnnotatedElementUtils.findAllMergedAnnotations(annotatedElement, ExtendWith.class).stream().filter((extended) -> {
            return Arrays.asList(extended.value()).contains(SpringExtension.class);
        }).findFirst().isPresent();
    }
    

    尝试覆盖此类以在您的应用上运行它。

    建议你直接用docker来提升kafka镜像

    【讨论】:

      【解决方案2】:

      嵌入式 Kafka 用于测试,而不是用于实际应用程序。

      可以在运行基于 Spring Kafka 的测试的测试类上指定的注释。在常规 Spring TestContext Framework 之外提供以下功能:

      ...

      一个库,提供内存中的 Kafka 实例来运行您的测试。

      如果您想制作一个实际的模拟应用程序,您还必须运行一个实际的 Kafka 实例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-10
        • 2018-12-13
        • 2018-10-01
        • 2017-12-13
        • 2019-07-16
        • 2017-11-05
        • 1970-01-01
        • 2018-06-12
        相关资源
        最近更新 更多