Write a Kafka consumer with Java using Spring Boot
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:
- The class TemperatureSensorService works as a Kafka Consumer, implementing the logic to read temperature and humidity data from a Kafka topic named temperatures;
- 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).
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:
- Install Kafka