Setting Up Local Kafka Container for Spring Boot Application

In today’s microservices and event-driven architecture, Apache Kafka is the de facto for streaming applications. However, setting up Kafka for local development in conjunction with your Spring Boot application can be tricky, especially when configuring it to run locally. 

Spring Boot application provides support for Kafka integration through the spring-kafka maven package. To work with spring-kafka, we need to connect to the Kafka instance. Typically, during development, we would just run a local Kafka instance and build against it. But with Docker Desktop and containers, things are much easier to set up than running a local Kafka instance. This article guides us through the steps for setting up the local Kafka container with the Spring Boot application.  

Prerequisites 

  1. We need to set up Docker Desktop. To do so, we can refer to this article
  2. Spring Boot application with spring-kafka package configured. 

Running Kafka Container

For running the Kafka container, we will first use the following docker-compose file:

YAML

 

This docker-compose file contains the configurations to pull the Kafka container and its dependency, the Zookeeper container. Zookeeper manages Kafka broker nodes in the cluster; we can find further details about this in this article.  

For registering the containers with Docker Desktop, we will use the following command:

PowerShell

 

Command prompt

This will pull the required images and launch the containers, and once the containers are launched, you can see the containers in Docker Desktop like below:

Containers in the docker desktop

Now that the Kafka container is up, we can then create the required topics using Docker Desktop console using the following command: 

PowerShell

 

Required topics

Now that the container is up and the required prerequisites have been performed, we can launch the Spring Boot application.  For the Spring Boot application, configure the Kafka bootstrap address as below:

Properties files

 

When we launch the Spring Boot application, we should see the logs for connection with Kafka, depending on the type of Spring Boot application, whether it is a producer or consumer.

Following the steps outlined in the article, we set up a local development environment using a Kafka container and a Spring Boot application.

Source:
https://dzone.com/articles/setting-up-local-kafka-container-for-spring-boot-application