第 8 章 在 Java 客户端中配置 Kafka 序列化器/反序列化器
本章详细介绍了如何在生成者和消费者 Java 客户端应用程序中配置 Kafka SerDe:
先决条件
8.1. 客户端应用程序中的 Service Registry 序列化器/反序列化配置 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
您可以使用本节中显示的示例常量配置特定的客户端序列化器/反序列化器(SerDe)服务和模式查找策略。另外,您可以在文件或实例中配置对应的 Service Registry 应用程序属性。
以下小节演示了常用的 SerDe 常量和配置选项的示例。
配置 SerDe 服务
public class SerdeConfig {
   public static final String REGISTRY_URL = "apicurio.registry.url"; 
   public static final String ID_HANDLER = "apicurio.registry.id-handler"; 
   public static final String ENABLE_CONFLUENT_ID_HANDLER = "apicurio.registry.as-confluent"; 
public class SerdeConfig {
   public static final String REGISTRY_URL = "apicurio.registry.url"; 
   public static final String ID_HANDLER = "apicurio.registry.id-handler"; 
   public static final String ENABLE_CONFLUENT_ID_HANDLER = "apicurio.registry.as-confluent"; 
- Service Registry 所需的 URL。
 - 
						扩展 ID 处理以支持其他 ID 格式,并使其与 Service Registry SerDe 服务兼容。例如,将默认 ID 格式从 
Long改为Integer支持 Confluent ID 格式。 - 
						简化了对一致性 ID 的处理。如果设置为 
true,则使用Integer用于全局 ID 查找。设置不应与ID_HANDLER选项一起使用。 
配置 SerDe 查找策略
public class SerdeConfig {
   public static final String ARTIFACT_RESOLVER_STRATEGY = "apicurio.registry.artifact-resolver-strategy"; 
   public static final String SCHEMA_RESOLVER = "apicurio.registry.schema-resolver"; 
...
public class SerdeConfig {
   public static final String ARTIFACT_RESOLVER_STRATEGY = "apicurio.registry.artifact-resolver-strategy"; 
   public static final String SCHEMA_RESOLVER = "apicurio.registry.schema-resolver"; 
...
- 实施工件解析器策略的 Java 类,并在 Kafka SerDe 和工件 ID 之间映射。默认为主题 ID 策略。这仅由 serializer 类使用。
 - 
						实施架构解析器的 Java 类。默认为 
DefaultSchemaResolver。这由序列化器和反序列化类使用。 
配置 Kafka 转换器
public class SerdeBasedConverter<S, T> extends SchemaResolverConfigurer<S, T> implements Converter, Closeable {
   public static final String REGISTRY_CONVERTER_SERIALIZER_PARAM = "apicurio.registry.converter.serializer"; 
   public static final String REGISTRY_CONVERTER_DESERIALIZER_PARAM = "apicurio.registry.converter.deserializer"; 
public class SerdeBasedConverter<S, T> extends SchemaResolverConfigurer<S, T> implements Converter, Closeable {
   public static final String REGISTRY_CONVERTER_SERIALIZER_PARAM = "apicurio.registry.converter.serializer"; 
   public static final String REGISTRY_CONVERTER_DESERIALIZER_PARAM = "apicurio.registry.converter.deserializer"; 
- 与 Service Registry Kafka 转换器搭配使用所需的序列化器。
 - 与 Service Registry Kafka 转换器搭配使用所需的反序列化器。
 
配置不同模式类型
有关如何为不同模式技术配置 SerDe 的详情,请参考以下内容: