凝视Spring Boot Kafka项目时出错

时间:2019-04-03 14:39:14

标签: spring-boot kafka-consumer-api spring-kafka

启动spring boot kafka项目时出错。

spring boot:2.1.2发布 Spring Kafka版本:2.2.5.RELEASE

不能为ackMode MANUAL_IMMEDIATE的自动提交配置用户

消费者配置

@Bean
public Map<String, Object> consumerConfigs() {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
    props.put(ConsumerConfig.GROUP_ID_CONFIG, consumerGroup);
    props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, 30000);
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    return props;
}

Kafka侦听器容器工厂

@Bean("kafkaListenerContainerFactory")
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory(RetryTemplate retryTemplate) {
    ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
    factory.setRetryTemplate(retryTemplate);
    factory.setRecoveryCallback(context -> {
        log.error("Maximum retry policy has been reached {}", context.getAttribute("record"));
        Acknowledgment ack = (Acknowledgment) context.getAttribute(RetryingMessageListenerAdapter.CONTEXT_ACKNOWLEDGMENT);
        ack.acknowledge();
        return null;
    });
    factory.setConcurrency(Integer.parseInt(kafkaConcurrency));
    return factory;
}

1 个答案:

答案 0 :(得分:0)

对于MANUAL_IMMEDIATE确认模式(对于任何手动模式,本质上来说),必须关闭ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG使用者属性。

那是该异常的原因。

我想您不能在spring.kafka.consumer.enableAutoCommit中使用application.properties