Write a Kafka consumer with Java using Spring Boot

Roberto Pozzi
5 min readMay 15, 2023

Recently I wrote the article Write a Kafka producer with Java using Spring Boot; the other side of the coin in an event streaming platform like Kafka is clearly a consumer, a component that consumes and acts on the messages the producer has published to a Kafka topic.

Building on the first article, I also used Spring Boot (https://docs.spring.io/spring-boot/docs/3.1.0-SNAPSHOT/reference/html/index.html) to develop a simple Java based Kafka consumer which reads temperature and humidity data from a Kafka topic:

  1. The class TemperatureSensorService works as a Kafka Consumer, implementing the logic to read temperature and humidity data from a Kafka topic named temperatures;
  2. The class Sensor maps the sensor data, either a real sensor or a simulated one, but the consumer doesn’t know (this is the beauty of decoupling).
Figure 1 — Kafka consumer example

Setup the environment

Refer to my previous article Write a Kafka producer with Java using Spring Boot and Apache Kafka quickstart (https://kafka.apache.org/quickstart) to download, extract Kafka on your local computer (at the time of writing the latest version was v2.13–3.4.0) and use Kafka command line tools to setup the environment doing the following:

  1. Install Kafka

--

--

Roberto Pozzi
Roberto Pozzi

Written by Roberto Pozzi

Roberto works as Cloud Architect, specialized in cloud native and container based architectures.

No responses yet