使用 Google Cloud 控制台在 Pub/Sub 中發布及接收訊息
本頁面說明如何使用Google Cloud 控制台在 Pub/Sub 中執行基本工作。
如要直接在 Google Cloud 控制台中,按照這項工作的逐步指南操作,請按一下「Guide me」(逐步引導):
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
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 role
(
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.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click 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 role
(
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.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the email address for a Google Account.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click 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」(提取)。
您應該會看到剛發布的訊息。訊息中會包含資料、Hello World 和訊息發布的時間。
使用 Google Cloud 控制台時,低訊息量的個別提取項目通常會傳回零訊息。如果您沒有看到訊息,請多按幾次「Pull」(提取) 來發出多個提取要求。這並不是 Pub/Sub 用戶端程式庫的問題。
清除所用資源
為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。
如果不再需要所建立的某個主題和訂閱項目,可以刪除。
如要刪除主題和訂閱項目,請完成下列步驟:
前往
「Topics」(主題) 頁面勾選
MyTopic旁邊的核取方塊,然後按一下「Delete」(刪除) 。完成確認視窗中的步驟,即可永久刪除主題。
或者,您也可以刪除先前建立的專案。
後續步驟
進一步瞭解本頁討論的 Pub/Sub 概念。
閱讀 Pub/Sub 服務的基本概念。
實際練習 Pub/Sub 系統的端對端範例。
請嘗試其他使用用戶端程式庫或 Google Cloud CLI 的 Pub/Sub 快速入門導覽課程。
進一步瞭解 Pub/Sub API。