Integrating Apache Kafka with Micronaut: A Step-by-Step Guide


Micronaut is a popular framework for building lightweight and efficient microservices in Java, Kotlin, and Groovy. When you need to integrate Kafka into your Micronaut application for event-driven processing, it’s essential to set up the integration correctly. In this guide, we’ll walk you through the process of integrating Kafka with a Micronaut application.


  1. Micronaut application (if not, you can quickly create one using the Micronaut CLI).
  2. Apache Kafka installed and running locally or on a remote server.
  3. Basic knowledge of Micronaut and Kafka.

Step 1: Add Kafka Dependency

In your Micronaut application’s build.gradle (for Gradle) or build.gradle.kts (for Kotlin DSL), add the Micronaut Kafka dependency:


Sync your project to fetch the new dependencies.

Step 2: Configure Kafka Connection In your application.yml (or file, configure the connection to your Kafka broker:

    servers: localhost:9092

This configuration tells your Micronaut application to connect to a Kafka broker running on localhost at port 9092. Adjust this based on your Kafka setup.

Step 3: Create a Kafka Producer

To produce messages to a Kafka topic, you’ll need a Kafka producer. In your Micronaut service or controller, inject a KafkaProducer and use it to send messages to a Kafka topic:

package com.example.event

import com.example.model.Student
import io.micronaut.configuration.kafka.annotation.KafkaClient
import io.micronaut.configuration.kafka.annotation.Topic

interface KafkaProducer {
    fun send(student: Student?)

Step 4: Create a Kafka Consumer

To consume messages from a Kafka topic, you’ll need a Kafka consumer. Create a Kafka listener to process incoming messages:

package com.example.event

import com.example.model.Student
import com.example.util.logger
import io.micronaut.configuration.kafka.annotation.KafkaListener
import io.micronaut.configuration.kafka.annotation.OffsetReset
import io.micronaut.configuration.kafka.annotation.Topic

@KafkaListener(offsetReset = OffsetReset.EARLIEST)
class KafkaConsumer {
    fun receive(
        student: Student?
    ) {
        logger().error("Successfully received student" + student)

Step 5: Send Messages

You can now use your Kafka producer to send messages to your Kafka topic from any part of your Micronaut application:

    suspend fun kafkaStudentProcessing(@Body request: Student): String? {
        return request.toString()

PR changes:


Related Post