Terjadi kesalahan saat memulai proyek spring boot kafka.

Spring boot : 2.1.2.RELEASE Spring kafka version : 2.2.5.RELEASE

Konsumen tidak dapat dikonfigurasi untuk komit otomatis untuk ackMode MANUAL_IMMEDIATE

Konfigurasi konsumen

@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;
}

Pabrik kontainer Kafka Listener

@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;
}
2
Bharat Wadhwa 3 April 2019, 17:39

1 menjawab

Jawaban Terbaik

Untuk mode ack MANUAL_IMMEDIATE (untuk mode manual apa pun, pada dasarnya), properti konsumen ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG harus dimatikan.

Itulah alasan pengecualian itu.

Saya kira Anda tidak bisa menggunakan spring.kafka.consumer.enableAutoCommit di application.properties Anda.

1
Artem Bilan 3 April 2019, 14:50