...

/

Producer and Consumer Applications Using Spring Kafka

Producer and Consumer Applications Using Spring Kafka

Learn how to send and receive messages to and from Kafka using Spring Kafka.

In this lesson, we will learn to use Kafka with a Spring Boot application with the help of a couple of additional applications.

Producer application

In this section, we will use the KafkaTemplate API in a Spring Boot application to send messages to Kafka. The application consists of a REST endpoint using Spring Boot web with the spring-boot-starter-web module. The application will be packaged as a JAR file, but run as a self-contained HTTP server by using an embedded Tomcat server.

Click the “Run“ button in the widget below. This will initiate the build process and start the application. Once the application has started, you should see the Started SpringKafkaProducerApplication message.


After that, follow the steps outlined below:

package com.example.demo;

import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageProducer {
    
    private static final String TOPIC = "spring-kafka-demo-topic";

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendMessage(String message) {
        this.kafkaTemplate.send(TOPIC, message);
    }
}
Spring Boot Kafka: Producer application

Invoke the REST endpoint to send messages. Click the “+” button to open a new terminal tab and run these commands.

curl -i -X POST -d 'redis=keyvalue' http://localhost:8080/send
curl -i -X POST -d 'postgres=rdbms' http://localhost:8080/send
curl -i -X POST -d 'cassandra=column' http://localhost:8080/send
curl -i -X POST -d 'influxdb=timeseries' http://localhost:8080/send
curl -i -X POST -d 'mongodb=document' http://localhost:8080/send
Sending messages

For each of these commands, you should see a response similar to ...

Access this course and 1400+ top-rated courses and projects.