使用用戶端程式庫在 Pub/Sub 中發布及接收訊息
透過 Pub/Sub 服務,應用程式就能以可靠、快速、非同步的方式交換訊息。事件順序如下:
- 資料生產者將訊息發布至 Pub/Sub 主題。
- 訂閱端用戶端會建立對該主題的訂閱,並提取來自訂閱項目的訊息。
您可以透過下列任一方法設定 Pub/Sub 環境: Trusted Cloud 控制台、Cloud Shell、用戶端程式庫或 REST API。本頁面說明如何開始使用用戶端程式庫,以 Pub/Sub 發布訊息。
Pub/Sub 提供高階和低階的自動產生用戶端程式庫。根據預設,如本快速入門導覽課程所示,我們建議使用高階用戶端程式庫。
事前準備
-
Install the Google Cloud CLI.
-
設定 gcloud CLI 以使用您的聯合身分。
詳情請參閱「 使用聯合身分登入 gcloud CLI」。
-
如要初始化 gcloud CLI,請執行下列指令:
gcloud init
-
Create or select a Trusted Cloud project.
-
Create a Trusted Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Trusted Cloud project you are creating. -
Select the Trusted Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Trusted Cloud project name.
-
-
Verify that billing is enabled for your Trusted Cloud project.
-
Enable the Pub/Sub API:
gcloud services enable pubsub.googleapis.com
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/pubsub.admin
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID
: your project ID.USER_IDENTIFIER
: the identifier for your user account. For examples, see Represent workforce pool users in IAM policies.ROLE
: the IAM role that you grant to your user account.
- 如要避免系統向您的 Trusted Cloud by S3NS 帳戶收取本指南所用資源的費用,請使用指令列刪除主題和訂閱項目。
gcloud pubsub subscriptions delete my-sub gcloud pubsub topics delete my-topic
-
Optional: Revoke the authentication credentials that you created, and delete the local credential file.
gcloud auth application-default revoke
-
Optional: Revoke credentials from the gcloud CLI.
gcloud auth revoke
進一步瞭解本頁討論的 Pub/Sub 概念。
閱讀 Pub/Sub 服務的基本概念。
瞭解如何建構一對多 Pub/Sub 系統,建立發布者應用程式,並發布至兩個不同的訂閱者應用程式。
請嘗試使用 gcloud CLI 或控制台的其他 Pub/Sub 快速入門導覽課程。
進一步瞭解 Pub/Sub API。
瞭解如何使用 Kotlin 執行 Pub/Sub。
安裝用戶端程式庫
下列範例說明如何安裝用戶端程式庫:
Python
如要進一步瞭解如何設定 Python 開發環境,請參閱 Python 開發環境設定指南。
# ensure that you are using virtualenv
# as described in the python dev setup guide
pip install --upgrade google-cloud-pubsub
C++
如要進一步瞭解如何安裝 C++ 程式庫,請參閱 GitHub README
。
C#
Install-Package Google.Cloud.PubSub.V1 -Pre
Go
go get cloud.google.com/go/pubsub
Java
If you are using Maven, add
the following to your pom.xml
file. For more information about
BOMs, see The Google Cloud Platform Libraries BOM.
If you are using Gradle, add the following to your dependencies:
If you are using sbt, add the following to your dependencies:
If you're using Visual Studio Code, IntelliJ, or Eclipse, you can add client libraries to your project using the following IDE plugins:
The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.
Node.js
npm install @google-cloud/pubsub
PHP
composer require google/cloud-pubsub
Ruby
gem install google-cloud-pubsub
建立主題與訂閱項目
您建立主題後,就可訂閱或發布至該主題。
請使用下列 gcloud pubsub topics create 指令,建立名為 my-topic
的主題。請勿變更主題名稱,因為本教學課程的其餘部分會參考這個名稱。
gcloud pubsub topics create my-topic
請使用 gcloud pubsub subscriptions create 指令以建立訂閱。只有在訂閱後發佈至主題的訊息才能在訂閱者應用程式中找到。
gcloud pubsub subscriptions create my-sub --topic my-topic
如要進一步瞭解為主題及訂閱命名的資訊,請參閱資源名稱。
發布訊息
執行下列範例前,請務必取消註解並填入程式碼中標示的所有必要值。這是將範例連結至專案和先前建立的 Pub/Sub 資源的必要步驟。
請使用 my-topic
做為主題 ID。
Python
C++
C#
Go
Java
Node.js
Node.js
PHP
Ruby
接收郵件
設定訂閱者,提取您剛才發布的訊息。每個訂閱者都必須確認可設定時間範圍內的每個訊息。未確認的訊息會重新傳送。請注意,Pub/Sub 偶爾會重複傳送訊息,確保所有訊息至少會傳送給訂閱者一次。
執行下列範例前,請務必取消註解並填入程式碼中標示的所有必要值。這是將範例連結至專案和先前建立的 Pub/Sub 資源的必要步驟
使用 my-sub
做為訂閱 ID。
如需更多擷取訊息的範例,請參閱用戶端程式庫程式碼範例。
Python
C++
C#
Go
Java
Node.js
PHP
Ruby