DEV Community

Code Green for CodeGreen

Posted on

Using Kafka in Spring Boot Application

Kafka Consumer and Producer in Spring Boot

1. Dependency Required

You need to include the following dependency in your pom.xml or build.gradle:

<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
    <version>2.7.0</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Also See: How to Setup Kafka Locally on Windows

2. Kafka Configuration in application.properties

You need to configure Kafka for both Producer and Consumer in your application.properties:

# Kafka Producer configuration
spring.kafka.bootstrap-servers=localhost:9092
# Kafka Consumer configuration
spring.kafka.consumer.group-id=my-group
Enter fullscreen mode Exit fullscreen mode

3. Configuring Kafka Producer with @KafkaTemplate

To configure a Kafka producer, you can use the @KafkaTemplate annotation. You can serialize the message to JSON format using the JsonSerializer:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.serializer.JsonSerializer;
import org.springframework.stereotype.Component;

@Component
public class MyKafkaProducer {

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    public void sendMessage(String topic, Object message) {
        kafkaTemplate.send(topic, message);
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Configuring Kafka Consumer with @KafkaListener

To configure a Kafka consumer, you can use the @KafkaListener annotation. You can deserialize the message from JSON format using the JsonDeserializer:

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

@Component
public class MyKafkaConsumer {

    @KafkaListener(topics = "my-topic", groupId = "my-group")
    public void listen(String message) {
        System.out.println("Received Message: " + message);
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Spring Boot makes it easy to implement Kafka consumer and producer using the Spring Kafka library. By using annotations like @KafkaTemplate and @KafkaListener, developers can quickly set up Kafka communication in their applications.

Top comments (0)