Integrating Spring Boot with Apache Kafka

Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. It is widely used for building real-time data pipelines and streaming applications. By integrating Kafka with Spring Boot, you can easily create event-driven applications that communicate through asynchronous messaging. In this post, we will explore how to set up a Spring Boot application with Kafka and demonstrate the basics of producing and consuming messages.

What is Apache Kafka?

Apache Kafka is an open-source stream processing platform that allows you to publish, subscribe to, store, and process streams of records in real-time. Kafka’s design is based on:

  • Topics: A category or feed name to which records are published.
  • Producers: Clients that publish records to Kafka topics.
  • Consumers: Clients that read records from Kafka topics.
  • Clusters: A Kafka cluster is made up of multiple Kafka brokers that manage the data.

Setting Up Kafka with Spring Boot

To start using Kafka with Spring Boot, you need to add the correct dependencies and set up Kafka on your system.

1. Adding Kafka Dependencies

Include the following dependencies in your pom.xml:

<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>

2. Configuring Kafka Properties

Add Kafka configuration in the application.properties file to set the bootstrap server and other properties:

spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=my-group
spring.kafka.consumer.auto-offset-reset=earliest

Creating a Producer

A producer sends messages to a Kafka topic. Here’s how to create a simple producer in a Spring Boot application:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class MessageProducer {

    private static final String TOPIC = "my-topic";

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendMessage(String message) {
        kafkaTemplate.send(TOPIC, message);
        System.out.println("Produced message: " + message);
    }
}

Creating a Consumer

The consumer listens for messages from a Kafka topic. Here’s how to configure a simple consumer:

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class MessageConsumer {

    @KafkaListener(topics = "my-topic", groupId = "my-group")
    public void listen(String message) {
        System.out.println("Consumed message: " + message);
    }
}

Running Kafka and Your Spring Boot Application

Before running your Spring Boot application, ensure that you have a Kafka broker running. You can set it up by downloading Kafka, starting Zookeeper, and then starting Kafka:

bin/zookeeper-server-start.sh config/zookeeper.properties
bin/kafka-server-start.sh config/server.properties

Once the broker is running, start your Spring Boot application. You can produce messages through a REST endpoint, or at startup:

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MessageStartupRunner implements CommandLineRunner {

    private final MessageProducer messageProducer;

    public MessageStartupRunner(MessageProducer messageProducer) {
        this.messageProducer = messageProducer;
    }

    @Override
    public void run(String... args) throws Exception {
        messageProducer.sendMessage("Hello Kafka!");
    }
}

Conclusion

Integrating Spring Boot with Apache Kafka enables you to build powerful, event-driven applications that can process streams of data efficiently. With Spring Kafka simplifying much of the boilerplate code associated with producers and consumers, developers can focus more on the business logic.

For further exploration and advanced patterns in Spring Boot and Kafka, be sure to check out the resources available at ITER Academy, where you can enhance your skills in building modern applications.

To learn more about ITER Academy, visit our website.

Scroll to Top