9/29/2025

Using Schema Registry in Quarkus app

Using Schema Registry in Quarkus Application Development

Schema Registry is a critical component when working with Apache Kafka and Avro in Quarkus applications. It ensures that the data structure (schema) is consistent and compatible across producers and consumers. Here's a concise guide to integrating Schema Registry in a Quarkus application using a Student object.


1. Add Dependencies

Include the necessary dependencies in your pom.xml for Kafka, Avro, and Schema Registry support:

<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-smallrye-reactive-messaging-kafka</artifactId> </dependency> <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-apicurio-registry-avro</artifactId> </dependency>

2. Configure Application Properties

Set up the connection to your Schema Registry in the application.properties file:

# Kafka broker configuration kafka.bootstrap.servers=localhost:9092 # Schema Registry configuration mp.messaging.connector.smallrye-kafka.schema.registry.url=http://localhost:8081 # Avro serialization mp.messaging.outgoing.my-topic.value.serializer=io.apicurio.registry.utils.serde.AvroKafkaSerializer mp.messaging.incoming.my-topic.value.deserializer=io.apicurio.registry.utils.serde.AvroKafkaDeserializer

🔁 Replace http://localhost:8081 with the URL of your Schema Registry (e.g., Confluent or Apicurio).


3. Define Avro Schema

Create Avro schema files (e.g., student.avsc) and generate Java classes using the Avro Maven plugin:

Example: student.avsc

{ "namespace": "com.example.avro", "type": "record", "name": "Student", "fields": [ { "name": "name", "type": "string" }, { "name": "email", "type": "string" }, { "name": "grade", "type": "int" } ] }

Add the Avro Maven Plugin to pom.xml:

<plugin> <groupId>org.apache.avro</groupId> <artifactId>avro-maven-plugin</artifactId> <version>1.11.1</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>schema</goal> </goals> <configuration> <sourceDirectory>${project.basedir}/src/main/avro</sourceDirectory> <outputDirectory>${project.build.directory}/generated-sources/avro</outputDirectory> </configuration> </execution> </executions> </plugin>

Place your student.avsc file in src/main/avro.


4. Implement Kafka Producers and Consumers

Use Quarkus' reactive messaging to produce and consume messages with the Student Avro object.

✅ Producer Example:

import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.eclipse.microprofile.reactive.messaging.Channel; import org.eclipse.microprofile.reactive.messaging.Emitter; import com.example.avro.Student; @ApplicationScoped public class KafkaStudentProducer { @Inject @Channel("my-topic") Emitter<Student> emitter; public void send(Student student) { emitter.send(student); } }

✅ Consumer Example:

import org.eclipse.microprofile.reactive.messaging.Incoming; import com.example.avro.Student; public class KafkaStudentConsumer { @Incoming("my-topic") public void consume(Student student) { System.out.println("Received student: " + student); } }

Here, Student is the Java class generated from your Avro schema (student.avsc).


5. Test Schema Compatibility

Ensure your producer and consumer schemas are compatible. The Schema Registry (e.g., Apicurio or Confluent) will validate compatibility automatically at runtime.


✅ Summary

By following these steps, you can seamlessly integrate Schema Registry into your Quarkus application, enabling robust, versioned, and schema-compliant Kafka messaging with Avro.

Using the Student object example, you’ve seen:

  • How to configure Schema Registry

  • Define and generate Avro classes

  • Produce and consume Kafka messages with reactive messaging

  • Automatically validate schema compatibility at runtime


💡 Tip: If you're using Apicurio Registry, you can also explore features like artifact versioning, API-based registration, and schema evolution rules (BACKWARD, FORWARD, FULL compatibility).

Niciun comentariu:

Linux for DevOps (Beginners)

Linux for DevOps (Beginners) 1. Linux Fundamentals 1.1 What is Open Source? Open-source software has source code freely available for anyone...