使用客户端库在 Pub/Sub 中发布和接收消息
本页面介绍如何执行以下操作:
使用 Google Cloud CLI 创建项目、启用结算功能,并启用 Pub/Sub Lite API。
使用 Google Cloud CLI 创建精简版预留、精简版主题和精简版订阅。
使用 Go、Java 和 Python 版 Pub/Sub 精简版客户端库发送和接收消息。
准备工作
- 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 Lite API:
gcloud services enable pubsublite.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/pubsublite.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 Lite API:
gcloud services enable pubsublite.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/pubsublite.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
安装客户端库
Go
go get cloud.google.com/go/pubsublite
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.
Python
pip install --upgrade google-cloud-pubsublite
创建精简版预留
如需创建精简版预留,请使用 gcloud pubsub lite-reservations create
命令。以下示例会创建一个名为 testRes
的预留。
gcloud pubsub lite-reservations create testRes \ --location=us-central1 \ --throughput-capacity=1
创建区域级精简版主题
如需创建区域性精简版主题,请使用 gcloud pubsub lite-topics create
命令。以下示例会创建一个名为 testTopic
的主题。
gcloud pubsub lite-topics create testTopic \ --location=us-central1 \ --partitions=1 \ --per-partition-bytes=30GiB \ --throughput-reservation=projects/PROJECT_ID/locations/us-central1/reservations/testRes \
替换以下内容:
PROJECT_ID:项目的 ID
创建精简版订阅
如需创建精简版订阅,请使用 gcloud pubsub lite-subscriptions create
命令。以下示例会创建一个名为 testSub
的订阅。
gcloud pubsub lite-subscriptions create testSub \ --location=us-central1 \ --topic=testTopic \ --delivery-requirement=deliver-immediately
发送消息
使用以下发布者应用向精简版主题发送消息:
Go
在尝试此示例之前,请按照 Pub/Sub Lite 快速入门:使用客户端库中的 Go 设置说明进行操作。 如需了解详情,请参阅 Pub/Sub Lite Go API 参考文档。
如需向 Pub/Sub Lite 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
Java
Python
在尝试此示例之前,请按照 Pub/Sub Lite 快速入门:使用客户端库中的 Python 设置说明进行操作。 如需了解详情,请参阅 Pub/Sub Lite Python API 参考文档。
如需向 Pub/Sub Lite 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
发布者向精简版主题发送 100 条消息,并输出 Pub/Sub 精简版服务接收的消息数量。
接收消息
使用以下订阅者应用接收来自精简版订阅的消息:
Go
在尝试此示例之前,请按照 《Pub/Sub Lite 快速入门:使用客户端库》中的 Go 设置说明进行操作。 如需了解详情,请参阅 Pub/Sub Lite Go API 参考文档。
如需向 Pub/Sub Lite 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
Java
在尝试此示例之前,请按照 Pub/Sub Lite 快速入门:使用客户端库中的 Java 设置说明进行操作。 如需了解详情,请参阅 Pub/Sub Lite Java API 参考文档。
如需向 Pub/Sub Lite 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
Python
在尝试此示例之前,请按照 Pub/Sub Lite 快速入门:使用客户端库中的 Python 设置说明进行操作。 如需了解详情,请参阅 Pub/Sub Lite Python API 参考文档。
如需向 Pub/Sub Lite 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。
订阅者接收消息后,订阅者会输出消息 ID 和消息数据。
清理
为避免因本页面中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的 Google Cloud 项目。
- 为避免系统因本指南中使用的资源向您的 Google Cloud 账号收取费用,请删除该主题和订阅。
gcloud pubsub lite-subscriptions delete testSub --location=us-central1 gcloud pubsub lite-topics delete testTopic --location=us-central1
-
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