Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Kafka Idempotence Producer configuration

For the native Java Kafka client, there is a Kafka configuration called, enable.idempotence and we can set it to be true to enable idempotence producer.

However, for Spring Kafka, I can't find similar idempotence property in KafkaProperties class.

So I am wondering, if I manually set in my Spring Kafka configuration file, whether this property will take effect or Spring will totally ignore this config for Spring Kafka?

like image 909
ttt Avatar asked Jan 27 '23 01:01

ttt


1 Answers

There are two ways to specify this property

application.properties You can use this property to specify any additional properties on producer

spring.kafka.producer.properties.*= # Additional producer-specific properties used to configure the client.

If you have any additional common config between Producer and Consumer

spring.kafka.properties.*= # Additional properties, common to producers and consumers, used to configure the client.

Through Code You can also override and customize the configs

 @Bean
public ProducerFactory<String, String> producerFactory() {

   Map<String, Object> configProps = new HashMap<>();
   configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,bootstrapAddress);
    configProps.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true);
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
    StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, 
      StringSerializer.class);
    return new DefaultKafkaProducerFactory<>(configProps);
}

@Bean
public KafkaTemplate<String, String> kafkaTemplate() {
    return new KafkaTemplate<>(producerFactory());
    }
 }
like image 82
Deadpool Avatar answered Feb 05 '23 17:02

Deadpool