本教程介绍了如何触发事件驱动型 Cloud Function 使用 Cloud Scheduler 并定位 Pub/Sub 主题。使用 事件驱动型函数,而您的函数应直接在 Google Cloud 项目中的事件(例如, Pub/Sub 主题或 Cloud Storage 存储桶中的更改。
请注意,您也可以安排 HTTP 函数 方法是定位函数的 HTTP 端点在以下情况下使用 HTTP 函数: 您需要让函数具有网址端点并响应 HTTP 请求, Webhook如需了解详情,请参阅 Cloud Functions 的类型。
安排 Cloud Function 执行是一种常见的做法, Cloud Scheduler 的使用场景。在此教程中,您将学习以下操作:
- 创建一个订阅 Pub/Sub 主题的简单 Cloud Functions 函数。
- 创建一个 Cloud Scheduler 作业,该作业将消息发布到 Pub/Sub 主题。
- 运行 Cloud Scheduler 作业。
- 验证 Cloud Function 是否由 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.
-
启用 Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler, Eventarc API。
-
Create a service account:
-
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.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
启用 Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler, Eventarc API。
-
Create a service account:
-
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.
-
- 默认情况下,Cloud Functions 函数会使用自动创建的
默认的 Compute Engine 服务账号
。您可以使用此服务账号来试用
本教程。不过,根据您的组织政策配置,
默认服务账号可能无法自动获得
自己的项目如果是这种情况,
向服务账号授予
以下角色:
<ph type="x-smartling-placeholder">
- </ph>
- Artifact Registry Writer (
roles/artifactregistry.writer
) - 日志写入者 (
roles/logging.logWriter
) - Storage Object Viewer (
roles/storage.objectViewer
)
- Artifact Registry Writer (
请注意,您必须授予 Cloud Run Invoker 角色,因为 在 Cloud Functions (第 2 代) 中, 可通过管理底层 API 来获得调用权限 Cloud Run 服务。
创建事件驱动型 Cloud Function
创建可直接响应事件时触发的事件驱动型函数 您的 Google Cloud 项目中;在本示例中,发布到 Pub/Sub 主题。
在 Google Cloud 控制台中,前往 Cloud Functions。 页面。
点击
创建函数。在基本信息部分中,执行以下操作:
- 在环境列表中,选择第 2 代。
- 输入函数的名称。
- 在区域列表中,选择一个区域。
在触发器部分中,执行以下操作:
- 在触发器类型列表中,选择 Cloud Pub/Sub。
- 在 Cloud Pub/Sub 主题列表中,选择现有主题或 要创建新主题,请点击创建主题。请注意主题的名称为 您在后续步骤中会用到它。
点击更多选项。
系统随即会打开 Eventarc 触发器面板。
在 Eventarc 触发器面板中,执行以下操作:
- 如果系统提示您允许 Pub/Sub 服务创建 调用函数所需的身份令牌,请点击授权。
- 在服务账号列表中,选择您要 凭据。
- 接受其他默认设置。
点击保存触发器。
Eventarc 触发器面板随即会关闭。
接受其他默认设置,然后点击下一步。
请勿修改默认运行时语言和示例代码。代码的作用 不使用任何云服务,也不需要启用其他权限。 点击部署。
创建 Cloud Scheduler 作业
创建具有 Pub/Sub 目标的 Cloud Scheduler 作业。
在 Google Cloud 控制台中,转到 Cloud Scheduler 页面。
点击
创建作业。输入作业的名称。
在区域列表中,选择一个区域。
使用 unix-cron 格式指定作业的频率:
30 16 * * 7
如需了解详情,请参阅 Cron 作业格式和时区。
在时区列表中,选择一个时区。
点击继续。
在目标类型列表中,选择 Pub/Sub。
选择您之前创建的 Pub/Sub 主题。
在邮件正文字段中,输入要发送到 Pub/Sub 目标主题。例如:“Hello world!”
点击创建。
您已创建一个 Cron 作业,它可向您的 Pub/Sub 发送消息 搜索主题。您的 Cloud Functions 函数订阅了这项服务 主题。
运行 Cloud Scheduler 作业
您现在可以运行自己创建的作业了。
在 Google Cloud Console 中,前往 Cloud Scheduler 页面。
选中您创建的作业对应的复选框,然后点击强制运行。
初次调用时,项目中创建的第一个作业可能需要 只需几分钟即可配置和运行。
作业运行后,Status of last execution(上次执行状态)应指示
Success
.
验证 Cloud Functions 中的结果
您可以验证 Cloud Function 是否已成功触发 由 Cron 作业执行
在 Google Cloud 控制台中,转到 Cloud Functions 页面。
点击函数名称。
函数详情页面打开,并显示了每秒调用次数图表 显示对该函数的第一次调用。
点击日志标签页。
您应该会看到一条类似于
Hello, YOUR_STRING!
的日志条目
清理
为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。
删除项目
- 在 Google Cloud 控制台中,进入管理资源页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击关闭以删除项目。
删除教程资源
在 Google Cloud 控制台中,转到 Cloud Scheduler 页面。
选择作业旁边的复选框。
点击
删除,然后确认您的 删除。在 Google Cloud 控制台中,前往 Pub/Sub 页面。
选中您的主题旁边的复选框。
点击
删除,然后确认您的 删除。在 Google Cloud 控制台中,转到 Cloud Functions 页面。
选中您的函数旁边的复选框。
点击
删除,然后确认您的 删除。在 Google Cloud 控制台中,转到服务账号页面。
选中您创建的服务账号旁边的复选框。
点击
删除,然后确认您的 删除。