我想将嵌入式 Kafka 用于 Spring Boot 应用程序。我可以使用嵌入式 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)