4.2. 使用 Registry REST API 命令管理 schema 和 API 工件版本
如果您没有在使用 v2 REST API 添加 schema 和 API 工件到 Apicurio Registry 时指定工件版本,Apicurio Registry 会自动生成一个。创建新工件时的默认版本是 1。
Apicurio Registry 还支持自定义版本,您可以在其中使用 X-Registry-Version HTTP 请求标头作为字符串来指定版本。指定自定义版本值将覆盖创建或更新工件时通常分配的默认版本。然后,在执行需要版本的 REST API 操作时,您可以使用此版本值。
本节演示了一个简单的基于 curl 的示例,它使用了 registry v2 核心 REST API 在 registry 中添加并检索自定义 Apache Avro schema 版本。在使用 REST API 添加或更新工件或添加工件版本时,您可以指定自定义版本。
前提条件
- 必须在自己的环境中安装并运行 Apicurio Registry。
流程
使用
/groups/{group}/artifacts操作在注册表中添加工件版本。以下示例curl命令为共享价格应用程序添加简单的工件:$ curl -X POST -H "Content-type: application/json; artifactType=AVRO" \ -H "X-Registry-ArtifactId: my-share-price" -H "X-Registry-Version: 1.1.1" \1 --data '{"type":"record","name":" p","namespace":"com.example", \ "fields":[{"name":"symbol","type":"string"},{"name":"price","type":"string"}]}' \2 http://MY-REGISTRY-HOST/apis/registry/v2/groups/my-group/artifacts3 验证响应是否包含预期的 JSON 正文,以确认是否添加了自定义工件版本。例如:
{"createdBy":"","createdOn":"2021-04-16T10:51:43+0000","modifiedBy":"", "modifiedOn":"2021-04-16T10:51:43+0000","id":"my-share-price","version":"1.1.1",1 "type":"AVRO","globalId":3,"state":"ENABLED","groupId":"my-group","contentId":3}2 使用 API 路径中的工件 ID 和版本从 registry 检索工件内容。在这个示例中,指定的 ID 是
my-share-price,版本是1.1.1:$ curl http://MY-REGISTRY-URL/apis/registry/v2/groups/my-group/artifacts/my-share-price/versions/1.1.1 \ {"type":"record","name":"price","namespace":"com.example", \ "fields":[{"name":"symbol","type":"string"},{"name":"price","type":"string"}]}