Connecting Kafka With Micronaut

Hacker hands using laptop computer to code
Reading Time: 2 minutes

Micronaut is a simple JAVA framework, used for web-based applications and microservices. It consumes less memory and shows compile time reflections unlike spring boot applications, where it exhibits run time reflections. Supports multiple languages such as Java, Groovy, and Kotlin. It is most likely similar to the spring framework.
There are some previous blogs that you can refer to, for a better understanding of Micronaut:

In this blog, we will try to make a simple Micronauts application with Kafka consumers and producers

1) Generating a Micronauts project via micronaut.io

The first thing that we would require is the Micronaut project, we can generate it via Micronauts CLI with the help of a bunch of commands or we can simply build a project via the micronaut.io site, if you are familiar with the spring boot then you have come across springboot.io which helps in building spring boot projects, similar https://micronaut.io/ will help in making the Micronauts projects as per your required configurations.
In this example, we will build a maven Java project.
Micronaut supports both Kafka producer and consumer configurations.

2) Generating a Micronaut project via CLI

The other way of generating a Micronaut project is via CLI, you simply need to run this command it will configure all the most required dependencies that are used by Kafka while running inside Micronaut projects.

$ mn create-app my-kafka-app --features kafka

Including Kafka dependencies

To run the Micronaut application with Kafka we initially have to import a few dependencies that are used by Kafka inside pom.xml, without it our code will give errors.
micronaut-http-server-netty is optional in case you need to hit any endpoint then you can use that dependency as well, but micronaut-kafka dependency is a must.

<dependency>
    <groupId>io.micronaut.configuration</groupId>
    <artifactId>micronaut-kafka</artifactId>
</dependency>
<dependency>
    <groupId>io.micronaut</groupId>
    <artifactId>micronaut-http-server-netty</artifactId>
</dependency>

Configuring Kafka

Before starting the project Kafka needs to be configured inside application.yml in your project.

kafka:
    bootstrap:
        servers: localhost:9092

You can even change the port number, 9092 is the default port for running Kafka

Kafka Producer

It will send the data in form of messages to the given topic, it is much simpler than consumer configurations, and Micronauts allows some simple annotations as well to do that easily.

@KafkaClient
public interface Messages {

    //kafka topic is send
void sendMessage(@KafkaKey String time, String message);

void sendMessage(@Topic String topic, @KafkaKey String time, String message);
}

Usage of each annotation:

  • @KafkaClient highlights the Kafka client
  • @Topic annotation topic on which the message has to be published
  • @Kafkakey annotation key of a message.

Kafka Consumer

It will consume the data in the form of messages from the givrn topic on which producer has published the message.

@KafkaListener(offsetReset = OffsetReset.EARLIEST)
public class MessageConsumer {

    @Topic("send")
    public void receive(@KafkaKey String time, String message) {
        System.out.println("Received Message   - " + time + " and " + message);
    }
}

Usage of each annotation:

  • @KafkaListener: It shows that this is a consumer and once the message is produced on the topic this will start listening from that particular topic.

References:

Leave a Reply