DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Spring Cloud Stream: A Brief Guide
  • Using KRaft Kafka for Development and Kubernetes Deployment
  • Keep Your Application Secrets Secret
  • Auto-Scaling a Spring Boot Native App With Nomad

Trending

  • Is Agile Right for Every Project? When To Use It and When To Avoid It
  • Automatic Code Transformation With OpenRewrite
  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  1. DZone
  2. Software Design and Architecture
  3. Containers
  4. Setting Up Local Kafka Container for Spring Boot Application

Setting Up Local Kafka Container for Spring Boot Application

Set up Kafka container for Spring Boot application, which covers container configuration, Docker Desktop, and Spring Boot integration.

By 
Amol Gote user avatar
Amol Gote
DZone Core CORE ·
Jan. 06, 25 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
4.7K Views

Join the DZone community and get the full member experience.

Join For Free

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
 
version: '3'
services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
    ports:
      - "2181:2181"

  kafka:
    image: confluentinc/cp-kafka:latest
    environment:
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
    ports:
      - "9092:9092"
    depends_on:
      - zookeeper


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
 
docker-compose up -d


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
 
kafka-topics --create --topic user-notification --partitions 1 --replication-factor 1 --bootstrap-server localhost:9092


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
 
kafka.bootstrapAddress=localhost:9092


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.

application Docker (software) kafka Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Spring Cloud Stream: A Brief Guide
  • Using KRaft Kafka for Development and Kubernetes Deployment
  • Keep Your Application Secrets Secret
  • Auto-Scaling a Spring Boot Native App With Nomad

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: