4.3. 使用 Service Registry REST API 命令管理 schema 和 API 工件引用
Service Registry 工件类型,如 Apache Avro、Protobuf 和 JSON Schema 可以包含从一个 工件文件到另一个工件文件的工件引用。您可以通过定义可重复使用的模式和 API 工件来创建参与,然后从多个位置引用它们。
本节展示了一个简单的基于 curl 的示例,它使用 Core Registry API v2 添加和检索对 Service Registry 中简单 Avro 模式工件的工件引用。
本例首先创建一个名为 ItemId
的模式工件:
slirpId 模式
然后,本例创建一个名为 Item
的模式工件,其中包含对嵌套的 ItemId
工件的引用。
带有嵌套 ItemId 模式的项目模式
先决条件
- Service Registry 已在您的环境中安装并运行。
流程
添加您要使用
/groups/{group}/artifacts
操作创建嵌套工件引用的ItemId
模式工件:curl -X POST MY-REGISTRY-URL/apis/registry/v2/groups/my-group/artifacts \ -H "Content-Type: application/json; artifactType=AVRO" \ -H "X-Registry-ArtifactId: ItemId" \ -H "Authorization: Bearer $ACCESS_TOKEN" \ --data '{"namespace": "com.example.common", "type": "record", "name": "ItemId", "fields":[{"name":"id", "type":"int"}]}'
$ curl -X POST MY-REGISTRY-URL/apis/registry/v2/groups/my-group/artifacts \ -H "Content-Type: application/json; artifactType=AVRO" \ -H "X-Registry-ArtifactId: ItemId" \ -H "Authorization: Bearer $ACCESS_TOKEN" \ --data '{"namespace": "com.example.common", "type": "record", "name": "ItemId", "fields":[{"name":"id", "type":"int"}]}'
Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
这个示例添加了一个 Avro schema 工件,工件 ID 为
ItemId
。如果您没有指定唯一的工件 ID,Service Registry 将自动作为 UUID 生成一个。 -
MY-REGISTRY-URL
是部署 Service Registry 的主机名。例如:my-cluster-service-registry-myproject.example.com
。 -
本例指定 API 路径中的
my-group
组 ID。如果没有指定唯一组 ID,则必须在 API 路径中指定../groups/default
。
-
这个示例添加了一个 Avro schema 工件,工件 ID 为
验证响应是否包含预期的 JSON 正文,以确认是否已添加工件。例如:
{"name":"ItemId","createdBy":"","createdOn":"2022-04-14T10:50:09+0000","modifiedBy":"","modifiedOn":"2022-04-14T10:50:09+0000","id":"ItemId","version":"1","type":"AVRO","globalId":1,"state":"ENABLED","groupId":"my-group","contentId":1,"references":[]}
{"name":"ItemId","createdBy":"","createdOn":"2022-04-14T10:50:09+0000","modifiedBy":"","modifiedOn":"2022-04-14T10:50:09+0000","id":"ItemId","version":"1","type":"AVRO","globalId":1,"state":"ENABLED","groupId":"my-group","contentId":1,"references":[]}
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 添加
Item
模式工件,它包括使用/groups/{group}/artifacts
操作的到ItemId
模式的工作引用:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
对于工件引用,您必须指定
application/create.extended+json
的自定义内容类型,该类型扩展了application/json
内容类型。
-
对于工件引用,您必须指定
验证响应是否包含预期的 JSON 正文,以确认是否使用引用创建工件。例如:
{"name":"Item","createdBy":"","createdOn":"2022-04-14T11:52:15+0000","modifiedBy":"","modifiedOn":"2022-04-14T11:52:15+0000","id":"Item","version":"1","type":"AVRO","globalId":2,"state":"ENABLED","groupId":"my-group","contentId":2, "references":[{"artifactId":"ItemId","groupId":"my-group","name":"ItemId","version":"1"}] }
{"name":"Item","createdBy":"","createdOn":"2022-04-14T11:52:15+0000","modifiedBy":"","modifiedOn":"2022-04-14T11:52:15+0000","id":"Item","version":"1","type":"AVRO","globalId":2,"state":"ENABLED","groupId":"my-group","contentId":2, "references":[{"artifactId":"ItemId","groupId":"my-group","name":"ItemId","version":"1"}] }
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 通过指定包含引用的工件的全局 ID,从 Service Registry 检索工件引用。在本例中,指定的全局 ID 是
2
:curl -H "Authorization: Bearer $ACCESS_TOKEN" MY-REGISTRY-URL/apis/registry/v2/ids/globalIds/2/references
$ curl -H "Authorization: Bearer $ACCESS_TOKEN" MY-REGISTRY-URL/apis/registry/v2/ids/globalIds/2/references
Copy to Clipboard Copied! Toggle word wrap Toggle overflow 验证响应是否包含此工件引用的预期 JSON 正文。例如:
[{"groupId":"my-group","artifactId":"ItemId","version":"1","name":"com.example.common.ItemId"}]
[{"groupId":"my-group","artifactId":"ItemId","version":"1","name":"com.example.common.ItemId"}]
Copy to Clipboard Copied! Toggle word wrap Toggle overflow