使用用戶端程式庫在 Pub/Sub 中發布及接收訊息
透過 Pub/Sub 服務,應用程式就能以可靠、快速、非同步的方式交換訊息。事件順序如下:
- 資料生產者將訊息發布至 Pub/Sub 主題。
- 訂閱者用戶端會建立對該主題的訂閱,並提取來自訂閱項目的訊息。
您可以使用下列任一方法設定 Pub/Sub 環境: Google Cloud 控制台、Cloud Shell、用戶端程式庫或 REST API。本頁面說明如何開始使用用戶端程式庫,以 Pub/Sub 發布訊息。
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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
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
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
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
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
- 如要避免系統向您的 Google Cloud 帳戶收取您在本指南中所用資源的費用,您可以使用指令列刪除主題和訂閱項目。
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
安裝用戶端程式庫
下列範例說明如何安裝用戶端程式庫:
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
還順利嗎?
清除 (選用)
後續步驟
進一步瞭解本頁討論的 Pub/Sub 概念。
閱讀 Pub/Sub 服務的基本概念。
瞭解如何建構一對多 Pub/Sub 系統,建立發布者應用程式,並將訊息發布至兩個個別的訂閱者應用程式。
請嘗試使用 gCloud CLI 或控制台 的其他 Pub/Sub 快速入門導覽課程。
進一步瞭解 Pub/Sub API。
瞭解如何使用 Kotlin 執行 Pub/Sub。