Spring Cloud @StreamListener不提供Acknowledgment标头,甚至自动提交设置为false

时间:2018-05-22 09:53:09

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

我目前陷入了一个手动管理Kafka偏移和提交的简单示例。我有一个Spring Cloud Streams应用程序,它设置enable.auto.commit = false(在打印ConsumerValues时在启动日志中看到),但是,当我解析消息时,它不提供确认标题。

这是我的倾听者:

@StreamListener(Sink.INPUT)
public void handleSchedulerMessage(@Payload SchedulerEvent event, @Header(KafkaHeaders.ACKNOWLEDGMENT) Acknowledgment acknowledgment) {
    log.debug("[message={}]", event);
    // todo: processing
    log.debug("Event processed successfully [event={}]", event);
}

配置的YAML也非常简单:

spring:
  application:
    name: scheduler
  cloud:
    stream:
      kafka:
        binder:
          brokers: *kafka-broker*:9092
          zkNodes: *zookeeper*:2181
      bindings:
        input:
          destination: scheduler
          contentType: application/json
          consumer:
            autoCommitOffset: false

当我发送消息时,会立即弹出错误:

2018-05-22 11:38:32.470 ERROR 11651 --- [container-0-C-1] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessageHandlingException: Missing header 'kafka_acknowledgment' for method parameter type [interface org.springframework.kafka.support.Acknowledgment], failedMessage=GenericMessage [payload=byte[38], headers={kafka_offset=9, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@fdac355, deliveryAttempt=3, kafka_timestampType=CREATE_TIME, kafka_receivedMessageKey=null, kafka_receivedPartitionId=0, kafka_receivedTopic=scheduler, kafka_receivedTimestamp=1526981909241, contentType=application/json}]

收到的消息不包含所需的标题,与文档在禁用autoCommit时所说的相反:

Whether to autocommit offsets when a message has been processed. If set to false, a header with the key kafka_acknowledgment of the type org.springframework.kafka.support.Acknowledgment header will be present in the inbound message. Applications may use this header for acknowledging messages.

代码并不复杂,我没有使用任何预先生成的项目。示例并不能解释我所做的事情,所以我不知道自己可能缺少什么。

1 个答案:

答案 0 :(得分:0)

看起来你的YAML中有一个级别的缩进。

根据文件,财产必须是这样的:

spring.cloud.stream.kafka.bindings.input.consumer.autoCommitOffset=false

但你的样本是这样的:

spring.cloud.stream.bindings.input.consumer.autoCommitOffset=false

注意中间的额外.kafka.

我不知道如何正确地帮助管理YAML,但这是我们必须使其发挥作用的。