In this post we are going use Apache Kafka in Quarkus application in simple steps.
- An IDE
- JDK 11+ installed with JAVA_HOME configured appropriately
- Apache Maven 3.8.1+
- Apache Kafka Installation
- Conduktor Installation
We are going to create a new project structure with the right dependencies. Go to https://code.quarkus.io, enter your group id and artifact id. Then in the extension list, select:
SmallRye Reactive Messaging - Kafka Connector
RESTEasy Jackson
Our Application pom file should have these below dependencies:
Here we created a simple class and inject an Emitter, i.e., an object responsible for sending a message to a channel. This emitter is attached to the "words-out" channel (and so will send messages to Kafka).
Next We will Consume the message from the "words-in" channel, uppercase it and send it to the "uppercase" channel. Messages come from the broker.
We will use Reactive Messaging. When you use Reactive Messaging, you send messages to a channel and receive them from another channel. These channels are mapped to the underlying messaging technology by configuration. In our application, we must indicate that our reception and publication channels will use the words Kafka channel. In src/main/resources/application.properties, add the following content.
After having configured the broker location with kafka.bootstrap.servers, we configure our two channels: "words-in" (receiving the records) and "words-out" (publishing the records).
We use the mp.messaging.incoming.words-in prefix to configure the channel. The connector attribute indicates who’s responsible for this channel, here the Kafka connector. As we don't configure the key and value deserializers, here it will take String as Default deserializers.
To configure the outbound movies-out channel, we use the mp.messaging.outgoing.words-out prefix. In addition to indicate who’s responsible for that channel, As we don't configure the key and value serializers, here it will take String as Default serializers.
Next Start Zookeeper : zookeeper-server-start.bat C:\kafka_2.12-3.3.1\config\zookeeper.properties
Start Kafka: kafka-server-start.bat C:\kafka_2.12-3.3.1\config\server.propertiesNext Start the Quarkus Application using command: ./mvnw compile quarkus:dev
Quarkus Application will start and it will get connect to kafka broker running at localhost:9092
Now we will be able to see topic "words" should be created
C:\kafka_2.12-3.3.1\bin\windows>kafka-topics.bat --list --bootstrap-server localhost:9092
__consumer_offsets
test
words
Now Start the kafka consumer console using command : kafka-console-consumer.bat --bootstrap-server localhost:9092 --from-beginning --topic words
The same can be verified using Conduktor Dashboard
Summary:
GitHub Link : rajivksingh13/teachlea
Please feel free to provide your valuable comments, Thanks.
0 Comments