Open In App

Spring Boot Kafka Producer Example

Improve
Improve
Like Article
Like
Save
Share
Report

Spring Boot is one of the most popular and most used frameworks of Java Programming Language. It is a microservice-based framework and to make a production-ready application using Spring Boot takes very less time. Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can “just run“. So some of the main features of Spring boot are listed below.

  • Create stand-alone Spring applications
  • Embed Tomcat, Jetty, or Undertow directly.
  • Provide ‘starter’ dependencies to simplify the build configuration.
  • Configure Spring and 3rd party libraries Automatically whenever possible.
  • Provide production-ready features like health checks, metrics, and externalized configuration.
  • Almost no code generation and no requirement for XML configuration.

Apache Kafka is a publish-subscribe messaging system. A messaging system lets you send messages between processes, applications, and servers. Broadly Speaking, Apache Kafka is software where topics (A topic might be a category) can be defined and further processed. Applications may connect to this system and transfer a message onto the topic. A message can include any kind of information, from any event on your Personal blog or can be a very simple text message that would trigger any other event. here we will be discussing how we can publish messages to Kafka topics with Spring Boot where Kafka is a pre-requisite 

Example:

Prerequisite: Make sure you have installed Apache Kafka in your local machine. Refer to this article How to Install and Run Apache Kafka on Windows?

Step 1: Go to this link https://start.spring.io/ and create a Spring Boot project. Add the following dependencies to your Spring Boot project. 

  • Spring Web
  • Spring for Apache Kafka

Step 2: Now let’s create a controller class named DemoController.

Java




// Java Program to Illustrate Controller Class
 
package com.amiya.kafka.apachekafkaproducer;
 
// Importing required classes
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;
 
// Annotation
@RestController
 
// Class
public class DemoController {
 
    // Autowiring Kafka Template
    @Autowired KafkaTemplate<String, String> kafkaTemplate;
 
    private static final String TOPIC = "NewTopic";
 
    // Publish messages using the GetMapping
    @GetMapping("/publish/{message}")
    public String publishMessage(@PathVariable("message")
                                 final String message)
    {
 
        // Sending the message
        kafkaTemplate.send(TOPIC, message);
 
        return "Published Successfully";
    }
}


 

 

Step 3: Now we have to do the following things in order to publish messages to Kafka topics with Spring Boot

 

  1. Run the  Apache Zookeeper server
  2. Run the  Apache Kafka  server
  3. Listen to the messages coming from the new topics

 

Run your Apache Zookeeper server by using this command

 

C:\kafka>.\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties

 

Similarly, run your Apache Kafka server by using this command

 

C:\kafka>.\bin\windows\kafka-server-start.bat .\config\server.properties

 

Run the following command to listen to the messages coming from the new topics 

 

C:\kafka>.\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic NewTopic --from-beginning

 

Step 4: Now run your spring boot application. Make sure you have changed the port number in the application.properties file

 

server.port=8081

 

Let’s run the Spring boot application inside the ApacheKafkaProducerApplication file

 

 

Step 5:Browse this URL and pass your message after the /publish/.

 

http://localhost:8081/publish/GeeksforGeeks

 

As we have passed “GeeksforGeeks” here you can see we got “Published Successfully” in return. And in real-time you can see the message has been published on the server also. The streaming of the message is in real-time. 

 

Output

 

Similarly, if we have passed “Hello World” here you can see we got “Published Successfully” in return. And in real-time you can see the message has been published on the server also.

 

Output

 



Last Updated : 02 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads