Spring Cloud
Kafka Stream
Producer Creation
Programming
Version 3.1

How can create a producer using Spring Cloud Kafka Stream 3.1

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 message-driven microservices and it provides support for Apache Kafka Streams through the Binder implementation. In this article, we will discuss how to create a producer using Spring Cloud Kafka Streams 3.1, leveraging the flexibility and the power of the Kafka Streams API.

Introduction to Spring Cloud Stream

Spring Cloud Stream abstracts away the complexity involved in producing and consuming messages. It allows developers to focus on business logic, relying on the framework to handle the infrastructure concerns. It is configurable through simple declarative properties, is dynamically scalable, and seamlessly integrates with Spring Boot.

Setting Up Spring Boot Project

To start with, create a new Spring Boot project. You can use Spring Initializr to generate the project skeleton. Choose Maven or Gradle as the build tool, and add 'Spring Cloud Stream' and 'Kafka Streams' as dependencies.

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

For Gradle, include:

groovy
implementation 'org.springframework.cloud:spring-cloud-starter-stream-kafka'
implementation 'org.springframework.kafka:spring-kafka'

Configuration

In application.yml (or application.properties), you need to set Kafka-specific properties:

yaml
1spring:
2  cloud:
3    stream:
4      kafka:
5        binder:
6          brokers: localhost:9092
7      bindings:
8        output-channel:
9          destination: topic-name
10          producer:
11            configuration:
12              key.serializer: org.apache.kafka.common.serialization.StringSerializer
13              value.serializer: org.apache.kafka.common.serialization.StringSerializer

Creating the Producer

Spring Cloud Stream uses an abstraction called bindings to connect application code to message brokers. A typical Kafka producer in Spring would look something like this:

Java Configuration:

java
1@EnableBinding(Source.class)
2public class KafkaProducer {
3
4    @Autowired
5    private MessageChannel outputChannel;
6
7    public void send(String message) {
8        outputChannel.send(MessageBuilder.withPayload(message).build());
9    }
10}

Here, Source.class contains the default output channel named output. This will be used to send messages to Kafka.

Simplified Configuration with Functional Style

With newer versions of Spring Cloud Stream (post 3.0), you can use the functional programming model which is more succinct:

java
1@SpringBootApplication
2public class KafkaProducerApplication {
3
4    public static void main(String[] args) {
5        SpringApplication.run(KafkaProducerApplication.class, args);
6    }
7
8    @Bean
9    public Supplier<String> produce() {
10        return () -> "Hello Kafka Streams with Spring Cloud Stream!";
11    }
12}

Key Concepts

  • Binder: Abstracts away the middleware-specific details. For Kafka, configurations related to topics, serialization, and deserialization are handled here.
  • Bindings: Defines how application methods are connected to external message systems.
  • Supplier: Part of the functional API, replaces message-handling methods with supplier functions that wrap the output data.

Summary Table

TermDescription
BinderAbstraction that manages interactions with message brokers.
BindingsConnection setup between application methods and messaging systems.
ProducerApplication that creates and sends messages to the Kafka topic.
SerializationProcess of converting an object into a binary or textual format to transport messages.

Conclusion

Using Spring Cloud Stream with Kafka Streams is a powerful combination allowing for robust, scalable applications that can handle streams of data effectively. The framework's approach simplifies the creation of messaging capabilities in microservices, and the latest programming model makes the declaration and development of stream processing applications more accessible.

Spring Cloud Kafka Streams is particularly useful for developers looking to implement event-driven architectures without becoming entangled in the underlying Kafka complexities such as topic management, serialization, and consumer grouping.


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.