Integrating Spring Boot with Apache Kafka for Streaming Data Processing

Apache Kafka is a leading platform for building real-time data pipelines and streaming applications. It is designed for high-throughput, fault-tolerant messaging, making it an ideal choice for use cases that require the processing of large volumes of data in real-time. In this post, we will explore how to integrate Apache Kafka with Spring Boot to create a robust data streaming application.

What is Apache Kafka?

Apache Kafka is a distributed event streaming platform that allows you to publish, subscribe to, store, and process streams of records. Key features of Kafka include:

  • Durability: Kafka retains messages for a configurable amount of time, ensuring that no data is lost.
  • Scalability: Kafka can handle a large amount of data by distributing it across multiple servers.
  • Consumer Groups: Multiple consumers can read from the same topic, balancing the load.

Setting Up Apache Kafka with Spring Boot

We will create a new Spring Boot application that integrates with Kafka to send and receive messages.

1. Setting Up Kafka Server

Before integrating Kafka with Spring Boot, ensure you have Kafka set up on your local machine. Follow the official Kafka quick start guide to create a Kafka server instance.

2. Create a Spring Boot Project

Create a Spring Boot project using Spring Initializr with the following dependencies:

  • Spring Web
  • Spring for Apache Kafka

3. Adding Dependencies

In your pom.xml, add the Spring Kafka dependency:

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

4. Configuring Kafka Properties

In your application.properties, configure the Kafka properties:

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

Creating a Kafka Producer

Next, create a service class to handle sending messages to Kafka:

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

@Service
public class MessageProducer {
    private final KafkaTemplate<String, String> kafkaTemplate;

    @Autowired
    public MessageProducer(KafkaTemplate<String, String> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

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

Creating a Kafka Consumer

Create a consumer class to listen to messages from a Kafka topic:

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

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

Creating a REST Controller to Send Messages

To send messages, you can create a REST controller that interacts with the producer:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/messages")
public class MessageController {

    @Autowired
    private MessageProducer messageProducer;

    @PostMapping
    public void sendMessage(@RequestParam String topic, @RequestParam String message) {
        messageProducer.sendMessage(topic, message);
    }
}

Running Your Application

Run your Spring Boot application and test the functionality using Postman or curl:

curl -X POST "http://localhost:8080/api/messages?topic=my-topic&message=Hello+Kafka"

Conclusion

Integrating Apache Kafka with Spring Boot allows you to build scalable, reactive applications capable of handling real-time data. With the ease of configuration and the powerful messaging capabilities of Kafka, you can enhance your application’s functionality significantly.

For more insights into advanced usage scenarios with Spring Boot and event streaming architectures, check out the wide array of resources available at ITER Academy to improve your skills.

To learn more about ITER Academy, visit our website.

Scroll to Top