VOOZH about

URL: https://www.geeksforgeeks.org/java/spring-boot-how-to-publish-string-messages-on-apache-kafka/

⇱ Spring Boot | How to publish String messages on Apache Kafka - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring Boot | How to publish String messages on Apache Kafka

Last Updated : 14 Mar, 2026

Apache Kafka is a distributed publish–subscribe messaging system that allows applications to send and receive messages between different services. In a Spring Boot application, Kafka can be used to publish string messages to Kafka topics for communication between systems. This helps in building real-time and event-driven applications.

  • Spring Boot can send string messages to Kafka topics using a producer.
  • Messages are simple strings (sequence of characters) sent for processing.
  • Kafka enables communication between different services and applications.

Steps to Implement Spring Boot Kafka String Producer

Step 1: Create Spring Boot Project

Go to Spring Initializr and create a new project.

  • Project: Maven
  • Language: Java
  • Dependencies: Spring Web, Spring for Apache Kafka

Download the project and import it into your IDE.

Step 2: Configure Kafka in application.properties

Open application.properties and add the Kafka server configuration.

spring.kafka.bootstrap-servers=localhost:9092

This connects the Spring Boot application to the Kafka broker.

Step 3: Create Kafka Configuration Class

Create a configuration class KafkaConfig.java to configure KafkaTemplate.

This class enables sending messages to Kafka topics

Step 4: Create Kafka Producer (Publish String Message)

Create a controller class KafkaProducerController.java to send messages to a Kafka topic.

Step 5: Start Kafka Services

Start the required Kafka services:

  1. Start Zookeeper Server
  2. Start Kafka Server

Step 6: Create Kafka Topic

For Mac / Linux:

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic message-topic

For Windows:

.\bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic message-topic

Step 7: Run Spring Boot Application

  • Start the main Spring Boot class.
  • Send a request to publish the message.

Step 8: Verify Output

  • The string message will be published to the Kafka topic.
  • Consumers subscribed to the topic will receive the message.
Comment
Article Tags: