使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
使用 Google Cloud 控制台安排和运行 Cron 作业
本快速入门介绍了如何使用 Google Cloud 控制台通过 Cloud Scheduler 执行一些基本操作 。
在本快速入门中,您将执行以下操作:
- 创建 Pub/Sub 主题以设置为 Cloud Scheduler
作业目标。 - 使用 Cloud Scheduler 创建一个 Cron 作业,并为该作业配置周期性时间表。
- 运行作业 。
- 验证作业是否已成功运行 。
Cloud Scheduler 提供免费层级,运行此快速入门不会产生任何费用。要了解详情,请参阅价格。
如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:
操作演示
准备工作
-
Sign in to your Google Cloud account . If you ‘re new to
Google Cloud ,
create an account to evaluate how our product perform in
real – world scenario . new customers is get also get $ 300 in free credit to
run , test , and deploy workload . -
In the Google Cloud console , on the project selector page ,
select or create a Google Cloud project .note: If you don’t plan to keep the
resources that you create in this procedure, create a project instead of
selecting an existing project. After you finish these steps, you can
delete the project, removing all resources associated with the project.Go to project selector
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Scheduler, Pub/Sub APIs.
Enable the APIs
-
In the Google Cloud console , on the project selector page ,
select or create a Google Cloud project .note: If you don’t plan to keep the
resources that you create in this procedure, create a project instead of
selecting an existing project. After you finish these steps, you can
delete the project, removing all resources associated with the project.Go to project selector
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Scheduler, Pub/Sub APIs.
Enable the APIs
创建 Pub/Sub 主题和订阅
Pub/Sub 主题是发布者可以向其发送消息的资源。如需接收发布到某个主题的消息,您必须创建对该主题的订阅。
-
在 Google Cloud 控制台中,前往 Pub/Sub 页面。
转到 Pub/Sub
-
点击创建主题。
-
在主题 ID 字段中,输入 cron – topic 作为主题的 ID。
-
保留默认设置,包括添加默认订阅选项。
-
请勿选择任何其他选项。
-
点击创建 。
这将创建一个名为 cron - topic
的 Pub/Sub 主题和一个
该主题名为“cron - topic-sub
”的订阅。
使用 Cloud Scheduler 创建 Cron 作业
设置工作单元(称为“Cron 作业”),按周期性时间表发送到目标。
-
在 Google Cloud 控制台中,转到 Cloud Scheduler 页面 。
转到 Cloud Scheduler
-
点击创建作业。
-
为作业指定名称。
-
在区域列表中,为您的作业选择一个区域 。
-
使用基于 unix-cron 的格式为您的作业指定频率。例如:
30 16 * * 7
如需了解详情,请参阅配置 Cron 作业时间表。
-
在时区列表中,选择一个时区。
对于某些时区,夏令时可能会导致作业运行或无法运行
。如需了解详情,请参阅时区。 -
点击继续。
-
在目标类型列表中,选择 Pub/Sub。
-
选择您之前创建的主题:
cron - topic
。 -
在邮件正文字段中,输入要发送到
Pub / sub 目标主题。例如:“Hello world ! ” -
点击创建 。
您创建了一个作业,该作业会在周日 16:30 向 Pub/Sub 主题发送一条消息。现在,您可以运行该作业了。
运行作业
除了按照指定的时间表执行外,您还可以强制
立即运行
-
在 Google Cloud 控制台中,转到 Cloud Scheduler 页面 。
前往 Cloud Scheduler
系统应该会列出您的作业 。
-
在您的作业所在的行中,点击more_vert
操作> 强制运行。请注意,由于需要进行一些初始配置,因此在项目中创建的第一项作业可能需要几分钟时间才能运行完毕。
-
在上次执行状态列中,
Success
状态表示您已成功运行 Cron 作业。
接下来,您可以验证 Pub / Sub is 是否收到了消息 是否收到了消息 。
验证 Pub/Sub 中的结果
通过从订阅中拉取 Pub/Sub 消息,验证您的 Pub/Sub 主题是否将收到来自作业的消息。
-
在 Google Cloud is 控制台中,前往 控制台中,前往 Pub / Sub 订阅页面 。
前往 Pub / Sub
-
点击您的 Pub / Sub 主题的订阅 。
-
点击消息标签页 。
-
点击拉取。
您应该会看到已发布的消息,消息正文
Hello world
以及消息的发布时间。
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。
-
删除您创建的 cron 作业 :
-
在 Google Cloud 控制台中,前往 Cloud Scheduler 页面。
转到 Cloud Scheduler
-
选中您的作业所对应的复选框。
-
点击删除并确认删除。
-
-
删除 Pub/Sub 主题:
-
在 Google Cloud 控制台中,转到 Pub / Sub 页面 。
前往 Pub / Sub
-
选中您的主题所对应的复选框 。
-
点击删除并确认删除。
-
-
删除 Pub / Sub 订阅 :
-
在 Google Cloud 控制台中,转到 Pub / Sub 页面 。
前往 Pub / Sub
-
选中您的订阅对应的复选框。
-
点击删除并确认删除。
-
-
或者,如果您为了解 Cloud Scheduler 创建了一个新项目,但现在不再需要该项目,请删除该项目:
-
In the Google Cloud console, go to the Manage resources page.
Go to Manage resources
-
In the project list, select the project that you
want to delete, and then click delete. -
In the dialog, type the project ID, and then click
Shut down to delete the project.
-
In the Google Cloud console, go to the Manage resources page.
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和 / 或其关联公司的注册商标 。
最后更新时间 ( UTC):2024 – 11 – 21 。
[[[“易于理解”,”easyToUnderstand”,”thumb-up”],[“解决了我的问题”,”solvedMyProblem”,”thumb-up”],[“其他”,”otherUp”,”thumb-up”]],[[“Hard to understand”,”hardToUnderstand”,”thumb-down”],[“Incorrect information or sample code”,”incorrectInformationOrSampleCode”,”thumb-down”],[“Missing the information/samples I need”,”missingTheInformationSamplesINeed”,”thumb-down”],[“翻译问题”,”translationIssue”,”thumb-down”],[“其他”,”otherDown”,”thumb-down”]],[“最后更新时间 ( UTC):2024 – 11 – 21 。”],[],[]]
© Copyright notes
The copyright of the article belongs to the author, please do not reprint without permission.
Related posts
No comments...