领先一步
VMware 提供培训和认证,助力您加速发展。
了解更多这篇博客介绍了 Spring Cloud Stream 4.0.x 版本中包含的 Schema Registry 支持的最新情况。
许多企业使用 schema registry 来处理 schema 演进用例,例如 Confluent Schema Registry。从 Spring Cloud Stream 1.1.x
版本到 3.0.0
版本,我们提供了 schema registry 服务器和基于 AVRO 的 schema registry 客户端转换器,它们可以从 schema registry 服务器协调 schema。Spring Cloud Stream 3.0.0 版本将 Schema Registry 组件移到了顶级 spring-cloud 项目中,并且 Spring Cloud Stream 将它们包含在其 BOM(Bill of Materials)中供最终用户应用程序使用。然而,这种方式扩展性不佳,在 3.2 版本中,我们决定不再将它们作为 Spring Cloud Stream BOM 的一部分。
Schema registry 在流式应用程序中的用例非常引人注目,社区一直渴望 Spring Cloud Stream 核心提供一个解决方案。我们很高兴通知您,schema registry 组件已回归 Spring Cloud Stream 的 4.0.0 版本。对于这些 schema registry 组件来说,这是一次完整的回归之旅。下面,我们将重新介绍这些组件并简要概述它们的功能。
Spring Cloud Stream 4.0.x
版本附带了一个独立的 Schema Registry 服务器,该服务器专为 Spring Cloud Stream 所解决的用例而定制。这些用例包括跨多个 schema 版本的 schema 演进。这些用例的核心要点是生产者应用程序使用旧的 schema 版本发布,而消费者则以向后兼容的方式处理所有 schema 版本。这些工作流程包括 schema 验证,并将经过验证的 schema 作为契约和消息转换器进行数据序列化的基础。我们不建议在 Spring Cloud Stream 之外使用此 Schema Registry 服务器。默认情况下,此 Schema Registry 服务器使用内存中的 H2 数据库作为后端存储。但是,您可以使用 Postgres 或 MySQL 作为后端数据库。Spring Cloud Stream 将此 Schema Registry 服务器作为功能齐全的 Spring Boot 应用程序提供。因此,您可以直接从 Maven Central 下载它(当 Spring Cloud Stream 4.0.0
正式发布时)。如果您想尝试快照或里程碑版本,可以从相关的 Artifactory 仓库下载。以下是 4.0.0-SNAPSHOT
版本的 Maven 坐标。如果您不想使用快照进行测试,也可以使用 4.0.0-M5
或 4.0.0-RC1
。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-schema-registry-server</artifactId>
<version>4.0.0-SNAPSHOT</version>
</dependency>
假设您不想使用独立的 Schema Registry 服务器,而是想使用自定义的。在这种情况下,您可以将服务器模块作为依赖项包含在您的应用程序中,并通过启用 @EnableSchemaRegistryServer
来创建自定义的 Spring Boot 应用程序。如果您有不支持的后端存储,则应使用此策略。
Spring Cloud Stream 4.0.0
还提供了一个 Schema Registry 客户端模块,该模块提供基于 AVRO 的消息转换器。这些转换器感知 Schema Registry,并与 Schema Registry 服务器通信以协调 schema,同时进行有效载荷的序列化转换。应用程序需要使用注解 @EnableSchemaRegistryClient
来通过 SchemaRegistryClient
bean 感知 schema registry 服务器。Spring Boot 会自动配置感知 schema registry 的基于 AVRO 的消息转换器。
以下是 4.0.0-SNAPSHOT
版本的 schema registry 客户端模块的 Maven 坐标。您也可以使用 4.0.0-M5
或 4.0.0-RC1
。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-schema-registry-client</artifactId>
<version>4.0.0-SNAPSHOT</version>
</dependency>
Confluent Schema Registry 是一个流行的商业解决方案,用于 Kafka 应用程序的 schema 演进。上面提到的 Spring Cloud Stream AVRO 消息转换器可以与 Confluent Schema Registry 配合使用。为此,应用程序需要覆盖 SchemaRegistryClient
bean 并提供 ConfluentSchemaRegistryClient
的实现。
我们提供了一系列示例应用程序,演示了本博客中描述的所有概念,请点击此处查看。更多详细信息请参阅 README。
此处是 Schema Registry 参考文档。
Spring Cloud Stream 4.0.0
重新引入了之前版本 Spring Cloud Stream 中的 Schema Registry 组件。本博客探讨了重新引入此支持的动机和概述。Spring Cloud Stream 中提供的支持可能只涵盖部分 schema 演进用例。但是,所提供的 schema registry 组件可以在传输无关的价值增值方面支持 Spring Cloud Stream 应用程序的许多 schema 演进用例。