使用客户端库在 Pub/Sub 中发布和接收消息
利用 Pub/Sub 服务,应用能够可靠、快速地异步交换消息。以下是事件序列:
- 具体过程为:数据提供方将消息发布到 Pub/Sub 主题;
- 订阅者客户端会创建对该主题的订阅,并处理来自该订阅的消息。
您可以使用以下任一方法设置 Pub/Sub 环境:Google Cloud 控制台、Cloud Shell、客户端库或 REST API。本页介绍了如何开始使用客户端库发布 Pub/Sub 消息。
Pub/Sub 提供高级别和低级别自动生成的客户端库。默认情况下,我们建议使用高级客户端库(如本快速入门中所示)。
如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:
准备工作
- 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.
-
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.
-
Enable the Pub/Sub API:
gcloud services enable pubsub.googleapis.com
-
Create local authentication credentials for your user account:
gcloud auth application-default login
-
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.
-
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.
-
Enable the Pub/Sub API:
gcloud services enable pubsub.googleapis.com
-
Create local authentication credentials for your user account:
gcloud auth application-default login
-
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
安装客户端库
以下示例向您展示了如何安装客户端库:
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 --save @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
结果怎么样?
清理(可选)
- 为避免系统因本指南中使用的资源向您的 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
后续步骤
详细了解本页面中讨论的 Pub/Sub 概念。
阅读 Pub/Sub 服务的基础知识。
了解如何构建一对多 Pub/Sub 系统,该系统会创建一个发布者应用,该应用会发布到两个单独的订阅者应用。
试用另一个使用 gCloud CLI 或控制台的 Pub/Sub 快速入门。
详细了解 Pub/Sub API。
了解如何使用 Kotlin 运行 Pub/Sub。