spring-bootapache-kafka

Make Kafka topics be created automatically from spring boot


My Spring boot project has 4 modules and I needed to use Kafka to establish communication between the modules. I installed Kafka on my computer and created the topics using cmd.

The problem is, after I submit my project, the topics won't work unless my teacher has the same topics created on his computer (from what I understand).

Is there a way to make sure my project automatically creates the topics?


Solution

  • Yes, you can use spring-kafka dependency and add some Topic beans

    @Bean
    public NewTopic topic() {
      return TopicBuilder.name("example")....build();
    }
    

    https://docs.spring.io/spring-kafka/reference/kafka/configuring-topics.html