安排 HTTP Cloud Functions 函数


本教程介绍如何使用 Cloud Scheduler 以及如何定位 HTTP 端点来触发 HTTP Cloud Functions 函数。如果您需要函数具有网址端点并响应 HTTP 请求(例如对于 webhook),请使用 HTTP 函数。

请注意,如果您的函数应直接触发以响应 Google Cloud 项目中的事件(例如 Pub/Sub 主题中收到消息或 Cloud Storage 存储桶中发生更改),您也可以安排事件驱动型函数。如需了解详情,请参阅 Cloud Functions 函数的类型

安排 Cloud Function 的执行是 Cloud Scheduler 的常见用例。在此教程中,您将学习以下操作:

  1. 创建一个简单的 HTTP Cloud Functions 函数。
  2. 创建 Cloud Scheduler 作业。
  3. 运行 Cloud Scheduler 作业。
  4. 验证 Cloud Scheduler 作业是否触发了该 Cloud Function。

费用

在本文档中,您将使用 Google Cloud 的以下收费组件:

您可使用价格计算器根据您的预计使用情况来估算费用。 Google Cloud 新用户可能有资格申请免费试用

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 启用 Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler API。

    启用 API

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. 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.

    6. Click Continue.
    7. Click Done to finish creating the service account.

  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. 确保您的 Google Cloud 项目已启用结算功能

  8. 启用 Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler API。

    启用 API

  9. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. 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.

    4. Click Create and continue.
    5. 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.

    6. Click Continue.
    7. Click Done to finish creating the service account.

  10. 请注意,您必须授予 Cloud Run Invoker 角色,因为在 Cloud Functions(第 2 代)中,可通过管理底层 Cloud Run 服务获得调用权限。

  11. 默认情况下,Cloud Functions 会使用为其运行时服务帐号自动创建的默认 Compute Engine 服务帐号。您可以使用此服务帐号试用本教程。但是,根据您的组织政策配置,系统可能不会自动向默认服务帐号授予项目的 Editor 角色。在这种情况下,您必须向服务帐号授予以下角色:
    1. Artifact Registry Writer (roles/artifactregistry.writer)
    2. 日志写入者 (roles/logging.logWriter)
    3. Storage Object Viewer (roles/storage.objectViewer)

创建 HTTP Cloud Functions 函数

创建一个由 Cloud Scheduler 触发的 HTTP 函数。

  1. 在 Google Cloud 控制台中,转到 Cloud Functions 页面。

    转到 Cloud Functions

  2. 点击 创建函数

  3. 基本信息部分中,执行以下操作:

    1. 环境列表中,选择第 2 代
    2. 输入函数的名称。
    3. 区域列表中,选择一个区域。
  4. 触发器部分中,执行以下操作:

    1. 触发器类型列表中,选择 HTTPS
    2. 对于身份验证,请选择需要身份验证

      如需了解详情,请参阅进行身份验证以进行调用(第 2 代)

  5. 接受其他默认设置,然后点击下一步

  6. 请勿修改默认运行时语言和示例代码。此代码不使用任何云服务,也不需要启用其他权限。点击部署

  7. 函数部署成功后,点击触发器标签页。

  8. 复制函数的 HTTP 网址。

创建 Cloud Scheduler 作业

创建一个使用 HTTP 触发操作的 Cloud Scheduler 作业。

  1. 在 Google Cloud 控制台中,转到 Cloud Scheduler 页面。

    转到 Cloud Scheduler

  2. 点击 创建作业

  3. 输入作业的名称。

  4. 区域列表中,选择一个区域。

  5. 使用 unix-cron 格式指定作业的频率

    30 16 * * 7
    

    如需了解详情,请参阅 Cron 作业格式和时区

  6. 时区列表中,选择一个时区。

  7. 点击继续

  8. 目标类型列表中,选择 HTTP

  9. 网址 字段中,输入您之前复制的函数的 HTTP 网址。

  10. Auth header 列表中,选择 Add OIDC token

  11. 服务帐号列表中,选择您之前创建的服务帐号。

  12. 受众群体字段中,输入您之前复制的函数的 HTTP 网址。

  13. 点击创建

您已经创建了一个 Cron 作业,该作业会在星期日的 16:30 运行您的 Cloud Function。

运行 Cloud Scheduler 作业

您现在可以运行自己创建的作业了。

  1. 在 Google Cloud Console 中,前往 Cloud Scheduler 页面。

    转到 Cloud Scheduler

  2. 选中您创建的作业对应的复选框,然后点击强制运行

    初次调用时,项目中创建的第一个作业可能需要几分钟时间进行配置和运行。

  3. 作业运行后,Status of last execution(上次执行状态)应指示 Success

验证 Cloud Functions 中的结果

您可以验证 Cron 作业是否成功触发并执行 Cloud Function。

  1. 在 Google Cloud 控制台中,转到 Cloud Functions 页面。

    转到 Cloud Functions

  2. 点击函数名称。

    函数详细信息页面将打开,每秒调用次数图表显示对函数的第一次调用。

  3. 点击日志标签页。

    您应该会看到一条类似于 POST 200 146 B 5 ms Google-Cloud-Scheduler https://REGION-PROJECT_ID.cloudfunctions.net/FUNCTION_NAME 的日志条目。

清理

为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。

删除项目

  1. 在 Google Cloud 控制台中,进入管理资源页面。

    转到“管理资源”

  2. 在项目列表中,选择要删除的项目,然后点击删除
  3. 在对话框中输入项目 ID,然后点击关闭以删除项目。

删除教程资源

  1. 在 Google Cloud 控制台中,转到 Cloud Scheduler 页面。

    转到 Cloud Scheduler

  2. 选择作业旁边的复选框。

  3. 点击 删除并确认删除。

  4. 在 Google Cloud 控制台中,转到 Cloud Functions 页面。

    转到 Cloud Functions

  5. 选中您的函数旁边的复选框。

  6. 点击 删除并确认删除。

  7. 在 Google Cloud 控制台中,转到服务账号页面。

    转到“服务账号”

  8. 选中您创建的服务帐号旁边的复选框。

  9. 点击 删除并确认删除。

后续步骤