ANGADJAVA Code Samples Bytes Example of Spring Boot with Apache Kafka

Example of Spring Boot with Apache Kafka

0 Comments 6:48 pm

architecture, church, building-3287063.jpg

Introduction: In this coding example, we will explore how to use Apache Kafka with a Spring Boot application. We will implement a simple messaging system where a producer sends messages to a Kafka topic, and a consumer listens for and processes these messages in real-time.

Step 1: Setting up Apache Kafka:

Step 2: Creating a Spring Boot Project:

  • Guide the readers on creating a new Spring Boot project using their preferred IDE or the Spring Initializr.
  • Mention the necessary dependencies to include, such as Spring Kafka and Spring Web.

Step 3: Configuring Kafka Properties:

Step 4: Implementing a Kafka Producer:

  • Create a KafkaProducer class using the @Service annotation.
  • Autowire the KafkaTemplate provided by Spring Kafka.
  • Implement a method to send messages to a Kafka topic.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class KafkaProducer {
    private final KafkaTemplate<String, String> kafkaTemplate;
    private static final String TOPIC = "myTopic";

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

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

Step 5: Implementing a Kafka Consumer:

  • Create a KafkaConsumer class using the @Service annotation.
  • Implement the @KafkaListener annotation to specify the topic and consumer group.
  • Define a method to process the received messages.
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class KafkaConsumer {
    private static final String TOPIC = "myTopic";

    @KafkaListener(topics = TOPIC, groupId = "myGroup")
    public void processMessage(String message) {
        System.out.println("Received message: " + message);
        // Process the received message
    }
}

Step 6: Creating a REST Controller:

  • Create a MessageController class and annotate it with @RestController.
  • Inject the KafkaProducer into the controller.
  • Implement an API endpoint to send messages to the Kafka topic.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {
    private final KafkaProducer kafkaProducer;

    @Autowired
    public MessageController(KafkaProducer kafkaProducer) {
        this.kafkaProducer = kafkaProducer;
    }

    @PostMapping("/messages")
    public void sendMessage(@RequestBody String message) {
        kafkaProducer.sendMessage(message);
    }
}

Conclusion: By integrating Apache Kafka with Spring Boot, developers can build real-time messaging systems efficiently. In this example, we demonstrated how to set up Apache Kafka, create a Spring Boot project, configure Kafka properties, and implement a Kafka producer and consumer using Spring Kafka annotations. With this foundation, you can now explore and expand upon the concepts to build more sophisticated applications that leverage Apache Kafka’s power for distributed event streaming.

Leave a Reply

Your email address will not be published. Required fields are marked *