简体   繁体   中英

How to configure min.insync.replicas parameter on Spring Kafka Producer?

On a Java Spring application, I'm configuring a Producer to use Acks and must set min.insync.replicas parameter too.

I got it setup Acks using:

configProps.put(ProducerConfig.ACKS_CONFIG, kafkaAcks);

but I can't found the min.insync.replicas property on ProducerConfig . Looking on Kafka Spring Docs I not found the property related with min.insync.replicas .

So, how I configure min.insync.replicas on a Kafka Spring application?

    @Bean
public KafkaTemplate<String, GenericRecord> kafkaTemplate() {
    return new KafkaTemplate<>(producerFactory());
}

public ProducerFactory<String, GenericRecord> producerFactory() {
    return new DefaultKafkaProducerFactory<>(getProducerGenericRecordConfigurations());
}

private Map<String, Object> getProducerGenericRecordConfigurations() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBootstrapServerUrl);
    configProps.put(KafkaAvroDeserializerConfig.SCHEMA_REGISTRY_URL_CONFIG, schemaRegistryURL);
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class);
    configProps.put(ProducerConfig.ACKS_CONFIG, kafkaAcks);
    return configProps;
}

min.insync.replicas is not a Producer configuration. It's a broker/topic setting.

You can set it at the broker level in your server.properties files for your brokers.

Otherwise you can set it per topic, either at creation or by altering it.

To add-on, for Spring Kafka, you can configure it in

1 application.properties

spring.kafka.admin.properties.min-in-sync-replicas=3

2 KafkaAdmin bean (Takes precedence over 1 above)

@Bean
public KafkaAdmin kafkaAdmin() {
    Map<String, Object> configs = new HashMap<>();
    configs.put("spring.kafka.admin.properties.min.insync.replicas", 3); //This will apply to all topics created by KafkaAdmin

3 No configuration

The broker's default of 1 is used as per here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM