Apache Kafka with Spring Boot Application

Steps we will follow:

  1. Run Kafka server in your local system
  2. Create two Spring Boot project (one for producer and one for consumer) using Spring Initializer
  3. Add Kafka configuration in Consumer service
  4. Add Kafka configuration in Producer service

Software required:

  1. IntelliJ (Community version)
  2. Kafka-2.8.1 (Download binary folder)

Step 1:

zookeeper-server-start.bat config/zookeeper.properties
kafka-server-start.bat config/server.properties
kafka-console-producer.bat --topic test-kafka --bootstrap-server localhost:9092
kafka-console-consumer.bat --topic test-kafka --from-beginning --bootstrap-server localhost:9092

Step 2:


Step 3:

spring.kafka.consumer.bootstrap-servers= localhost:9092
spring.kafka.consumer.group-id= mygroup
package com.example.kafka.kafkaconsumer.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

public class Consumer {
private final Logger logger = LoggerFactory.getLogger(Consumer.class.getName());

@KafkaListener(topics = "commentary")
public void consume(String msg) {
logger.info(String.format("->> %s", msg));
kafka-console-producer.bat — topic commentary — bootstrap-server localhost:9092

Step 4:

spring.kafka.producer.bootstrap-servers= localhost:9092
package com.example.kafkaproducer.service;

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

public class Producer {
private static final Logger logger = LoggerFactory.getLogger(Producer.class);

private KafkaTemplate<String, String> kafkaTemplate;

public void sendMessage(String Topic, String message) {
logger.info(String.format("#### -> Producing message -> %s", message));
this.kafkaTemplate.send(Topic, message);
package com.example.kafkaproducer.controller;

import com.example.kafkaproducer.service.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping(value = "/kafka")
public class KafkaController {

private final Producer producer;

KafkaController(Producer producer) {
this.producer = producer;

@PostMapping(value = "/send")
public void sendMessageToKafkaTopic(@RequestBody String message) {
this.producer.sendMessage("commentary", message);




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Magic of Houdini and CSS Paint Worklets

Build your Android app Faster and Smaller than ever

Weekly Development Update

I will convert psd, xd, figma, sketch to html css bootstrap

How to reduce flakiness/Save the time of Test case execution using the “test retries” feature of…

Introducing AWS Cloud9: A Cloud-Based IDE

ⲶNERGY LIST in #Consortia

Part I: What’s A Coding Bootcamp? App Academy vs. Hack Reactor — Career Karma

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shyamal Jadav

Shyamal Jadav

More from Medium

Apache Kafka Configuration In Spring Boot With Producer And Consumer Example

Spring boot Elasticsearch Remote Access

Spring Boot Admin: Initiation

Microservices with Spring Boot Part 2