使用 Google Cloud 控制台在 Pub/Sub 中发布和接收消息
本页面介绍如何使用Google Cloud 控制台执行 Pub/Sub 中的基本任务。
如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:
准备工作
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
- 
    
    
      In the Google Cloud console, on the project selector page, select or create a Google Cloud 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
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Pub/Sub API. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. 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 Google Cloud 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 Google Cloud console, go to the IAM page. 前往 IAM
- 选择项目。
- 点击 授予访问权限。
- 
              在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。 
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
 
- 
              
- 
    
    
      In the Google Cloud console, on the project selector page, select or create a Google Cloud 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
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Pub/Sub API. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. 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 Google Cloud 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 Google Cloud console, go to the IAM page. 前往 IAM
- 选择项目。
- 点击 授予访问权限。
- 
              在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。 
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
 
- 
              
- 在 Google Cloud 控制台中,前往 Pub/Sub 页面。 
- 在主题页面中,点击 - 创建主题 。
- 在随即打开的窗口中的主题 ID 字段中输入 - MyTopic。- 保留其余选项的默认值,然后点击创建。 - 您会看到成功消息: - A new topic and a new subscription have been successfully created.- 您刚刚创建了一个名为 - MyTopic的主题和关联的默认订阅- MyTopic-sub。
- 在 Google Cloud 控制台中,前往 Pub/Sub 订阅页面。 
- 在订阅页面中,点击创建订阅。 
- 在 - 订阅 ID 字段中输入- MySub。
- 对于选择 Cloud Pub/Sub 主题,从下拉菜单中选择 - MyTopic主题。
- 保留其余字段的默认值。 
- 点击 - 创建 。- 您会看到成功消息: - Subscription successfully added.- MySub订阅现已关联到- MyTopic主题。Pub/Sub 会将发送到- MyTopic的所有消息传递到- MySub和- MyTopic-sub订阅。
- 在 Google Cloud 控制台中,前往 Pub/Sub 主题页面。 
- 点击 - MyTopic主题。
- 点击 - 主题 页面,然后点击- MyTopic。
- 在 - MyTopic页面中,点击- 消息 标签页。
- 点击 - 发布消息 。
- 在 - 消息正文 窗口中,输入- Hello World。
- 点击 - 发布 。如果发布成功,页面底部会显示一条消息,提示您“消息已发布”。
- 在 Google Cloud 控制台中,前往 Pub/Sub 订阅页面。 
- 在消息标签页中,点击拉取。 
- 转到 - 主题 页面。
- 勾选 - MyTopic旁边的复选框,然后点击- 删除 。
- 完成确认窗口中的步骤以永久删除主题。 
- 详细了解本页面中讨论的 Pub/Sub 概念。 
- 阅读 Pub/Sub 服务的基础知识。 
- 完成 Pub/Sub 系统的端到端示例。 
- 试用另一个使用客户端库或 Google Cloud CLI 的 Pub/Sub 快速入门。 
- 详细了解 Pub/Sub API。 
创建主题
添加另一个订阅
如需为您刚刚创建的主题添加另一个订阅,请完成以下步骤:
向主题发布消息
从订阅中拉取消息
您应该会看到刚刚发布的消息。消息包含相关数据 (Hello World) 以及消息发布时间。
使用 Google Cloud 控制台时,消息量较少的单次拉取操作通常会返回零消息。如果您没有看到消息,请多次点击拉取以发出多个拉取请求。这不是 Pub/Sub 客户端库的问题。
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。
如果您不再需要您创建的主题和订阅,可以将其删除。
如需删除主题和订阅,请完成以下步骤:
或者,您也可以删除您创建的项目。