Timothy
Timothy

Reputation: 1035

Spring Boot for Kafka - Multiple KafkaStreams config in one app

In spring for kafka stream can we define two configurations in one app? If we have two @Bean for config, like this, how can I use secondConfig into the stream?

@Configuration
@EnableKafkaStreams
public class KafkaStreamConfig {

    @Bean(name = KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_CONFIG_BEAN_NAME)
    public KafkaStreamsConfiguration kafkaStreamConfig() {
        var props = new HashMap<String, Object>();

        props.put(StreamsConfig.APPLICATION_ID_CONFIG, "kafka-stream");
        props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");

        ....
 
        return new KafkaStreamsConfiguration(props);
    }

    @Bean(name = "secondConfig")
    public KafkaStreamsConfiguration kafkaStreamConfig() {
        var props = new HashMap<String, Object>();

        props.put(StreamsConfig.APPLICATION_ID_CONFIG, "kafka-stream");
        props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "other-machine:9092");

        ....
 
        return new KafkaStreamsConfiguration(props);
    }


}

Thanks

Upvotes: 0

Views: 849

Answers (1)

Gary Russell
Gary Russell

Reputation: 174689

Spring will only create one factory bean (from the default configuration bean). For the second one you will need to define a StreamsBuilderFactoryBean that uses it.

Upvotes: 1

Related Questions