第 9 章 Validating Kafka messages using serializers/deserializers in Java clients
Apicurio Registry provides client serializers/deserializers (SerDes) for Kafka producer and consumer applications written in Java. Kafka producer applications use serializers to encode messages that conform to a specific event schema. Kafka consumer applications use deserializers to validate that messages have been serialized using the correct schema, based on a specific schema ID. This ensures consistent schema use and helps to prevent data errors at runtime.
This chapter explains how to use Kafka client SerDes in your producer and consumer client applications:
- 第 9.1 节 “Kafka client applications and Apicurio Registry”
- 第 9.2 节 “Strategies to look up a schema in Apicurio Registry”
- 第 9.3 节 “Registering a schema in Apicurio Registry”
- 第 9.4 节 “Using a schema from a Kafka consumer client”
- 第 9.5 节 “Using a schema from a Kafka producer client”
- 第 9.6 节 “Using a schema from a Kafka Streams application”
Prerequisites
- You have read 第 1 章 Introduction to Apicurio Registry.
- You have installed Apicurio Registry.
- You have created Kafka producer and consumer client applications.
9.1. Kafka client applications and Apicurio Registry 复制链接链接已复制到粘贴板!
Apicurio Registry decouples schema management from client application configuration. You can enable a Java client application to use a schema from Apicurio Registry by specifying its URL in your client code.
You can store the schemas in Apicurio Registry to serialize and deserialize messages, which are referenced from your client applications to ensure that the messages that they send and receive are compatible with those schemas. Kafka client applications can push or pull their schemas from Apicurio Registry at runtime.
Schemas can evolve, so you can define rules in Apicurio Registry, for example, to ensure that schema changes are valid and do not break previous versions used by applications. Apicurio Registry checks for compatibility by comparing a modified schema with previous schema versions.
Apicurio Registry schema technologies
Apicurio Registry provides schema registry support for schema technologies such as:
- Avro
- Protobuf
- JSON Schema
These schema technologies can be used by client applications through the Kafka client serializer/deserializer (SerDes) services provided by Apicurio Registry. The maturity and usage of the SerDes classes provided by Apicurio Registry might vary. The sections that follow provide more details about each schema type.
Producer schema configuration
A producer client application uses a serializer to put the messages that it sends to a specific broker topic into the correct data format.
To enable a producer to use Apicurio Registry for serialization:
- Define and register your schema with Apicurio Registry (if it does not already exist).
Configure your producer client code with the following:
- URL of Apicurio Registry
- Apicurio Registry serializer to use with messages
- Strategy to map the Kafka message to a schema artifact in Apicurio Registry
- Strategy to look up or register the schema used for serialization in Apicurio Registry
After registering your schema, when you start Kafka and Apicurio Registry, you can access the schema to format messages sent to the Kafka broker topic by the producer. Alternatively, depending on configuration, the producer can automatically register the schema on first use.
If a schema already exists, you can create a new version using the registry REST API based on compatibility rules defined in Apicurio Registry. Versions are used for compatibility checking as a schema evolves. A group ID, artifact ID, and version represents a unique tuple that identifies a schema.
Consumer schema configuration
A consumer client application uses a deserializer to get the messages that it consumes from a specific broker topic into the correct data format.
To enable a consumer to use Apicurio Registry for deserialization:
- Define and register your schema with Apicurio Registry (if it does not already exist)
Configure the consumer client code with the following:
- URL of Apicurio Registry
- Apicurio Registry deserializer to use with the messages
- Input data stream for deserialization
Retrieve schemas using a content ID
By default, the schema is retrieved from Apicurio Registry by the deserializer using a content ID (which is an ID unique to the content of an artifact version, but not unique to the version itself), which is specified in the message being consumed. The schema content ID can be located in the message headers or in the message payload, depending on the configuration of the producer application. By default, the content ID will be located in the message body.
When locating the content ID in the message payload, the format of the data begins with a magic byte, used as a signal to consumers, followed by the content ID, and the message data as normal. For example:
# ...
[MAGIC_BYTE]
[CONTENT_ID]
[MESSAGE DATA]
Then when you start Kafka and Apicurio Registry, you can access the schema to format messages received from the Kafka broker topic.
Retrieve schemas using a global ID
Alternatively, you can configure to retrieve schemas from Apicurio Registry based on the global ID, which is the unique ID of the artifact version. The same options are available to you when using global ID instead of contentID. You can either send the global ID in the message headers or the message body (default).
When locating the global ID in the message payload, the format of the data begins with a magic byte, used as a signal to consumers, followed by the global ID, and the message data as normal. For example:
# ...
[MAGIC_BYTE]
[GLOBAL_ID]
[MESSAGE DATA]