1.2. Apicurio Registry 中的 schema 和 API 工件
存储在 Apicurio Registry 中的项目,如事件模式和 API 设计,称为 registry 工件。下面显示了一个简单共享价格应用程序的 JSON 格式的 Apache Avro 模式工件示例:
Avro 模式示例
当将 schema 或 API 设计添加到 Apicurio Registry 中的工件时,客户端应用程序可以使用该 schema 或 API 设计来验证客户端消息在运行时符合正确的数据结构。
模式和 API 组
工件组是 schema 或 API 工件的可选命名集合。每个组都包含一组逻辑相关的模式或 API 设计,通常由单个实体管理,这些实体属于特定的应用程序或组织。
在添加 schema 和 API 设计时,您可以创建可选工件组,以便在 Apicurio Registry 中组织它们。例如,您可以创建组来匹配 development 和 production 的应用程序环境,或 sales 和 engineering 机构。
schema 和 API 组可以包含多个工件类型。例如,您可能具有 Protobuf、Avro、JSON Schema、OpenAPI 或 AsyncAPI 工件。
您可以使用 Apicurio Registry web 控制台、REST API、命令行、Maven 插件或 Java 客户端应用程序创建 schema 和 API 工件和组。以下简单示例演示了使用 Core Registry REST API:
curl -X POST -H "Content-type: application/json; artifactType=AVRO" \
-H "X-Registry-ArtifactId: share-price" \
--data '{"type":"record","name":"price","namespace":"com.example", \
"fields":[{"name":"symbol","type":"string"},{"name":"price","type":"string"}]}' \
https://my-registry.example.com/apis/registry/v2/groups/my-group/artifacts
$ curl -X POST -H "Content-type: application/json; artifactType=AVRO" \
-H "X-Registry-ArtifactId: share-price" \
--data '{"type":"record","name":"price","namespace":"com.example", \
"fields":[{"name":"symbol","type":"string"},{"name":"price","type":"string"}]}' \
https://my-registry.example.com/apis/registry/v2/groups/my-group/artifacts
本例创建一个名为 my-group 的工件组,并添加一个带有 share-price 的工件 ID 的 Avro 模式。
在使用 Apicurio Registry web 控制台时指定组是可选的,并自动创建一个 default 组。使用 REST API 或 Maven 插件时,如果您不想创建唯一组,请在 API 路径中指定 默认组。
对其他模式和 API 的引用
一些 Apicurio Registry 工件类型可以包含从一个 工件文件到另一个工件的工件引用。您可以通过定义可重复使用的模式或 API 组件来创建效率,然后从多个位置引用它们。例如,您可以使用 $ref 语句在 JSON Schema 或 OpenAPI 中指定引用,或使用 导入 声明在 Google Protobuf 中指定引用,或使用嵌套命名空间在 Apache Avro 中指定引用。
以下示例显示了一个名为 TradeKey 的简单 Avro 模式,其中包含使用嵌套命名空间对另一个名为 Exchange 的 schema 的引用:
带有嵌套交换模式的 Tradekey 模式
Exchange 模式
工件引用存储在 Apicurio Registry 中,作为工件元数据的集合,这些元数据从特定于工件类型的引用映射到内部 Apicurio Registry 引用。Apicurio Registry 中的每个工件引用都包含以下内容:
- 组 ID
- 工件 ID
- 工件版本
- 工件引用名称
您可以使用 Apicurio Registry core REST API、Maven 插件和 Java serializers/deserializers (SerDes)管理工件引用。Apicurio Registry 存储工件引用以及工件内容。Apicurio Registry 还维护所有工件引用的集合,以便您可以搜索或列出特定工件的所有引用。
支持的工件类型
Apicurio Registry 目前仅支持以下工件类型引用:
- Avro
- protobuf
- JSON 架构
- OpenAPI
- AsyncAPI