![]() |
VOOZH | about |
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.
Go to Spring Initializr and create a new project.
Download the project and import it into your IDE.
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.
Create a configuration class KafkaConfig.java to configure KafkaTemplate.
This class enables sending messages to Kafka topics
Create a controller class KafkaProducerController.java to send messages to a Kafka topic.
Start the required Kafka services:
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