带弹簧启动的简单嵌入式Kafka测试示例

时间:2018-02-12 18:23:03

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

编辑FYI:working gitHub example

我在互联网上搜索,找不到嵌入式Kafka测试的工作简单示例。

我的设置是:

  • 春季启动
  • 在一个班级中具有不同主题的多个@KafkaListener
  • 用于测试的嵌入式Kafka正常启动
  • 使用Kafkatemplate进行测试,发送给主题但是 @KafkaListener方法即使在很长的睡眠时间之后也没有收到任何东西
  • 未显示任何警告或错误,仅显示日志中来自Kafka的垃圾邮件

请帮帮我。主要是过度配置或过度设计的示例。我相信它可以做得很简单。 谢谢,伙计们!

#elif

private static String SENDER_TOPIC =“test.kafka.topic”;

@Controller
public class KafkaController {

    private static final Logger LOG = getLogger(KafkaController.class);

    @KafkaListener(topics = "test.kafka.topic")
    public void receiveDunningHead(final String payload) {
        LOG.debug("Receiving event with payload [{}]", payload);
        //I will do database stuff here which i could check in db for testing
    }
}

3 个答案:

答案 0 :(得分:21)

嵌入式Kafka测试适用于以下配置,

测试类的注释

@EnableKafka
@SpringBootTest(classes = {KafkaController.class}) // Specify @KafkaListener class if its not the same class,or not loaded with test config
@EmbeddedKafka(partitions = 1, controlledShutdown = false,
    brokerProperties = {"listeners=PLAINTEXT://localhost:3333", "port=3333"})
public class KafkaConsumerTest{
@Autowired
KafkaEmbedded kafkaEmbeded;

@Autowired
KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry;

在设置方法的注释之前

@Before
public void setUp() throws Exception {
  for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry.getListenerContainers()) {
    ContainerTestUtils.waitForAssignment(messageListenerContainer, 
    kafkaEmbeded.getPartitionsPerTopic());
  }
}

注意:我没有使用@ClassRule来创建嵌入式Kafka,而是使用自动布线@Autowired embeddedKafka

@Test
public void testReceive() throws Exception {
     kafkaTemplate.send(topic, data);
}

希望这有帮助!

编辑:测试标有@TestConfiguration

的配置类
@TestConfiguration
public class TestConfig {

@Bean
public ProducerFactory<String, String> producerFactory() {
    return new DefaultKafkaProducerFactory<>(KafkaTestUtils.producerProps(kafkaEmbedded));
}

@Bean
public KafkaTemplate<String, String> kafkaTemplate() {
    KafkaTemplate<String, String> kafkaTemplate = new KafkaTemplate<>(producerFactory());
    kafkaTemplate.setDefaultTopic(topic);
    return kafkaTemplate;
}

现在@Test方法将自动装配KafkaTemplate并使用is发送消息

kafkaTemplate.send(topic, data);

使用上一行更新了答案代码块

答案 1 :(得分:5)

我现在解决了这个问题

@BeforeClass
public static void setUpBeforeClass() {
    System.setProperty("spring.kafka.bootstrap-servers", embeddedKafka.getBrokersAsString());
    System.setProperty("spring.cloud.stream.kafka.binder.zkNodes", embeddedKafka.getZookeeperConnectionString());
}
在我调试时,我看到嵌入式卡卡服务器正在使用随机端口。

我无法找到它的配置,因此我将kafka配置设置为与服务器相同。看起来对我来说仍然有点难看。

我很想拥有@Mayur提到的行

@EmbeddedKafka(partitions = 1, controlledShutdown = false, brokerProperties = {"listeners=PLAINTEXT://localhost:9092", "port=9092"})

但无法在互联网上找到正确的依赖关系。

答案 2 :(得分:0)

因为接受的答案无法为我编译或使用。我根据https://blog.mimacom.com/testing-apache-kafka-with-spring-boot/想与您分享什么,找到了另一个解决方案。

依赖项是“ spring-kafka-test”版本:“ 2.2.7.RELEASE”

@RunWith(SpringRunner.class)
@EmbeddedKafka(partitions = 1, topics = { "testTopic" })
@SpringBootTest
public class SimpleKafkaTest {

    private static final String TEST_TOPIC = "testTopic";

    @Autowired
    EmbeddedKafkaBroker embeddedKafkaBroker;

    @Test
    public void testReceivingKafkaEvents() {
        Consumer<Integer, String> consumer = configureConsumer();
        Producer<Integer, String> producer = configureProducer();

        producer.send(new ProducerRecord<>(TEST_TOPIC, 123, "my-test-value"));

        ConsumerRecord<Integer, String> singleRecord = KafkaTestUtils.getSingleRecord(consumer, TEST_TOPIC);
        assertThat(singleRecord).isNotNull();
        assertThat(singleRecord.key()).isEqualTo(123);
        assertThat(singleRecord.value()).isEqualTo("my-test-value");

        consumer.close();
        producer.close();
    }

    private Consumer<Integer, String> configureConsumer() {
        Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("testGroup", "true", embeddedKafkaBroker);
        consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        Consumer<Integer, String> consumer = new DefaultKafkaConsumerFactory<Integer, String>(consumerProps)
                .createConsumer();
        consumer.subscribe(Collections.singleton(TEST_TOPIC));
        return consumer;
    }

    private Producer<Integer, String> configureProducer() {
        Map<String, Object> producerProps = new HashMap<>(KafkaTestUtils.producerProps(embeddedKafkaBroker));
        return new DefaultKafkaProducerFactory<Integer, String>(producerProps).createProducer();
    }
}