gpt4 book ai didi

apache-kafka - Spring Kafka 幂等生产者配置

转载 作者:行者123 更新时间:2023-12-03 20:27:49 24 4
gpt4 key购买 nike

对于原生 Java Kafka 客户端,有一个名为 enable.idempotence 的 Kafka 配置。我们可以将它设置为 true启用幂等生产者。

但是,对于 Spring Kafka,我在 KafkaProperties 中找不到类似的幂等性。类(class)。

所以我想知道,如果我在我的 Spring Kafka 配置文件中手动设置,这个属性会生效还是 Spring 会完全忽略 Spring Kafka 的这个配置?

最佳答案

有两种方法可以指定此属性

application.properties 您可以使用此属性来指定生产者的任何其他属性

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

如果您在生产者和消费者之间有任何额外的通用配置
spring.kafka.properties.*= # Additional properties, common to producers and consumers, used to configure the client.

直通码 您还可以覆盖和自定义配置
 @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());
}
}

关于apache-kafka - Spring Kafka 幂等生产者配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57145778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com