使用 Google Cloud 控制台在 Pub/Sub 中發布及接收訊息

本頁面說明如何使用Google Cloud 控制台在 Pub/Sub 中執行基本工作。


如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


事前準備

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Pub/Sub API.

    Enable the API

  5. Make sure that you have the following role or roles on the project: Pub/Sub Admin

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. 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.

    4. 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

    1. In the Google Cloud console, go to the IAM page.

      前往「IAM」頁面
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Enable the Pub/Sub API.

    Enable the API

  9. Make sure that you have the following role or roles on the project: Pub/Sub Admin

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. 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.

    4. 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

    1. In the Google Cloud console, go to the IAM page.

      前往「IAM」頁面
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]

建立主題

  1. 前往 Google Cloud 控制台的「Pub/Sub」頁面。

    前往 Pub/Sub

  2. 在「Topics」(主題) 頁面中,點按「Create topic」(建立主題)

  3. 在開啟的視窗中找到「Topic ID」(主題 ID) 欄位,然後輸入 MyTopic

    保留其餘選項的預設值,然後按一下「Create」(建立)

    系統會顯示成功訊息:A new topic and a new subscription have been successfully created.

    您剛建立名為 MyTopic 的主題,以及相關聯的預設訂閱 MyTopic-sub

新增第二個訂閱項目

如要為剛建立的主題新增第二個訂閱項目,請完成下列步驟:

  1. 前往 Google Cloud 控制台的「Pub/Sub subscriptions」(Pub/Sub 訂閱項目) 頁面。

    前往「訂閱項目」頁面

  2. 在「Subscriptions」(訂閱項目)頁面中,按一下「Create subscription」(建立訂閱項目)

  3. 「Subscription ID」(訂閱項目 ID) 欄位中輸入 MySub

  4. 在「Select a Cloud Pub/Sub topic」(選取 Cloud Pub/Sub 主題) 中,從下拉式選單中選取 MyTopic 主題。

  5. 保留其餘選項的預設值。

  6. 按一下「Create」(建立)

    系統會顯示成功訊息:Subscription successfully added.

    MySub 訂閱項目現在已附加至 MyTopic 主題。Pub/Sub 會將傳送至 MyTopic 的所有訊息傳遞到 MySubMyTopic-sub 訂閱項目。

將訊息發布到主題

  1. 前往 Google Cloud 控制台的「Pub/Sub topics」(Pub/Sub 主題) 頁面。

    前往「主題」

  2. 按一下 MyTopic 主題。

  3. 按一下「Topics」(主題) 頁面,然後點按 MyTopic

  4. MyTopic 頁面中,點按「Messages」(訊息) 分頁標籤。

  5. 按一下「Publish message」(發布訊息)

  6. 「Message body」(訊息內文) 視窗中輸入 Hello World

  7. 按一下「Publish」(發布)。發布成功後,頁面底部會顯示「Message published」(訊息已發布) 訊息。

提取訂閱項目中的訊息

  1. 前往 Google Cloud 控制台的「Pub/Sub subscriptions」(Pub/Sub 訂閱項目) 頁面。

    前往「訂閱項目」頁面

  2. 在「Messages」(訊息) 分頁中,按一下「Pull」(提取)

您應該會看到剛發布的訊息。訊息中會包含資料、Hello World 和訊息發布的時間。

使用 Google Cloud 控制台時,低訊息量的個別提取項目通常會傳回零訊息。如果您沒有看到訊息,請多按幾次「Pull」(提取) 來發出多個提取要求。這並不是 Pub/Sub 用戶端程式庫的問題。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。

如果不再需要所建立的某個主題和訂閱項目,可以刪除。

如要刪除主題和訂閱項目,請完成下列步驟:

  1. 前往「Topics」(主題) 頁面

  2. 勾選 MyTopic 旁邊的核取方塊,然後按一下「Delete」(刪除)

  3. 完成確認視窗中的步驟,即可永久刪除主題。

或者,您也可以刪除先前建立的專案

後續步驟