首页 > 解决方案 > 缺少嵌入式KafkaCluster?

问题描述

这篇博文https://www.confluent.io/blog/stream-processing-part-2-testing-your-streaming-application/指的是 EmbeddedKafkaCluster 类,它应该在库 kafka-streams-test 中-实用程序。

但是,库中缺少此类,例如 org.apache.kafka/kafka-streams-test-utils/2.5.1。

我以为我可以使用来自github的源代码https://github.com/a0x8o/kafka/blob/master/streams/src/test/java/org/apache/kafka/streams/integration/utils/EmbeddedKafkaCluster.java

但是这个源代码引用了一些类,例如 kafka.zk.EmbeddedZookeeper 和 kafka.utils.MockTime,我认为它们必须在像 org.apache.kafka/kafka_2.13/2.5.1 这样的库中。不幸的是,他们也失踪了。

在这种情况下,配置项目以使用 EmbeddedKafkaCluster 的最佳方法是什么?

谢谢

鲍里斯

标签: apache-kafkaintegration-testingapache-kafka-streams

解决方案


添加以下依赖项:

    //build.gradle

    testCompile group: 'junit', name: 'junit', version: '4.13'
    testCompile group: 'org.hamcrest', name: 'hamcrest-junit', version: '2.0.0.0'

    compile group: 'org.apache.kafka', name: 'kafka_2.13', version: '2.7.0'
    testCompile group: 'org.apache.kafka', name: 'kafka_2.13', version: '2.7.0', classifier:'test'
    compile group: 'org.apache.kafka', name: 'kafka-streams', version: '2.7.0'
    testCompile group: 'org.apache.kafka', name: 'kafka-streams', version: '2.7.0', classifier: 'test'
    compile group: 'org.apache.kafka', name: 'kafka-streams-test-utils', version:  '2.7.0'
    testCompile group: 'org.apache.kafka', name: 'kafka-clients', version: '2.7.0', classifier: 'test'

如果您使用的是 Maven,请根据以下代码转换所有依赖项:

//pom.xml
<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-streams</artifactId>
    <version>2.7.0</version>
    <scope>test</scope>
    <classifier>test</classifier>
</dependency>

并按以下方式创建 EmbeddedKafkaCluster(Kotlin 示例):

    @Test
    fun createEmbeddedKafkaClusterTest() {

        val NUM_BROKERS = 1
        val embeddedKafkaCluster = EmbeddedKafkaCluster(NUM_BROKERS)
        Assert.assertNotNull(embeddedKafkaCluster)

        embeddedKafkaCluster.start()

        embeddedKafkaCluster.createTopic("TestTopic")
    }

推荐阅读