Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Spring Boot - Create and Configure Topics in Apache Kafka
Next article icon

Spring Boot - Create and Configure Topics in Apache Kafka

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Topics are a special and essential component of Apache Kafka that are used to organize events or messages. In other words, Kafka Topics enable simple data transmission and reception across Kafka Servers by acting as Virtual Groups or Logs that store messages and events in a logical sequence. In this article, we will discuss about how to configure Kafka topics from the spring boot application.

Step by Step Implementation

First, our spring boot project needs Kafka dependencies. Include this part in your pom.xml file. Any Kafka version is accessible. We would prefer that you use the most recent Kafka client. For additional information on the Kafka version.

Java
<dependency>     <groupId>org.springframework.kafka</groupId>     <artifactId>spring-kafka</artifactId>     <version>2.9.0</version>     <scope>compile</scope> </dependency> 

Create a class with whatever name you prefer, but I used TopicConfiguration.java. We need to convert this class into a bean, and since we are configuring Kafka topics, we included the @Configuration annotation from Spring Boot. 

Java
import java.io.*;  @Configuration public class TopicConfiguration {    } 

The process of adding new topics to our broker is handled by a KafkaAdmin bean. A KafkaAdmin bean is automatically registered with Spring Boot. The KafkaAdmin bean has to be explicitly registered for non-Spring Boot applications. The next step is to construct a new bean that tells KafkaAdmin what our Kafka URL is. BOOTSTRAP_SERVERS_CONFIG: Host and port on where Kafka broker is running.

BOOTSTRAP_SERVERS_CONFIG: <kafka_url>:<port>
Java
@Bean  public KafkaAdmin admin() {     Map<String, Object> configs = new HashMap<>();     configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,                 "localhost:9092");     return new KafkaAdmin(configs); } 

We can now configure and create Kafka topics. A lot of various settings can be used right here while creating a topic. For more information about TopicBuilder.

Java
import java.io.*;  @Bean  public NewTopic topic1() {     return TopicBuilder.name("TOPIC-1")         .partitions(1)         .replicas(1)         .build(); }  @Bean public NewTopic topic2() {     return TopicBuilder.name("TOPIC-2")         .partitions(1)         .replicas(1)         .build(); } 

File: TopicConfiguration.java

Java
import java.io.*;  @Configuration public class TopicConfiguration {      @Bean        public KafkaAdmin admin()     {         Map<String, Object> configs = new HashMap<>();         configs.put(             AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,             "localhost:9092");         return new KafkaAdmin(configs);     }      @Bean        public NewTopic topic1()     {         return TopicBuilder.name("TOPIC-1")             .partitions(1)             .replicas(1)             .build();     }      @Bean        public NewTopic topic2()     {         return TopicBuilder.name("TOPIC-2")             .partitions(1)             .replicas(1)             .build();     } } 

In the Springboot application, Kafka topics can be created and configured this way. The spring boot application will automatically create Kafka topics on the specified Kafka broker when it is launched. To get the topic configuration details on the server, run this command.

kafka-topics.sh --bootstrap-server localhost:9092 --topic <topic_name> --describe

Output:

This is the output from the server.

Kafka topic details
 

Next Article
Spring Boot - Create and Configure Topics in Apache Kafka

M

malavmevada
Improve
Article Tags :
  • Java
  • Technical Scripter
  • Technical Scripter 2022
  • Java-Spring-Boot
  • Apache Kafka Java
Practice Tags :
  • Java

Similar Reads

    Containerizing Spring Boot Application
    Java applications, known for their robustness sometimes become difficult to deploy and manage. This is where containerization comes into the picture. Packaging your Java app into a lightweight and self-contained independent unit can provide many benefits to the developers: PortabilityScalabilityFast
    3 min read
    How to Send Images in Spring Boot without using Servlet and Redundant JSP Codes?
    Open IntelliJ IDE and in the Controller section of your project inside the Controller class you need to build the Image serve method. The Image serve method looks like below and as you have to collect the Image response use @GetMapping Annotation to collect the image. ImageHandler Controller Method
    2 min read
    How to Create Todo List API using Spring Boot and MySQL?
    Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
    6 min read
    Spring Boot - Transaction Management Using @Transactional Annotation
    The @Transactional annotation is the metadata used for managing transactions in the Spring Boot application. To configure Spring Transaction, this annotation can be applied at the class level or method level. In an enterprise application, a transaction is a sequence of actions performed by the appli
    9 min read
    Spring Boot - Map Entity to DTO using ModelMapper
    In enterprise applications, we use RESTful services to establish the communication between client and server. The general idea is that the client sends the request to the server and the server responds to that request with some response. Generally, we have three different layers in most of the appli
    8 min read
    Spring Boot | How to consume JSON messages using Apache Kafka
    Apache Kafka is a stream processing system that lets you send messages between processes, applications, and servers. In this article, we will see how to publish JSON messages on the console of a Spring boot application using Apache Kafka. In order to learn how to create a Spring Boot project, refer
    3 min read
    Spring Boot | How to consume string messages using Apache Kafka
    Apache Kafka is a publish-subscribe messaging queue used for real-time streams of data. A messaging queue lets you send messages between processes, applications, and servers. In this article we will see how to send string messages from apache kafka to the console of a spring boot application.  Appro
    3 min read
    Spring Boot | How to publish String messages on Apache Kafka
    Apache Kafka is a publish-subscribe messaging system. A messaging queue lets you send messages between processes, applications, and servers. In this article, we will see how to send string messages to Apache Kafka in a spring boot application. In order to learn how to create a spring boot project, r
    2 min read
    Spring Boot | How to publish JSON messages on Apache Kafka
    Apache Kafka is a publish-subscribe messaging system. A messaging queue lets you send messages between processes, applications, and servers. In this article, we will see how to send JSON messages to Apache Kafka in a spring boot application. In order to learn how to create a spring boot project, ref
    4 min read
    Spring Boot - Consume JSON Object From Kafka Topics
    Apache Kafka is a publish-subscribe messaging system. A messaging system lets someone is sending messages between processes, applications, and servers. Broadly Speaking, Apache Kafka is software where topics (A topic might be a category) can be defined and further processed. Applications may connect
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences