Spring Kafka 幂等生产者配置
Spring Kafka Idempotence Producer configuration
对于本机 Java Kafka 客户端,有一个名为 enable.idempotence
的 Kafka 配置,我们可以将其设置为 true
以启用幂等生产者。
但是,对于SpringKafka,我在KafkaProperties
class中找不到类似的幂等性属性。
所以我想知道,如果我在我的Spring Kafka配置文件中手动设置,这个属性是否会生效或者Spring会完全忽略这个配置Spring卡夫卡?
您可以使用 ProducerConfig
找到它,因为它是生产者配置。为了启用此功能,您需要在 producerConfigs 中添加以下行:
Properties producerProperties = new Properties();
producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
producerProperties.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true);
有两种方法可以指定这个属性
application.properties 您可以使用此 属性 指定 producer
上的任何其他属性
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());
}
}
您正在尝试添加 Spring KafkaProperties 未处理的功能,如果您查看文档,可以执行以下操作:
Only a subset of the properties supported by Kafka are available directly through the KafkaProperties class.
If you wish to configure the producer or consumer with additional properties that are not directly supported, use the following properties:
spring.kafka.properties.prop.one=first
spring.kafka.admin.properties.prop.two=second
spring.kafka.consumer.properties.prop.three=third
spring.kafka.producer.properties.prop.four=fourth
spring.kafka.streams.properties.prop.five=fifth
雅尼克
对于本机 Java Kafka 客户端,有一个名为 enable.idempotence
的 Kafka 配置,我们可以将其设置为 true
以启用幂等生产者。
但是,对于SpringKafka,我在KafkaProperties
class中找不到类似的幂等性属性。
所以我想知道,如果我在我的Spring Kafka配置文件中手动设置,这个属性是否会生效或者Spring会完全忽略这个配置Spring卡夫卡?
您可以使用 ProducerConfig
找到它,因为它是生产者配置。为了启用此功能,您需要在 producerConfigs 中添加以下行:
Properties producerProperties = new Properties();
producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
producerProperties.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true);
有两种方法可以指定这个属性
application.properties 您可以使用此 属性 指定 producer
上的任何其他属性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());
}
}
您正在尝试添加 Spring KafkaProperties 未处理的功能,如果您查看文档,可以执行以下操作:
Only a subset of the properties supported by Kafka are available directly through the KafkaProperties class.
If you wish to configure the producer or consumer with additional properties that are not directly supported, use the following properties:
spring.kafka.properties.prop.one=first
spring.kafka.admin.properties.prop.two=second
spring.kafka.consumer.properties.prop.three=third
spring.kafka.producer.properties.prop.four=fourth
spring.kafka.streams.properties.prop.five=fifth
雅尼克