使用 Cloud de Confiance 控制台在 Pub/Sub 中发布和接收消息
本页面介绍如何使用Cloud de Confiance 控制台执行 Pub/Sub 中的基本任务。
准备工作
-
In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Cloud de Confiance project.
Enable the Pub/Sub API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Make sure that you have the following role or roles on the project: Pub/Sub Admin
Check for the roles
-
In the Cloud de Confiance console, go to the IAM page.
Go to IAM - Select the project.
-
In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
Grant the roles
-
In the Cloud de Confiance console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the identifier for a user in a workforce identity pool. For details, see Represent workforce pool users in IAM policies, or contact your administrator.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
创建主题
在 Cloud de Confiance 控制台中,前往 Pub/Sub 页面。
在主题页面中,点击
创建主题 。在随即打开的窗口中的主题 ID 字段中输入
MyTopic。保留其余选项的默认值,然后点击创建。
您会看到成功消息:
A new topic and a new subscription have been successfully created.您刚刚创建了一个名为
MyTopic的主题和关联的默认订阅MyTopic-sub。
添加另一个订阅
如需为您刚刚创建的主题添加另一个订阅,请完成以下步骤:
在 Cloud de Confiance 控制台中,前往 Pub/Sub 订阅页面。
在订阅页面中,点击创建订阅。
在
订阅 ID 字段中输入MySub。对于选择 Cloud Pub/Sub 主题,从下拉菜单中选择
MyTopic主题。保留其余字段的默认值。
点击
创建 。您会看到成功消息:
Subscription successfully added.MySub订阅现已关联到MyTopic主题。Pub/Sub 会将发送到MyTopic的所有消息传递到MySub和MyTopic-sub订阅。
向主题发布消息
在 Cloud de Confiance 控制台中,前往 Pub/Sub 主题页面。
点击
MyTopic主题。点击
主题 页面,然后点击MyTopic。在
MyTopic页面中,点击消息 标签页。点击
发布消息 。在
消息正文 窗口中,输入Hello World。点击
发布 。如果发布成功,页面底部会显示一条消息,提示您“消息已发布”。
从订阅中拉取消息
在 Cloud de Confiance 控制台中,前往 Pub/Sub 订阅页面。
在消息标签页中,点击拉取。
您应该会看到刚刚发布的消息。消息包含相关数据 (Hello World) 以及消息发布时间。
使用 Cloud de Confiance 控制台时,消息量较少的单次拉取操作通常会返回零消息。如果您没有看到消息,请多次点击拉取以发出多个拉取请求。这不是 Pub/Sub 客户端库的问题。
清理
为避免因本页中使用的资源导致您的 Cloud de Confiance 账号产生费用,请按照以下步骤操作。
如果您不再需要您创建的主题和订阅,可以将其删除。
如需删除主题和订阅,请完成以下步骤:
转到
主题 页面。勾选
MyTopic旁边的复选框,然后点击删除 。完成确认窗口中的步骤以永久删除主题。
或者,您也可以删除您创建的项目。
后续步骤
详细了解本页面中讨论的 Pub/Sub 概念。
阅读 Pub/Sub 服务的基础知识。
完成 Pub/Sub 系统的端到端示例。
试用另一个使用客户端库或 Google Cloud CLI 的 Pub/Sub 快速入门。
详细了解 Pub/Sub API。