本文档介绍如何为 Pub/Sub 主题创建架构。
准备工作
在创建架构之前,请先完成以下操作:
- 了解主题和发布工作流程。
- 了解 Pub/Sub 架构的工作原理。
- 创建一个主题。
所需的角色和权限
如需获取创建和管理架构所需的权限,
请让管理员授予您
项目的 Pub/Sub Editor (roles/pubsub.editor
) IAM 角色。
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
此预定义角色包含 创建和管理架构所需的权限。如需查看所需的确切权限,请展开所需权限部分:
所需权限
如需创建和管理架构,您需要具备以下权限:
-
创建架构:
pubsub.schemas.create
-
将架构附加到主题:
pubsub.schemas.attach
-
提交架构修订版本:
pubsub.schemas.commit
-
删除架构或架构修订版本:
pubsub.schemas.delete
-
获取架构或架构修订版本:
pubsub.schemas.get
-
列出架构:
pubsub.schemas.list
-
列出架构修订版本:
pubsub.schemas.listRevisions
-
回滚架构:
pubsub.schemas.rollback
-
验证消息:
pubsub.schemas.validate
-
获取架构的 IAM 政策:
pubsub.schemas.getIamPolicy
-
为架构配置 IAM 政策:
pubsub.schemas.setIamPolicy
您可以向主账号(例如用户、群组 网域或服务账号。您可以在一个项目中创建架构 将其附加到其他项目中的主题。 确保您拥有 资源。
创建架构
您可以使用 Google Cloud 控制台、gcloud CLI Pub/Sub API 或 Cloud 客户端库使用。以下是 创建架构的一些准则:
如需让架构能够与 Pub/Sub 搭配使用,您只需定义 一个顶级类型。引用其他类型的 import 语句 不受支持。
您可以将同一架构与多个主题相关联。
您可以手动测试消息是否根据架构进行验证。
控制台
如需创建架构,请按照以下步骤操作:
在 Google Cloud 控制台中,前往 Pub/Sub 架构页面。
点击创建架构。
在架构 ID 字段中,输入架构的 ID。
如需了解架构命名准则,请参阅主题、订阅或快照命名准则。
对于 Schema type,选择 Avro 或 Protocol Buffer。
详细了解架构类型。
在架构定义字段中,输入 Avro 或 Protocol Buffer 架构定义
例如,以下是 Avro 中的示例架构。
{ "type": "record", "name": "Avro", "fields": [ { "name": "ProductName", "type": "string", "default": "" }, { "name": "SKU", "type": "int", "default": 0 }, { "name": "InStock", "type": "boolean", "default": false } ] }
可选:点击验证定义,检查架构定义是否正确无误 正确。
验证检查不会检查架构与 要发布的消息在下一步中测试消息。
可选:您可以测试是否发布了架构正确的消息。
点击测试消息。
在 Test message 窗口中,选择一种消息编码。
在邮件正文中,输入测试邮件。
点击测试。
例如,下面是测试架构的消息示例。 在此示例中,选择 Message encoding(消息编码)为
JSON
。{"ProductName":"GreenOnions", "SKU":34543, "InStock":true}
退出测试消息页面。
点击创建以保存架构。
gcloud
gcloud pubsub schemas create SCHEMA_ID \ --type=SCHEMA_TYPE \ --definition=SCHEMA_DEFINITION
其中:
- SCHEMA_TYPE 是
avro
或protocol-buffer
。 - SCHEMA_DEFINITION 是包含架构定义的
string
,系统会根据所选架构类型设置其格式。
您还可以在文件中指定架构定义:
gcloud pubsub schemas create SCHEMA_ID \ --type=SCHEMA_TYPE \ --definition-file=SCHEMA_DEFINITION_FILE
其中:
- SCHEMA_TYPE 是
avro
或protocol-buffer
。 - SCHEMA_DEFINITION_FILE 是一个
string
,其中包含指向 包含架构定义的文件,并根据所选的 架构类型。
REST
要创建架构,请发送如下所示的 POST 请求:
POST https://pubsub.googleapis.com/v1/projects/PROJECT_ID/schemas/SCHEMA_ID Authorization: Bearer $(gcloud auth application-default print-access-token) Content-Type: application/json --data @response-body.json
在请求正文中指定以下字段:
{ "definition": SCHEMA_DEFINITION "type": SCHEMA_TYPE }
其中:
- SCHEMA_TYPE 是
avro
或protocol-buffer
。 - SCHEMA_DEFINITION 是包含架构定义的字符串,系统会根据所选架构类型设置其格式。
响应正文应包含架构资源的 JSON 表示法。例如:
{ "name": SCHEMA_NAME, "type": SCHEMA_TYPE, "definition": SCHEMA_DEFINITION "revisionId": REVISION_ID "revisionCreateTime": REVISION_CREATE_TIME }
其中:
- REVISION_ID 是服务器为修订版本生成的 ID。
- REVISION_CREATE_TIME 是 ISO 8601 时间戳, 已创建修订版本。
C++
在尝试此示例之前,请按照《快速入门:使用客户端库》中的 C++ 设置说明进行操作。如需了解详情,请参阅 Pub/Sub C++ API 参考文档。
Avro
Proto
C#
在尝试此示例之前,请按照《快速入门:使用客户端库》中的 C# 设置说明进行操作。 如需了解详情,请参阅 Pub/Sub C# API 参考文档。
Avro
Proto
Go
在尝试此示例之前,请按照《快速入门:使用客户端库》中的 Go 设置说明进行操作。 如需了解详情,请参阅 Pub/Sub Go API 参考文档。
Avro
Proto
Java
在尝试此示例之前,请按照《快速入门:使用客户端库》中的 Java 设置说明进行操作。 如需了解详情,请参阅 Pub/Sub Java API 参考文档。
Avro
Proto
Node.js
在尝试此示例之前,请按照《快速入门:使用客户端库》中的 Node.js 设置说明进行操作。如需了解详情,请参阅 Pub/Sub Node.js API 参考文档。
Avro
Proto
Node.js
在尝试此示例之前,请按照《快速入门:使用客户端库》中的 Node.js 设置说明进行操作。如需了解详情,请参阅 Pub/Sub Node.js API 参考文档。
Avro
Proto
PHP
在尝试此示例之前,请按照《快速入门:使用客户端库》中的 PHP 设置说明进行操作。如需了解详情,请参阅 Pub/Sub PHP API 参考文档。
Avro
Proto
Python
在尝试此示例之前,请按照《快速入门:使用客户端库》中的 Python 设置说明进行操作。 如需了解详情,请参阅 Pub/Sub Python API 参考文档。
Avro
Proto
Ruby
在尝试此示例之前,请按照《快速入门:使用客户端库》中的 Ruby 设置说明进行操作。 如需了解详情,请参阅 Pub/Sub Ruby API 参考文档。
Avro
Proto
创建架构后,您可以在架构页面中查看架构的详细信息。
您可以将架构与主题相关联。