Spring Boot Apache Kafka Example, easy in 15 minutes

The Spring Boot Apache Kafka Example introduces you to the way Spring Boot will auto-configure a Spring Kafka application based on the jar dependencies using default values. But you make sure the spring-kafka-xxx.jar is on the project classpath. Let’s dig deeper.

Spring Boot Apache Kafka Example

Download and Install Apache Kafka

If you work on Windows platform, please read the post to know the way to download and install Apache Kafka. If you work on other platforms please read the official documentation here. So far, Apache Kafka is started on your local machine.

Project Setup

  • Spring Kafka: 2.3.4.RELEASE
  • Spring Boot: 2.2.2.RELEASE
  • Apache Kafka: kafka_2.13-2.4.0
  • Maven: 3.5

Project Structure

Spring Boot Apache Kafka Example

Maven Dependencies

At least, we must have these dependencies: spring-boot-starter, spring-kafka in pom.xml file. Note, you can change spring-kafka version for your own needs.

Configure Kafka Application with application.yml

Spring Boot takes care of most of the configuration. However, some properties that need to be explicitly set in the application.yml file:

  1. The kafka.consumer.auto-offset-reset:earliest by default, it will start reading from the beginning of the topic and stream all of the existing.
  2. The kafka.consumer.group-id: employee you should always configure group.id unless you are using the simple assignment API and you don’t need to store offsets in Kafka.

Sending Kafka Messages with Spring Boot

Spring Boot automatically configures and initializes a KafkaTemplate based on the properties configured in the application.yml property file.

Receiving Kafka Messages with Spring Boot

By annotating a method with @KafkaListener annotation Spring Kafka will automatically create a message listener container. Annotation that marks a method to be the target of a Kafka message listener on the specified topics.

Spring Boot Application

Now, we create a Spring Boot application to demonstrate the application like below.

Running the Application

Before we make a demo for the Spring Boot Apache Kafka example, Kafka server must start on localhost with port 9092 which is the default configuration of Kafka. Then we run Spring boot application normally and see the output.

That’s all about Spring Boot Apache Kafka Example. You should download the complete source code and run on your local machine to get more deeply the way Kafka works.

Download complete source code

spring-kafka-spring-boot-example.zip (2 downloads)

References

Apache Kafka Official Website
Spring Kafka Client Compatability
Spring Kafka Documentation
Spring Boot Tutorial for Beginners

Please share it if you found this useful
Hide Buttons