Springboot kafka consumer with code example:
Applications that require to read messages from kafka needs to use a kafkaConsumer and receive messages from the subscribed topics.Reading messages from kafka is different than reading from other messaging systems.In kafka there will be consumer groups which internally has multiple consumers. For the sake of this example , we will have one consumer group and one consumer. This tutorial shows how easy it is to implement Springboot kafka consumer.
The dependencies required for the Springboot kafka consumer will be specified in pom.xml for a sprinboot project with maven.
KafkaConsumerConfig class is responsible to specify kafka consumer specifics like :
- Kafka broker names: e.g localhost:9092
- Consumer Group id
- Topic name : The kafka topic from which your application needs to read data from.
- Serialization : Type of Deserialization
KafkaListenerContainerFactory is responsible to create the listener container.@EnableKafka enables detection of
KafkaListener annotations on any spring managed bean in the container.
@KafkaListener annotation marks the target method as a kafka listener of specified topics.The method readPayload has this annotation which means that Springboot kafka consumer will start reading messages from the topic ‘first_topic’.
Start the application by running the main class. During the application startup , springboot connects to the kafka broker and it starts listening to the topic.
Let me post a message to the kafka producer:
The application will immediately read the message and writes it to the console:
You can download this repo from github.
Read about Rest implementation using springboot here.