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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

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

  • Event-Driven Architectures: Designing Scalable and Resilient Cloud Solutions
  • Building Custom Tools With Model Context Protocol
  • A Modern Stack for Building Scalable Systems
  • Cloud Security and Privacy: Best Practices to Mitigate the Risks
  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
Oops! Something Went Wrong

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
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!