GSP094
概览
使用 Google Cloud Pub/Sub 服务,应用能够可靠、快速地异步交换消息。为实现这一目标,数据制作者需要将消息发布到 Cloud Pub/Sub 主题。然后,订阅程序客户端会创建对该主题的订阅,并处理来自该订阅的消息。对于无法可靠地传送的消息,Cloud Pub/Sub 最多会将其保留七天。
在本实验中,您将学习如何开始使用 Python 客户端库在 Cloud Pub/Sub 上发布消息。
您将执行的操作
在本实验中,您需要执行以下操作:
- 了解 Pub/Sub 的基础知识。
- 创建并列出 Pub/Sub 主题。
- 创建并列出 Pub/Sub 订阅。
- 向主题发布消息。
- 使用拉取订阅程序输出单独的主题消息。
设置和要求
点击“开始实验”按钮前的注意事项
请阅读以下说明。实验是计时的,并且您无法暂停实验。计时器在您点击开始实验后即开始计时,显示 Google Cloud 资源可供您使用多长时间。
此实操实验可让您在真实的云环境中开展实验活动,免受模拟或演示环境的局限。我们会为您提供新的临时凭据,让您可以在实验规定的时间内用来登录和访问 Google Cloud。
为完成此实验,您需要:
- 能够使用标准的互联网浏览器(建议使用 Chrome 浏览器)。
注意:请使用无痕模式或无痕浏览器窗口运行此实验。这可以避免您的个人账号与学生账号之间发生冲突,这种冲突可能导致您的个人账号产生额外费用。
注意:如果您已有自己的个人 Google Cloud 账号或项目,请不要在此实验中使用,以避免您的账号产生额外的费用。
如何开始实验并登录 Google Cloud 控制台
-
点击开始实验按钮。如果该实验需要付费,系统会打开一个弹出式窗口供您选择付款方式。左侧是实验详细信息面板,其中包含以下各项:
-
打开 Google 控制台按钮
- 剩余时间
- 进行该实验时必须使用的临时凭据
- 帮助您逐步完成本实验所需的其他信息(如果需要)
-
点击打开 Google 控制台。
该实验会启动资源并打开另一个标签页,显示登录页面。
提示:请将这些标签页安排在不同的窗口中,并将它们并排显示。
注意:如果您看见选择帐号对话框,请点击使用其他帐号。
-
如有必要,请从实验详细信息面板复制用户名,然后将其粘贴到登录对话框中。点击下一步。
-
请从实验详细信息面板复制密码,然后将其粘贴到欢迎对话框中。点击下一步。
重要提示:您必须使用左侧面板中的凭据。请勿使用您的 Google Cloud Skills Boost 凭据。
注意:在本次实验中使用您自己的 Google Cloud 帐号可能会产生额外费用。
-
继续在后续页面中点击以完成相应操作:
- 接受条款及条件。
- 由于该帐号为临时帐号,请勿添加帐号恢复选项或双重验证。
- 请勿注册免费试用。
片刻之后,系统会在此标签页中打开 Cloud 控制台。
注意:您可以点击左上角的导航菜单来查看列有 Google Cloud 产品和服务的菜单。
激活 Cloud Shell
Cloud Shell 是一种装有开发者工具的虚拟机。它提供了一个永久性的 5GB 主目录,并且在 Google Cloud 上运行。Cloud Shell 提供可用于访问您的 Google Cloud 资源的命令行工具。
- 点击 Google Cloud 控制台顶部的激活 Cloud Shell 。
如果您连接成功,即表示您已通过身份验证,且当前项目会被设为您的 PROJECT_ID 环境变量所指的项目。输出内容中有一行说明了此会话的 PROJECT_ID:
Your Cloud Platform project in this session is set to YOUR_PROJECT_ID
gcloud
是 Google Cloud 的命令行工具。它已预先安装在 Cloud Shell 上,且支持 Tab 自动补全功能。
- (可选)您可以通过此命令列出活跃账号名称:
gcloud auth list
-
点击授权。
-
现在,输出的内容应如下所示:
输出:
ACTIVE: *
ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net
To set the active account, run:
$ gcloud config set account `ACCOUNT`
- (可选)您可以通过此命令列出项目 ID:
gcloud config list project
输出:
[core]
project = <project_ID>
输出示例:
[core]
project = qwiklabs-gcp-44776a13dea667a6
Note: For full documentation of gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
任务 1. 创建虚拟环境
Python 虚拟环境用于将软件包安装与系统隔离开来。
- 安装
virtualenv
环境:
sudo apt-get install -y virtualenv
- 构建虚拟环境:
python3 -m venv venv
- 激活虚拟环境。
source venv/bin/activate
任务 2. 安装客户端库
- 运行以下命令以安装客户端库:
pip install --upgrade google-cloud-pubsub
- 克隆 GitHub 代码库以获取示例代码:
git clone https://github.com/googleapis/python-pubsub.git
- 进入该目录:
cd python-pubsub/samples/snippets
任务 3. Pub/Sub - 基础知识
Google Cloud Pub/Sub 是一种异步全球消息传递服务。Pub/Sub 中常见的三个术语是“主题”“发布”和“订阅”。
主题是一个共享字符串,可让应用通过普通会话与其他应用联系。
发布者向 Cloud Pub/Sub 主题推送(或发布)消息。然后,订阅程序会针对该会话创建订阅,并从主题中拉取消息或为推送订阅配置网络钩子。每个订阅程序都必须在可配置的时间范围内确认每条消息。
总的来说,发布者创建主题并向主题发送消息,订阅程序则为相应主题创建订阅以接收来自该主题的消息。
Google Cloud 中的 Pub/Sub
Pub/Sub 已预安装在 Cloud Shell 中,因此您无需安装或配置即可开始使用此服务。在本实验中,您将使用 Python 创建主题、订阅程序,然后查看相应消息。您可以使用 gcloud 命令行将消息发布到主题中。
任务 4. 创建主题
要向 Cloud Pub/Sub 发布数据,您需要创建一个主题,然后为该主题配置一位发布者。
- 在 Cloud Shell 中,您的项目 ID 应该会自动存储在环境变量
GOOGLE_CLOUD_PROJECT
中:
echo $GOOGLE_CLOUD_PROJECT
- 请确保输出与连接详情中的项目 ID 一致。
publisher.py
是一个脚本,可展示如何使用 Cloud Pub/Sub API 对主题执行基本操作。查看发布者脚本的内容:
cat publisher.py
注意:您也可以使用 Cloud Shell 中已安装的 Shell 编辑器(例如 nano 或 vim),或者使用 Cloud Shell 代码编辑器来查看 python-pubsub/samples/snippets/publisher.py
。
- 发布者脚本的信息:
python publisher.py -h
输出示例:
usage: publisher.py [-h]
project
{list,create,delete,publish,publish-with-custom-attributes,publish-with-futures,publish-with-error-handler,publish-with-batch-settings}
...
This application demonstrates how to perform basic operations on topics
with the Cloud Pub/Sub API.
For more information, see the README.md under /pubsub and the documentation
at https://cloud.google.com/pubsub/docs.
positional arguments:
project Your Google Cloud project ID
{list,create,delete,publish,publish-with-custom-attributes,publish-with-futures,publish-with-error-handler,publish-with-batch-settings}
list Lists all Pub/Sub topics in the given project.
create Create a new Pub/Sub topic.
delete Deletes an existing Pub/Sub topic.
publish Publishes multiple messages to a Pub/Sub topic.
publish-with-custom-attributes
Publishes multiple messages with custom attributes to
a Pub/Sub topic.
publish-with-futures
Publishes multiple messages to a Pub/Sub topic and
prints their message IDs.
publish-with-error-handler
Publishes multiple messages to a Pub/Sub topic with an
error handler.
publish-with-batch-settings
Publishes multiple messages to a Pub/Sub topic with
batch settings.
optional arguments:
-h, --help show this help message and exit
- 运行以下发布者脚本以创建 Pub/Sub 主题:
python publisher.py $GOOGLE_CLOUD_PROJECT create MyTopic
输出示例:
Topic created: name: "projects/qwiklabs-gcp-fe27729bc161fb22/topics/MyTopic"
测试已完成的任务
点击检查我的进度以验证您已完成的任务。如果您成功创建了一个 Cloud Pub/Sub 主题,系统将会显示一个评估分数。
创建主题。
- 以下命令会返回指定项目中所有 Pub/Sub 主题的列表:
python publisher.py $GOOGLE_CLOUD_PROJECT list
输出示例:
name: "projects/qwiklabs-gcp-fe27729bc161fb22/topics/MyTopic"
您也可以在 Cloud 控制台中查看您刚刚创建的主题。
- 前往导航菜单 > Pub/Sub > 主题。
您应该会看到 MyTopic
。
任务 5. 创建订阅
- 使用
subscriber.py
脚本为主题创建 Pub/Sub 订阅:
python subscriber.py $GOOGLE_CLOUD_PROJECT create MyTopic MySub
测试已完成的任务
点击检查我的进度以验证您已完成的任务。如果您成功创建了 Cloud Pub/Sub 订阅,系统将会显示一个评估分数。
创建订阅。
- 以下命令会返回指定项目中订阅程序的列表:
python subscriber.py $GOOGLE_CLOUD_PROJECT list-in-project
您只会看到一个订阅,因为您只创建了一个。
输出示例:
projects/qwiklabs-gcp-7877af129f04d8b3/subscriptions/MySub
-
在控制台中查看您刚刚创建的订阅。在左侧窗格中,点击订阅。您应该会看到订阅名称和其他详情。
-
关于 subscriber
脚本的信息:
python subscriber.py -h
输出:
usage: subscriber.py [-h]
project
{list_in_topic,list_in_project,create,create-push,delete,update,receive,receive-custom-attributes,receive-flow-control,receive-synchronously,listen_for_errors}
...
This application demonstrates how to perform basic operations on
subscriptions with the Cloud Pub/Sub API.
For more information, see the README.md under /pubsub and the documentation
at https://cloud.google.com/pubsub/docs.
positional arguments:
project Your Google Cloud project ID
{list_in_topic,list_in_project,create,create-push,delete,update,receive,receive-custom-attributes,receive-flow-control,receive-synchronously,listen_for_errors}
list_in_topic Lists all subscriptions for a given topic.
list_in_project Lists all subscriptions in the current project.
create Create a new pull subscription on the given topic.
create-push Create a new push subscription on the given topic.
delete Deletes an existing Pub/Sub topic.
update Updates an existing Pub/Sub subscription's push
endpoint URL. Note that certain properties of a
subscription, such as its topic, are not modifiable.
receive Receives messages from a pull subscription.
receive-custom-attributes
Receives messages from a pull subscription.
receive-flow-control
Receives messages from a pull subscription with flow
control.
receive-synchronously
Pulling messages synchronously.
listen_for_errors Receives messages and catches errors from a pull
subscription.
optional arguments:
-h, --help show this help message and exit
任务 6. 发布消息
现在,您已设置了 MyTopic
(主题)、创建了对 MyTopic
(MySub
) 的订阅,请尝试使用 gcloud 命令行向 MyTopic
发布消息。
- 向
MyTopic
发布消息“hello”:
gcloud pubsub topics publish MyTopic --message "Hello"
- 向
MyTopic
再发送一些消息 - 运行以下命令(使用您的姓名和喜欢的食物替换 <> 中的内容):
gcloud pubsub topics publish MyTopic --message "Publisher's name is <您的姓名>"
gcloud pubsub topics publish MyTopic --message "Publisher likes to eat <食物>"
gcloud pubsub topics publish MyTopic --message "Publisher thinks Pub/Sub is awesome"
任务 7. 查看邮件
现在,您已向 MyTopic 发送了多条消息,请使用 Mysub 拉取和查看这些消息。
- 使用 MySub 从 MyTopic 中拉取消息:
python subscriber.py $GOOGLE_CLOUD_PROJECT receive MySub
输出示例:
Listening for messages on projects/qwiklabs-gcp-7877af129f04d8b3/subscriptions/MySub
Received message: Message {
data: 'Publisher thinks Pub/Sub is awesome'
attributes: {}
}
Received message: Message {
data: 'Hello'
attributes: {}
}
Received message: Message {
data: "Publisher's name is Harry"
attributes: {}
}
Received message: Message {
data: 'Publisher likes to eat cheese'
attributes: {}
}
- 按 Ctrl + c 以停止监听。
任务 8. 检验您的掌握情况
我们在下方准备了一些单选题,以加强您对本实验所涉概念的理解。请尽您所能回答。
恭喜!
您使用 Python 创建了一个 Pub/Sub 主题,向该主题发布了数据,还创建了一个订阅并用它从该主题拉取了数据。
完成挑战任务
本自学实验包含在基准:基础架构挑战任务中。一项挑战任务就是一系列相关的实验,学习时按部就班地完成这些实验即可。完成挑战任务即可赢得一枚徽章,以表彰您取得的成就。您可以公开展示徽章,还可以在您的在线简历或社交媒体账号中加入指向徽章的链接。欢迎注册参加此挑战任务或任何包含此实验的挑战任务,完成后就能立即获得相应的积分。请参阅 Google Cloud Skills Boost 目录,查看提供的所有挑战任务。
参与下一项实验
Pub/Sub Lite:Qwik StartPub/Sub Lite 作为 Pub/Sub 的补充,是适用于具有可预测流量模式的消息传递系统的区域服务。如果您每秒发布 1 MiB 至 1 GiB 消息,则 Pub/Sub Lite 是适合提取大量事件的费用最低的一种方案。
后续步骤/了解详情
本实验是 Qwik Starts 系列实验的其中一项。通过这些实验,您可以一窥 Google Cloud 的各项功能。请在 Google Cloud Skills Boost 目录中搜索“Qwik Starts”,找到您要参与的下一项实验!
Google Cloud 培训和认证
…可帮助您充分利用 Google Cloud 技术。我们的课程会讲解各项技能与最佳实践,可帮助您迅速上手使用并继续学习更深入的知识。我们提供从基础到高级的全方位培训,并有点播、直播和虚拟三种方式选择,让您可以按照自己的日程安排学习时间。各项认证可以帮助您核实并证明您在 Google Cloud 技术方面的技能与专业知识。
上次更新手册的时间:2023 年 9 月 22 日
上次测试实验的时间:2023 年 9 月 22 日
版权所有 2024 Google LLC 保留所有权利。Google 和 Google 徽标是 Google LLC 的商标。其他所有公司名和产品名可能是其各自相关公司的商标。