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

Apache kafka Spring-Kafka幂等生产者配置,apache-kafka,spring-kafka,Apache Kafka,Spring Kafka,对于本机Java Kafka客户端,有一个名为enable.idemptence的Kafka配置,我们可以将其设置为true以启用idemptence producer 然而,对于SpringKafka,我在KafkaProperties类中找不到类似的幂等性属性 所以我想知道,如果我在我的Spring Kafka配置文件中手动设置,该属性是否会生效,或者Spring是否会完全忽略Spring Kafka的此配置?您可以通过ProducerConfig找到它,因为它是生产者配置。要启用此功能,您

对于本机Java Kafka客户端,有一个名为
enable.idemptence
的Kafka配置,我们可以将其设置为
true
以启用idemptence producer

然而,对于SpringKafka,我在
KafkaProperties
类中找不到类似的幂等性属性


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

您可以通过
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());
    }
 }
@Bean
公共生产工厂生产工厂(){
Map configProps=new HashMap();
configProps.put(ProducerConfig.BOOTSTRAP\u SERVERS\u CONFIG,bootstrapAddress);
configProps.put(ProducerConfig.ENABLE\u idemptence\u CONFIG,true);
configProps.put(ProducerConfig.KEY\u序列化程序\u类\u配置,
StringSerializer.class);
configProps.put(ProducerConfig.VALUE\u序列化程序\u类\u配置,
StringSerializer.class);
返回新的DefaultKafkaProducerFactory(configProps);
}
@豆子
公共卡夫卡模板卡夫卡模板(){
返回新的卡夫卡模板(producerFactory());
}
}

您试图添加Spring Kafkapproperties无法处理的功能,如果查看文档,可以执行以下操作:

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

扬尼克