Search Tutorials

Spring Boot + Apache Kafka Hello World Example | JavaInUse

Spring Boot + Apache Kafka Hello World Example

In this post we will integrate Spring Boot and Apache Kafka instance.
In a previous post we had seen how to get Apache Kafka up and running.

Spring Boot Apache Kafka Tutorial

RabbitMQ - Table Of Contents

What is Apache Kafka Understanding Apache Kafka Architecture Internal Working Of Apache Kafka Getting Started with Apache Kafka - Hello World Example Spring Boot + Apache Kafka Example


This tutorial is explained in the below Youtube Video.

Lets Begin-

The project will be as follows-
Define the pom.xml as follows- Add the spring-kafka dependency.
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">





Define the KafkaSender class to send message to the kafka topic named as java_in_use-topic.
package com.javainuse.service;

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

public class KafkaSender {
	private KafkaTemplate<String, String> kafkaTemplate;
	String kafkaTopic = "java_in_use_topic";
	public void send(String message) {
	    kafkaTemplate.send(kafkaTopic, message);

Define a Controller which will pass the message and trigger the send message to the Kafka Topic using the KafkaSender class.
package com.javainuse.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.javainuse.service.KafkaSender;

@RequestMapping(value = "/javainuse-kafka/")
public class ApacheKafkaWebController {

	KafkaSender kafkaSender;

	@GetMapping(value = "/producer")
	public String producer(@RequestParam("message") String message) {

		return "Message sent to the Kafka Topic java_in_use_topic Successfully";

Finally Define the Spring Boot Class with @SpringBootApplication annotation
package com.javainuse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootHelloWorldApplication {

	public static void main(String[] args) {
				new Object[] { SpringBootHelloWorldApplication.class }, args);

We are done with the required Java code. Now lets start Apache Kafka. As we had explained in detail in the Getting started with Apache Kafka perform the following.
  • Start Apache Zookeeper-
    C:\kafka_2.12->.\bin\windows\zookeeper-server-start.bat .\config\

    Spring Boot Apache Zookeeper
  • Start Apache Kafka-
    C:\kafka_2.12->.\bin\windows\kafka-server-start.bat .\config\

    Spring Boot Apache Kafka
  • Next start the Spring Boot Application by running it as a Java Application.
  • Also Start the consumer listening to the java_in_use_topic-
    C:\kafka_2.12->.\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic java_in_use_topic --from-beginning

    Spring Boot Apache Kafka Example
  • Finally hit the url as follows- http://localhost:8080//javainuse-kafka/producer?message=test
    Spring Boot Apache Kafka Topic
  • This will trigger the message to be sent to the java_in_use_topic. We can see in the consumer started the message is recieved.
    Spring Boot Apache Kafka Consume

Download Source Code

Download it -
Spring Boot + Apache Kafka Hello World Example