Spring Cloud Stream
Kafka Topics
Multiple Consuming
Cloud Computing
Data Streaming

Spring cloud stream and consume multiple kafka topics

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Spring Cloud Stream is a framework for building highly scalable event-driven microservices connected with shared messaging systems. It allows developers to send and receive messages between different parts of an application by using an abstraction over messaging middleware such as Apache Kafka, RabbitMQ, and others.

Introduction to Spring Cloud Stream

Spring Cloud Stream builds upon Spring Boot and Spring Integration, providing easy configuration, flexibility, and a pluggable message-broker API. It extends the Spring programming model to support modern stream processing capabilities, enabling continuous data processing by connecting multiple independent microservices.

Core Concepts

  1. Bindings: The connection between application code and message broker channels.
  2. Binders: An abstraction that encapsulates messaging broker-specific implementation.
  3. Channels: Interfaces facilitating input and output messaging.

Setting Up With Kafka

Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. Integrating Spring Cloud Stream with Kafka provides a robust infrastructure to ensure scalable and flexible message consumption. Here’s a basic setup:

Dependencies

Include the following Maven dependencies in your pom.xml:

xml
1<dependencies>
2    <dependency>
3        <groupId>org.springframework.cloud</groupId>
4        <artifactId>spring-cloud-starter-stream-kafka</artifactId>
5    </dependency>
6</dependencies>

Application Configuration

Configure the application.yml to specify Kafka broker details:

yaml
1spring:
2  cloud:
3    stream:
4      kafka:
5        binder:
6          brokers: localhost:9092
7      bindings:
8        input1:
9          destination: topic1
10        input2:
11          destination: topic2
12        output:
13          destination: topic3

Consuming Multiple Kafka Topics

A common requirement in many stream-based applications is the ability to consume data from multiple Kafka topics simultaneously. Spring Cloud Stream caters to this by allowing developers to define multiple input bindings. Here’s how you can implement such functionality in a microservice.

Java Configuration

java
1@EnableBinding(Sink.class)
2public class KafkaMultipleTopicConsumer {
3
4    @StreamListener(Sink.INPUT1)
5    public void handleMessagesFromTopic1(String message) {
6        System.out.println("Received from topic1: " + message);
7    }
8
9    @StreamListener(Sink.INPUT2)
10    public void handleMessagesFromTopic2(String message) {
11        System.out.println("Received from topic2: " + message);
12    }
13}

Here, Sink.INPUT1 and Sink.INPUT2 are interfaces extending SubscribableChannel with respective bindings to topic1 and topic2.

Dynamic Destinations

If topic destinations are not known at the compile time or need to consume from dynamic topics, Spring Cloud Stream supports this scenario via programmable message channels.

java
1@Autowired
2private BinderAwareChannelResolver resolver;
3
4public void sendMessageToDynamicTopic(String topicName, String message) {
5    MessageChannel channel = resolver.resolveDestination(topicName);
6    channel.send(MessageBuilder.withPayload(message).build());
7}

Ensuring High Availability and Scalability

Leveraging Kafka and Spring Cloud Stream together offers significant advantages like seamless message rebalancing and fault tolerance. To scale the consumer service across multiple instances, simply run more instances of your application, and Kafka will handle the partitioning of the data across consumers for load balancing.

Summary Table

FeatureDescription
AbstractionSimplifies connectivity with various message brokers like Kafka.
FlexibilityConfigurable message channels and serialization.
ScalabilityScales via partitioned data processing and consumer groups.
Dynamic ConsumptionSupports dynamic topic consumption at runtime.
Fault ToleranceHigh availability through Kafka’s brokerage service.

Conclusion

Spring Cloud Stream with Kafka integration provides a powerful toolkit for building message-driven and event-rich applications that are robust, scalable, and maintainable. Through simple annotations and configurations, developers can easily establish communication between services, ensuring data consistency and real-time processing are maintained throughout the application lifecycle.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.