How to consume from Kafka Spring Cloud Stream by default and also consume a Kafka message generated by the confluent API?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Kafka is a powerful distributed messaging system that has gained significant traction in handling high-volume, real-time data streams. With Java being a popular choice for Kafka applications, frameworks like Spring Cloud Stream provide sophisticated abstractions for Kafka message handling, simplifying application development and improving code maintainability.
Consuming Kafka Messages in Spring Cloud Stream
Spring Cloud Stream is a framework built on top of Spring Boot designed to build highly scalable event-driven microservices connected with shared messaging systems. It abstracts away many of the complexities associated with typical message brokers such as Kafka.
Prerequisites
- JDK 8 or later
- Maven or Gradle
- Kafka running instance
Configuring a Spring Cloud Stream Application
To use Spring Cloud Stream with Kafka, start by adding the necessary dependencies in your pom.xml or build.gradle. Below is an example for a Maven project:
Application Configuration
Configure the application to connect to Kafka through application.yml or application.properties:
Here, my-topic is the Kafka topic from which messages will be consumed, and my-group is the consumer group of your service.
Define a Listener
Spring Cloud Stream uses functional programming model. Define a bean for handling incoming Kafka messages:
The method input is a bean that will automatically attach to the Kafka topic configured.
Error Handling
Handle potential errors by configuring a consumer property like so:
This configuration will attempt to consume a message a maximum of 3 times before failing.
Consuming Kafka Messages Generated by Confluent API
The Confluent Platform is a more enterprise-ready modification of Apache Kafka that simplifies the management and development of Kafka systems. Kafka messages generated by the Confluent API can be consumed in a similar manner as those from vanilla Kafka.
Key Considerations
- Schema Registry: Confluent often uses Schema Registry for schema management, essential when your data format is complex, such as AVRO.
- Compatibility: Ensure the compatibility settings (e.g., full, backward) in Schema Registry meet your application’s requirements.
Configurations might require extensions, such as:
This configuration enables Avro serializers and sets the Schema Registry’s URL.
Summary Table
| Feature | Spring Cloud Stream | Confluent Kafka API |
| Maven Dependency | spring-cloud-starter-stream-kafka | same as Spring Cloud Stream |
| Broker Configuration | application.yaml or application.properties | Same as standard Kafka but might require additional properties like schema.registry.url |
| Programming Model | Functional (Consumer, Supplier, Function) | Same as Spring Cloud Stream |
| Error Handling | Configurable properties (max-attempts, back-off) | Same, but consideration for schema incompatibilities |
| Schema Registry Integration | Configuration extension needed | Essential for utilizing Confluent enhanced features |
Enhancements
- Monitoring and Observability: Integrate Spring Cloud Stream applications with observability tools like Spring Boot Actuator, Prometheus, and Grafana for real-time monitoring metrics.
- Kubernetes Deployment: Profile your application for deployment in a Kubernetes cluster to manage high availability, scalability, and resiliency.
- Advanced Message Processing: Utilize stateful operations available in Kafka Streams with Spring Cloud Stream for more sophisticated analytics and real-time stream processing.
By understanding both the simple and advanced usage options provided by Spring Cloud Stream and Confluent, you can effectively build robust, microservices-driven streaming applications in Kafka.

