Search code examples
spring-kafka

Can we have 2 different spring kafka consumer properties in a single application properties file?


Consumer1 Configuration details (for batch)

spring.kafka.consumer.[0].bootstrap-servers = ${bootstrap.servers1}
spring.kafka.consumer.[0].enable-auto-commit = false
spring.kafka.consumer.[0].auto-offset-reset = latest
spring.kafka.consumer.[0].max-poll-records = 100
spring.kafka.consumer.[0].key-deserializer= org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.[0].value-deserializer= org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.[0].properties.allow.auto.create.topics = false
spring.kafka.consumer.[0].properties.max.poll.interval.ms = 300000
spring.kafka.listener.[0].ack-mode = MANUAL
spring.kafka.listener.[0].concurrency = 1

Consumer2 Configuration details (for single record)

spring.kafka.consumer.[1].bootstrap-servers = ${bootstrap.servers2}
spring.kafka.consumer.[1].enable-auto-commit = false
spring.kafka.consumer.[1].auto-offset-reset = latest
spring.kafka.consumer.[1].key-deserializer= org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.[1].value-deserializer= org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.[1].properties.allow.auto.create.topics = false
spring.kafka.consumer.[1].properties.max.poll.interval.ms = 300000
spring.kafka.listener.[1].ack-mode = MANUAL
spring.kafka.listener.[1].concurrency = 1

Solution

  • No; to configure multiple sets of infrastructure, you need to define the beans manually instead of using auto configuration.

    However, you can override individual properties on the @KafkaListener directly:

        /**
         * Kafka consumer properties; they will supersede any properties with the same name
         * defined in the consumer factory (if the consumer factory supports property overrides).
         * <p>
         * <b>Supported Syntax</b>
         * <p>The supported syntax for key-value pairs is the same as the
         * syntax defined for entries in a Java
         * {@linkplain java.util.Properties#load(java.io.Reader) properties file}:
         * <ul>
         * <li>{@code key=value}</li>
         * <li>{@code key:value}</li>
         * <li>{@code key value}</li>
         * </ul>
         * {@code group.id} and {@code client.id} are ignored.
         * @return the properties.
         * @since 2.2.4
         * @see org.apache.kafka.clients.consumer.ConsumerConfig
         * @see #groupId()
         * @see #clientIdPrefix()
         */
        String[] properties() default {};
    

    e.g. @KafakListener( ..., properties = "bootstrap.servers=${bootstrap.servers2}")