This repository contains code examples for implementing a Kafka consumer and producer in a Spring Boot application.
The Kafka producer is responsible for publishing messages to Kafka topics. It uses the following configuration:
The KafkaProducerConfig
class is a configuration class that sets up the necessary configuration for the Kafka producer. It includes the following:
bootstrapServers
: This field is populated with the value of the propertyspring.kafka.bootstrap-servers
from the configuration properties file.producerConfig()
: This method returns aMap
of producer configurations, including the bootstrap servers, key serializer, and value serializer.
The provided code is a configuration class for a Kafka producer in a Spring Boot application. Let's break it down:
-
Import Statements:
org.apache.kafka.clients.producer.ProducerConfig
: This class provides constants for configuring the producer.org.apache.kafka.common.serialization.StringSerializer
: This class serializes objects of typeString
to bytes.
-
Annotations:
@Configuration
: This annotation indicates that the class is a configuration class and should be processed by the Spring container.
-
Field:
@Value("${spring.kafka.bootstrap-servers}")
: This annotation injects the value of the propertyspring.kafka.bootstrap-servers
from the configuration properties file into thebootstrapServers
field.
-
Method:
producerConfig()
: This method returns aMap
of producer configurations.- Configuration properties are set in the
props
map using constant keys fromProducerConfig
. ProducerConfig.BOOTSTRAP_SERVERS_CONFIG
: Specifies the list of Kafka brokers to connect to. It is set to the value ofbootstrapServers
field.ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG
: Specifies the serializer class for the message key. In this case, it's set toStringSerializer
.ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG
: Specifies the serializer class for the message value. Also set toStringSerializer
.- The
props
map is returned to be used as Kafka producer configuration.
The Kafka consumer is responsible for subscribing to Kafka topics and processing incoming messages. It uses the following configuration:
The KafkaConsumerConfig
class is a configuration class that sets up the necessary configuration for the Kafka consumer. It includes the following:
bootstrapServers
: This field is populated with the value of the propertyspring.kafka.bootstrap-servers
from the configuration properties file.consumerConfig()
: This method returns aMap
of consumer configurations, including the bootstrap servers, group ID, key deserializer, and value deserializer.
The KafkaMessageListener
class is a Spring @Component
that listens to Kafka topics and processes incoming messages. It includes the following:
@KafkaListener
annotation: This annotation is used to specify the Kafka topic(s) to listen to and the method to handle the incoming messages.@Payload
annotation: This annotation is used to specify the payload of the Kafka message.@Headers
annotation: This annotation is used to specify any headers associated with the Kafka message.
To run the Kafka consumer and producer examples, follow these steps:
- Clone the repository:
git clone <repository_url>
- Configure the Kafka properties in the
application.properties
file.