![]() |
VOOZH | about |
In Apache Kafka, topics are used to organize and store messages. They act as logical channels where producers send messages and consumers read them. In a Spring Boot application, Kafka topics can be created and configured programmatically so that they are automatically generated when the application starts.
Go to Spring Initializr and create a new Spring Boot project.
If the dependency was not added during project creation, add it manually in the pom.xml file.
Create a new class named TopicConfiguration.java.Annotate the class with @Configuration.
Create a KafkaAdmin bean to connect the application with the Kafka broker. 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>
Now create a topic using TopicBuilder. A lot of various settings can be used right here while creating a topic.
File: TopicConfiguration.java
Run the main Spring Boot application class.
Use this command:
kafka-topics.sh --bootstrap-server localhost:9092 --topic <topic_name> --describe
Output:
This is the output from the server.