Spring Boot + Apache Kafka Tutorial - #9 - Create Kafka Consumer


Welcome to Spring Boot + Apache Kafka Tutorial series. In this lecture, we will create Kafka Consumer and will have an end-to-end demo.

Lecture - #9 - Create Kafka Consumer

Transcript:

Hi. Welcome back. In this lecture, we'll create a Kafka Consumer to consumer message from the Kafka topic. Well, if you can see our Kafka architecture so far we have created a Kafka Producer which will send a message to the Kafka topic. Now, in this lecture, we'll create a Kafka Consumer which will consume a message from that particular Kafka topic. Well, let's head over to the IntelliJ idea, and let's write the Java code to create a Kafka consumer. Let's go to Kafka package right-click on it new and then choose Java class and let's give class name as KafkaConsumer all right hit enter and let's go ahead and let's annotate class with @Service annotation to make this class as Spring bean. So within this class, we're going to create a subscriber method that will subscribe to that topic. So we are going to basically use KafkaListener annotation from Spring Kafka Library to listen or to subscribe to the topic. So let's first create the method and then when you use annotation to subscribe to the topic. So let's say public and return type of the method is void and let's say consume is the method name and we want to basically consume the type string, right message type string. So let's say string message and then let's annotate this method with @KafkaListener annotation and make sure that you choose KafkaListener annotation from springframework.kafka.annotation Okay. So Spring basically provides this annotation to subscribe, you know, to the topic, well, let's call this annotation over here and this annotation has a lot of properties. So we're going to use topics attribute to provide a topic. So let's say the topic name is javaguides. Now, this consume() method, it acts as a subscriber and it subscribed to the javaguides Kafka topic. Well, whenever Kafka's Producer will send a message to the Kafka topic then this subscriber method will receive that message from this javaguides Kafka topic. Well, there can be any number of subscribers. Okay. In our example, to keep it simple, we have only one consume() subscriber method to subscribe to the Kafka javaguides topic. Okay, great. We have provided a consumer group id right so we need to also provide a consumer group I.D. to these annotations. We needed to do that we're going to call group ID and then we have given a consumer group ID as myGroup, right. So if we can open an application.properties file, we have given a consumer group ID as myGroup right? So this myGroup id, we have to provide in that annotation. All right, perfect. Now what we'll do is we're going to have a Logger over here and then we log the messages. So let's say private static final and then logger from slf4j from the library. And then this should be a logger and then LoggerFactory and getLogger and then the class All right. Now, let's use this logger to log the messages. Okay? Logger dot info() and then let's use String.format() method and let's say message received. And then let's put the placeholder %s and then pass the message Okay. Well, whenever Kafka Producer will send a message to the topic, then this consumer will consume that message from the Kafka topic and it will print to the console by using this log statement All right. Pretty simple. Now let's go and let's run our Spring Boot application and let's verify how this Kafka Consumer will work. So let me stop the existing server and let me start the server again. All right. And here you can see message received. So right now in a topic, we have these number of messages. Right. Now we have written a Kafka Consumer so we can able to see that all these messages are consumed by these Kafka Consumer and the same messages were printed in the console by using this log statement message received followed by message. Now let's go ahead and let's use rest API to send a message to the topic using Kafka producer and we'll see how this Kafka consumer will consume that message. Well, let me go to your browser and let me send a message using this rest API. Let's say hello Kafka hit enter Well, let's go to IntelliJ idea and let's see the logs and there we go message received Hello, Kafka. Okay, so let me send one more message let's say hello spring boot Kafka and here you can see the log statement. message received Hello spring boot Kafka. It means that the Kafka consumer that we have written is working as expected. This is how basically we write a Kafka producer and consumer to send a message to the topic and consume that message from the Kafka topic.

Comments