春季启动spring.kafka.bootstrap-servers没有被消费者配置接受

时间:2018-08-01 15:37:58

标签: spring-boot spring-kafka

我正在尝试使用将spring boot项目连接到kafka。 在我的application.properties文件中,我具有以下配置:

spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.consumer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.consumer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.template.default-topic=my_default_topic
spring.kafka.admin.fail-fast=true
spring.kafka.consumer.auto-offset-reset=latest
spring.kafka.consumer.group-id=my_group_id
spring.kafka.listener.concurrency=10
spring.kafka.bootstrap-servers=kafka:9092

但是在日志中,我看到只有这些值中的一些才使它进入消耗配置。日志中的配置在这里:

2018-08-01 15:20:34.640  WARN 1 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refr[3/5548]
mpt: org.springframework.context.ApplicationContextException: Failed to start bean 'org.springframework.kafka.config.internalKafkaListenerEndpointRegistry'; nested exception is org.apache.kafka.common.KafkaException: Failed to cons[2/5548]
fka consumer                                                                                                                                                                                                                           [1/5548]
        isolation.level = read_uncommitted                                                                                                                            [46/5548]
        key.deserializer = class org.apache.kafka.common.serialization.StringDeserializer                                                                             [45/5548]
        max.partition.fetch.bytes = 1048576                                                                                                                           [44/5548]
        max.poll.interval.ms = 300000                                                                                                                                 [43/5548]
        check.crcs = true                                                                                                                                             [58/5548]
        client.id =
        connections.max.idle.ms = 540000
        enable.auto.commit = true
        exclude.internal.topics = true
        fetch.max.bytes = 52428800
        fetch.max.wait.ms = 500
        fetch.min.bytes = 1
        group.id = null
        heartbeat.interval.ms = 3000
        interceptor.classes = null
        internal.leave.group.on.close = true
        isolation.level = read_uncommitted
        key.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
        max.partition.fetch.bytes = 1048576
        max.poll.interval.ms = 300000
        max.poll.records = 500
        metadata.max.age.ms = 300000
        metric.reporters = []
        metrics.num.samples = 2
        metrics.recording.level = INFO
        metrics.sample.window.ms = 30000
        partition.assignment.strategy = [class org.apache.kafka.clients.consumer.RangeAssignor]
        receive.buffer.bytes = 65536
        reconnect.backoff.max.ms = 1000
        reconnect.backoff.ms = 50
        request.timeout.ms = 305000
        retry.backoff.ms = 100
        sasl.jaas.config = null
        sasl.kerberos.kinit.cmd = /usr/bin/kinit
        sasl.kerberos.min.time.before.relogin = 60000
        sasl.kerberos.service.name = null
        sasl.kerberos.ticket.renew.jitter = 0.05
        sasl.kerberos.ticket.renew.window.factor = 0.8
        sasl.mechanism = GSSAPI
        security.protocol = PLAINTEXT
        send.buffer.bytes = 131072
        session.timeout.ms = 10000
        ssl.cipher.suites = null
        ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
        ssl.endpoint.identification.algorithm = null
        ssl.key.password = null
        ssl.keymanager.algorithm = SunX509
        ssl.keystore.location = null
        ssl.keystore.password = null
        ssl.keystore.type = JKS
        ssl.protocol = TLS
        ssl.provider = null
        ssl.secure.random.implementation = null
        ssl.trustmanager.algorithm = PKIX
        ssl.truststore.location = null
        ssl.truststore.password = null
        ssl.truststore.type = JKS
        value.deserializer = class org.springframework.kafka.support.serializer.JsonDeserializer

尤其是缺少bootstrap.servers和group.id。然后我得到一个例外

 org.springframework.context.ApplicationContextException: Failed to start bean 'org.springframework.kafka.config.internalKafkaListenerEndpointRegistry'; nested exception is or
g.apache.kafka.common.KafkaException: Failed to construct kafka consumer

因为找不到引导服务器。我知道我可以将属性手动传递到DefaultKafkaConsumerFactory bean中,但是我希望Spring Boot能够自动处理。有什么办法吗? 谢谢!

编辑: 我正在尝试像这样使用@KafkaListener来消耗消息...

 @KafkaListener(topics = "${app-name.inputTopic}")
    public void handleMessage(CustomMessage message){ 
       //my code
    }

2 个答案:

答案 0 :(得分:3)

我意识到在项目的其他地方有@Configuration bean正在手动设置属性。我删除了它们,一切正常。

答案 1 :(得分:0)

签出 KafkaProperties 对象。它包含标准属性。就像工厂一样。

  @Bean
  public KafkaTemplate<Integer, String> createTemplate(KafkaProperties properties)
  {
    Map<String, Object> props = properties.buildProducerProperties();
    
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

    KafkaTemplate<Integer, String> template = new KafkaTemplate<>(new DefaultKafkaProducerFactory<>(props));
    return template;
  }

原答案:https://stackoverflow.com/a/60961582/5740547