使用客户端库在 Pub/Sub 中发布和接收消息
利用 Pub/Sub 服务,应用能够可靠、快速地异步交换消息。以下是事件序列:
- 具体过程为:数据提供方将消息发布到 Pub/Sub 主题;
- 订阅者客户端创建对该主题的订阅,并处理来自该订阅的消息。
您可以使用以下任一方法设置 Pub/Sub 环境:Google Cloud 控制台、Cloud Shell、客户端库或 REST API。本页面介绍如何开始使用客户端库发布 Pub/Sub 消息。
Pub/Sub 提供高层级和低层级自动生成的客户端库。默认情况下,如本快速入门所示,我们建议使用高级客户端库。
如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:
准备工作
- 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.
-
-
Enable the Pub/Sub API:
gcloud services enable pubsub.googleapis.com
-
Create local authentication credentials for your user account:
gcloud auth application-default login
-
向您的 Google 账号授予角色。对以下每个 IAM 角色运行以下命令一次:
roles/pubsub.admin
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:EMAIL_ADDRESS" --role=ROLE
- 将
PROJECT_ID
替换为您的项目 ID。 - 将
EMAIL_ADDRESS
替换为您的电子邮件地址。 - 将
ROLE
替换为每个角色。
- 将
安装客户端库
以下示例向您展示了如何安装客户端库:
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
-
可选:撤消您创建的身份验证凭据,并删除本地凭据文件。
gcloud auth application-default revoke
-
可选:从 gcloud CLI 撤消凭据。
gcloud auth revoke
后续步骤
详细了解本页面中讨论的 Pub/Sub 概念。
阅读 Pub/Sub 服务的基础知识。
了解如何构建一对多 Pub/Sub 系统,该系统用于创建发布到两个单独的订阅者应用的发布者应用。
尝试使用 gCloud CLI 或控制台的其他 Pub/Sub 快速入门。
详细了解 Pub/Sub API。
了解如何使用 Kotlin 运行 Pub/Sub。