使用 Google Cloud 控制台在 Pub/Sub 中發布及接收訊息
本頁面說明如何使用Google Cloud 控制台在 Pub/Sub 中執行基本工作。
如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導):
事前準備
- 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
- 選取專案。
- 按一下 「授予存取權」。
- 
              在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。 
- 在「Select a role」(選取角色) 清單中,選取角色。
- 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
- 按一下 [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
- 選取專案。
- 按一下 「授予存取權」。
- 
              在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。 
- 在「Select a role」(選取角色) 清單中,選取角色。
- 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
- 按一下 [Save]。
 
- 
              
- 前往 Google Cloud 控制台的「Pub/Sub」頁面。 
- 在「Topics」(主題) 頁面中,點按 - 「Create topic」(建立主題) 。
- 在開啟的視窗中找到「Topic ID」(主題 ID) 欄位,然後輸入 - MyTopic。- 保留其餘選項的預設值,然後按一下「Create」(建立)。 - 系統會顯示成功訊息: - A new topic and a new subscription have been successfully created.- 您剛建立名為 - MyTopic的主題,以及相關聯的預設訂閱- MyTopic-sub。
- 前往 Google Cloud 控制台的「Pub/Sub subscriptions」(Pub/Sub 訂閱項目) 頁面。 
- 在「Subscriptions」(訂閱項目)頁面中,按一下「Create subscription」(建立訂閱項目)。 
- 在 - 「Subscription ID」(訂閱項目 ID) 欄位中輸入- MySub。
- 在「Select a Cloud Pub/Sub topic」(選取 Cloud Pub/Sub 主題) 中,從下拉式選單中選取 - MyTopic主題。
- 保留其餘選項的預設值。 
- 按一下 - 「Create」(建立) 。- 系統會顯示成功訊息: - Subscription successfully added.- MySub訂閱項目現在已附加至- MyTopic主題。Pub/Sub 會將傳送至- MyTopic的所有訊息傳遞到- MySub和- MyTopic-sub訂閱項目。
- 前往 Google Cloud 控制台的「Pub/Sub topics」(Pub/Sub 主題) 頁面。 
- 按一下 - MyTopic主題。
- 按一下 - 「Topics」(主題) 頁面,然後點按- MyTopic。
- 在 - MyTopic頁面中,點按- 「Messages」(訊息) 分頁標籤。
- 按一下 - 「Publish message」(發布訊息) 。
- 在 - 「Message body」(訊息內文) 視窗中輸入- Hello World。
- 按一下 - 「Publish」(發布) 。發布成功後,頁面底部會顯示「Message published」(訊息已發布) 訊息。
- 前往 Google Cloud 控制台的「Pub/Sub subscriptions」(Pub/Sub 訂閱項目) 頁面。 
- 在「Messages」(訊息) 分頁中,按一下「Pull」(提取)。 
- 前往 - 「Topics」(主題) 頁面
- 勾選 - MyTopic旁邊的核取方塊,然後按一下- 「Delete」(刪除) 。
- 完成確認視窗中的步驟,即可永久刪除主題。 
- 進一步瞭解本頁討論的 Pub/Sub 概念。 
- 閱讀 Pub/Sub 服務的基本概念。 
- 實際練習 Pub/Sub 系統的端對端範例。 
- 請嘗試其他使用用戶端程式庫或 Google Cloud CLI 的 Pub/Sub 快速入門導覽課程。 
- 進一步瞭解 Pub/Sub API。 
建立主題
新增第二個訂閱項目
如要為剛建立的主題新增第二個訂閱項目,請完成下列步驟:
將訊息發布到主題
提取訂閱項目中的訊息
您應該會看到剛發布的訊息。訊息中會包含資料、Hello World 和訊息發布的時間。
使用 Google Cloud 控制台時,低訊息量的個別提取項目通常會傳回零訊息。如果您沒有看到訊息,請多按幾次「Pull」(提取) 來發出多個提取要求。這並不是 Pub/Sub 用戶端程式庫的問題。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。
如果不再需要所建立的某個主題和訂閱項目,可以刪除。
如要刪除主題和訂閱項目,請完成下列步驟:
或者,您也可以刪除先前建立的專案。