本教程介绍如何使用 Cloud Scheduler 并以 Pub/Sub 主题为目标来触发事件驱动型 Cloud Run 函数。如果您的函数应直接触发以响应项目中的事件(例如 Pub/Sub 主题中收到消息或 Cloud Storage 存储桶发生更改),请使用事件驱动型函数。 Google Cloud
请注意,您还可以通过以函数的 HTTP 端点为目标来安排 HTTP 函数 。如果您需要函数具有网址端点并响应 HTTP 请求(例如针对 webhook),可使用 HTTP 函数。如需了解详情,请参阅 Cloud Run 函数的类型。
Cloud Scheduler 的常见用例之一是能够安排 Cloud Run 函数的执行。在本教程中,您将学习以下操作:
- 创建一个订阅 Pub/Sub 主题的简单 Cloud Run 函数。
- 创建一个 Cloud Scheduler 作业,以将消息发布到 Pub/Sub 主题。
- 运行 Cloud Scheduler 作业。
- 验证 Cloud Run 函数是否由 Cloud Scheduler 作业触发。
费用
在本文档中,您将使用的以下收费组件: Google Cloud
您可使用 价格计算器 根据您的预计使用情况来估算费用。
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手, 请创建一个账号来评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler, Eventarc APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart. - Click Create and continue.
-
Grant the Cloud Run > Cloud Run Invoker role to the service account.
To grant the role, find the Select a role list, then select Cloud Run > Cloud Run Invoker.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler, Eventarc APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart. - Click Create and continue.
-
Grant the Cloud Run > Cloud Run Invoker role to the service account.
To grant the role, find the Select a role list, then select Cloud Run > Cloud Run Invoker.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
- 默认情况下,Cloud Run functions 使用自动创建的
默认 Compute Engine 服务账号
作为其运行时服务帐号。您可以使用此服务帐号来试用本教程。不过,根据您的组织政策配置,默认服务帐号可能不会自动获得项目的 Editor 角色。如果是这种情况,您必须
向该服务账号
授予以下角色:
- Artifact Registry Writer (
roles/artifactregistry.writer) - Logs Writer (
roles/logging.logWriter) - Storage Object Viewer (
roles/storage.objectViewer)
- Artifact Registry Writer (
请注意,您必须授予 Cloud Run Invoker 角色,因为 在 Cloud Run functions(第 2 代)中, 您可以通过管理底层 Cloud Run 服务来获取调用权限。
创建事件驱动型 Cloud Run 函数
创建一个事件驱动型函数,该函数应直接触发以响应项目中的事件 ;在本例中,该事件是指发布到 Pub/Sub 主题的消息。 Google Cloud
在 Google Cloud 控制台中,前往 Cloud Run functions 页面。
点击 创建函数。
在基本信息 部分中,执行以下操作:
- 在环境 列表中,选择第 2 代 。
- 输入函数的名称。
- 在区域 列表中,选择一个区域。
在触发器 部分中,执行以下操作:
- 在触发器类型 列表中,选择 Cloud Pub/Sub 。
- 在 Cloud Pub/Sub 主题 列表中,选择现有主题,或者点击创建主题 以创建新主题。记下主题的名称,因为您将在后续步骤中用到它。
点击更多选项 。
Eventarc 触发器 面板随即打开。
在 Eventarc 触发器 面板中,执行以下操作:
- 如果系统提示您允许 Pub/Sub 服务创建调用函数所需的身份令牌,请点击授予 。
- 在服务账号 列表中,选择您之前创建的服务帐号。
- 接受其他默认设置。
点击保存触发器 。
Eventarc 触发器 面板随即关闭。
接受其他默认设置,然后点击下一步 。
请勿修改默认运行时语言和示例代码。该代码不使用任何云服务,也不需要启用其他权限。 点击部署 。
创建 Cloud Scheduler 作业
创建一个以 Pub/Sub 为目标的 Cloud Scheduler 作业。
在 Google Cloud 控制台中,前往 Cloud Scheduler 页面。
点击 Click 创建作业 。
输入作业的名称。
在区域 列表中,选择一个区域。
使用 unix-cron 格式指定作业的频率 :
30 16 * * 7如需了解详情,请参阅 Cron 作业格式和时区。
在时区 列表中,选择一个时区。
点击继续 。
在目标类型 列表中,选择 Pub/Sub 。
选择您之前创建的 Pub/Sub 主题。
在消息正文 字段中,输入一条要发送到您的 Pub/Sub 目标主题的字符串。例如:“Hello world!”
点击创建 。
您已创建一个 cron 作业,该作业会在周日 16:30 向您的 Pub/Sub 主题发送一条消息。您的 Cloud Run 函数已订阅该主题。
运行 Cloud Scheduler 作业
您现在可以运行您创建的作业。
在 Google Cloud 控制台中,前往 Cloud Scheduler 页面。
选中您创建的作业对应的复选框,然后点击强制运行 。
首次调用时,在项目中创建的第一个作业可能需要几分钟才能配置和运行。
作业运行后,上次执行的状态 应显示
Success。
验证 Cloud Run functions 中的结果
您可以验证您的 Cloud Run 函数是否由 cron 作业成功触发和执行。
在 Google Cloud 控制台中,前往 Cloud Run functions 页面。
点击函数名称。
函数详情 页面随即打开,每秒调用次数 图表显示了您首次调用该函数的情况。
点击日志 标签页。
您应该会看到类似
Hello, YOUR_STRING!的日志条目
清理
为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。
删除项目
- 在 Google Cloud 控制台中,前往 管理资源 页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击 关闭以删除项目。
删除教程资源
在 Google Cloud 控制台中,前往 Cloud Scheduler 页面。
选中作业旁边的复选框。
点击 删除 并确认您要删除此存储分区。
在 Google Cloud 控制台中,前往 Pub/Sub 页面。
选中主题旁边的复选框。
点击 删除 并确认您要删除此存储分区。
在 Google Cloud 控制台中,前往 Cloud Run functions 页面。
选中函数旁边的复选框。
点击 删除 并确认您要删除此存储分区。
在 Google Cloud 控制台中,前往服务账号页面。
选中您创建的服务帐号旁边的复选框。
点击 删除 并确认您要删除此存储分区。