java - Spring Boot Embedded Kafka无法连接

标签 java spring-boot kotlin apache-kafka confluent-platform

我正在尝试为我的 Kafka 消费者编写集成测试。我已经完成了 official reference documentation但是当我开始测试时,我只看到这个重复的无限广告:

-2019-04-03 15:47:34.002 WARN 13120 --- [ main] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-1, groupId=my-group] Connection to node -1 could not be established. Broker may not be available.

我做错了什么?

我正在使用 JUnit5、Spring Boot 以及 spring-kafkaspring-kafka-test

我的 @Configuration 类上有 @EnableKafka 注释。

这是我的测试类的样子:

@ExtendWith(SpringExtension::class)
@SpringBootTest(classes = [TestKafkaConfig::class])
@DirtiesContext
@EmbeddedKafka(
        partitions = 1,
        topics = [KafkaIntegrationTest.MY_TOPIC])
class KafkaIntegrationTest {

    @Autowired
    private lateinit var embeddedKafka: EmbeddedKafkaBroker

    @Test
    fun test() {
        val senderProps = KafkaTestUtils.senderProps(embeddedKafka.brokersAsString)
        val template = KafkaTemplate(DefaultKafkaProducerFactory<Int, String>(senderProps))
        template.defaultTopic = KafkaIntegrationTest.MY_TOPIC
        template.sendDefault("foo")
    }
}

我的 application.yml 看起来像这样:

kafka:
  consumer:
    group-id: my-group
    bootstrap-servers: ${BOOTSTRAP_SERVERS:localhost:9092}
    value-deserializer: io.confluent.kafka.serializers.KafkaAvroDeserializer
    key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
    properties:
      schema.registry.url: ${SCHEMA_REGISTRY_URL:http://localhost:8081}
      specific.avro.reader: true

我也尝试设置一个 MockSchemaRegistryClient 但我得到了完全相同的重复消息。 (这就是我尝试设置 MockSchemaRegistryClient 的方式):

@TestConfiguration
@Import(TestConfig::class)
class TestKafkaConfig {

    @Autowired
    private lateinit var props: KafkaProperties

    @Bean
    fun schemaRegistryClient() = MockSchemaRegistryClient()

    @Bean
    fun kafkaAvroSerializer() = KafkaAvroSerializer(schemaRegistryClient())

    @Bean
    fun kafkaAvroDeserializer() = KafkaAvroDeserializer(schemaRegistryClient(), props.buildConsumerProperties())

    @Bean
    fun producerFactory(): ProducerFactory<*, *> = DefaultKafkaProducerFactory(
            props.buildProducerProperties(),
            StringSerializer(),
            kafkaAvroSerializer())

    @Bean
    fun consumerFactory(): ConsumerFactory<*, *> = DefaultKafkaConsumerFactory(
            props.buildConsumerProperties(),
            StringDeserializer(),
            kafkaAvroDeserializer()
    )

    @Bean
    fun kafkaListenerContainerFactory() = ConcurrentKafkaListenerContainerFactory<Any, Any>().apply {
        setConsumerFactory(consumerFactory() as ConsumerFactory<in Any, in Any>?)
    }

}

我做错了什么? 请注意我正在使用 Confluent Schema Registry 并尝试从 Avro 反序列化。

我要测试的是我的消费者是否有效,如下所示:

open class SomeConsumer(private val someUseCase) {

    @KafkaListener(topics = ["\${kafka.some-topic}"])
    open fun processMessage(record: ConsumerRecord<String, SomeObject>) {
        someUseCase.call(record)
    }
}

最佳答案

我相信您缺少为测试设置代理 url。

文档中有关于如何获取这个值的说明:

When the embedded Kafka and embedded Zookeeper server are started by the EmbeddedKafkaBroker, a system property named spring.embedded.kafka.brokers is set to the address of the Kafka brokers and a system property named spring.embedded.zookeeper.connect is set to the address of Zookeeper. Convenient constants (EmbeddedKafkaBroker.SPRING_EMBEDDED_KAFKA_BROKERS and EmbeddedKafkaBroker.SPRING_EMBEDDED_ZOOKEEPER_CONNECT) are provided for this property.

(它位于 junit 部分的底部 here )

解决此问题的一种方法是在测试中将 kafka.consumers.bootstrap-servers 设置为此值,例如

spring:
    kafka:
        consumer:
            bootstrap-servers: ${spring.embedded.kafka.brokers}

关于java - Spring Boot Embedded Kafka无法连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55497296/

相关文章:

java - 在测试中处理本​​地与远程数据库时区差异

java - Jacoco exec 文件总是出现 EOFException

Spring Boot 使用 OAuth2 发送电子邮件

rest - Kotlin - Ktor - 如何处理 PATCH 调用中的可选 API 资源字段?

android - 如果文件存在,为什么 ACTION_CREATE_DOCUMENT Intent 不能正确重命名文件?

Java MD5 哈希函数给出不正确的哈希值

java - 尝试使用java更改背景颜色时出错

java - JDBC 和 Sql Server 2008 连接

java - Spring boot 1.4.x 和自定义 CharsetProvider

java - 如何使用android中的 Intent 打开存储中的特定文件夹