Multiple Kafka Stream Configurations in a Spring Boot Application

Spring Boot for Kafka provides the default configuration for Kafka streams and an annotation named “EnableKafkaStreams” to enable those Kafka streams default components.
In detail, there is a default config class “KafkaStreamsDefaultConfiguration” which is automatically imported when using the “EnableKafkaStreams” annotation (You can check the EnableKafkaStreams Javadoc for complete usage). This config class (“KafkaStreamsDefaultConfiguration”) registers a StreamsBuilderFactoryBean if a org.apache.kafka.streams.StreamsConfig with the name KafkaStreamsDefaultConfiguration# DEFAULT_STREAMS_CONFIG_BEAN_NAME is present in the application context. Otherwise an UnsatisfiedDependencyException} is thrown.

As you understand this is really useful for cases where as part of our application we use only one kStream or multiple but with the same configuration. Nevertheless when you need multiple kStreams in the same application with different configurations you would need to apply your own config and not rely on the default one.
For example a very important configuration that is needed when defining a kStream (as well as Kafka consumers) is the consumer group. For kStreams the “application.id” acts also as the consumer group name for the stream and it is needed in case we would like to point our stream application at an earlier or later offset (More details can be found here manage-consumer-groups)

Assuming you would like to have two kStreams with two different configs:
1) Do not use the “EnableKafkaStreams” annotation which only creates one factory bean (from the default configuration bean).
2) Specify your own stream builder beans

3) Inject the above two stream builder beans (“app1KafkaStreamsBuilder”, “app2KafkaStreamsBuilder”) respectively to the components that create your desired Kafka stream topology.

--

--

Founder | Senior Software Engineer at NCODIFY : An innovative Tech Professional Services company specializing in Software Development.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nick Drakopoulos

Founder | Senior Software Engineer at NCODIFY : An innovative Tech Professional Services company specializing in Software Development.