本教程介绍了如何在 Cloud Scheduler 中以 HTTP 端点为目标来安排 HTTP Cloud Run 函数。如果您需要函数具有网址端点并响应 HTTP 请求(例如针对 webhook),可使用 HTTP 函数。
或者,如果您创建了 CloudEvents 函数,则可以使用 Eventarc 事件触发器安排函数响应项目中的事件。Eventarc 可以触发 Pub/Sub 主题中的消息、Cloud Storage 存储分区中的更改等。如需了解详情,请参阅 Cloud Run 函数类型。
安排 Cloud Run 函数的执行是 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 products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.
-
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 following roles to the service account: Cloud Run > Cloud Run Invoker, Cloud Run > Cloud Run Builder.
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click
Add another role and add each additional role. - 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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Cloud Functions, Cloud Run Admin, Cloud Scheduler APIs.
-
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 following roles to the service account: Cloud Run > Cloud Run Invoker, Cloud Run > Cloud Run Builder.
To grant a role, find the Select a role list, then select the role.
To grant additional roles, click
Add another role and add each additional role. - Click Continue.
-
Click Done to finish creating the service account.
-
请注意,您必须授予 Cloud Run Invoker 角色,因为在 Cloud Run 函数中,您可以通过管理底层 Cloud Run 服务来获取调用权限。
创建 HTTP Cloud Run 函数
创建一个由 Cloud Scheduler 触发的 HTTP 函数。
在 Google Cloud 控制台中,转到 Cloud Run 页面。
点击编写函数。
在配置部分,执行以下操作:
- 在服务名称字段中,输入函数的名称。
- 在区域列表中,选择一个区域。
- 请勿修改默认运行时语言。
对于身份验证,请选择需要进行身份验证。
如果您通过网域限制组织政策来限制项目的未经身份验证的调用,则您需要按照测试专用服务中的说明访问已部署的服务。
如需了解详情,请参阅进行身份验证以便调用。
接受其他默认值,然后点击创建。
复制该函数的 HTTP 网址。
创建 Cloud Scheduler 作业
创建使用 HTTP 触发操作的 Cloud Scheduler 作业。
在 Google Cloud 控制台中,转到 Cloud Scheduler 页面。
点击
创建作业。输入作业的名称。
在区域列表中,选择一个区域。
使用 unix-cron 格式指定作业的频率:
30 16 * * 7
如需了解详情,请参阅 Cron 作业格式和时区。
在时区列表中,选择一个时区。
点击继续。
在目标类型列表中,选择 HTTP。
在 网址 字段中,输入您之前复制的函数的 HTTP 网址。
在身份验证标头列表中,选择添加 OIDC 令牌。
在服务账号列表中,选择您之前创建的服务账号。
在受众群体字段中,输入您之前复制的函数的 HTTP 网址。
点击创建。
您创建了一个 Cron 作业,用于在周日 16:30 运行 Cloud Run 函数。
运行 Cloud Scheduler 作业
现在,您可以运行自己创建的作业了。
在 Google Cloud Console 中,前往 Cloud Scheduler 页面。
选中您创建的作业对应的复选框,然后点击强制运行。
在项目中创建的第一项作业在首次调用时,可能需要几分钟才能配置和运行。
作业运行后,上次执行的状态应显示为
Success
。
验证 Cloud Run functions 中的结果
您可以验证您的 Cloud Run 函数是否已成功被 cron 作业触发并执行。
在 Google Cloud 控制台中,转到 Cloud Run 页面。
依次选择过滤服务 > 部署类型 > 函数。这会过滤服务列表,使其仅显示函数。
点击函数名称。
系统会打开服务详情页面,并显示服务指标。
点击日志标签页。
您应会看到类似如下的日志条目:
POST 200 146 B 5 ms Google-Cloud-Scheduler https://FUNCTION_NAME
清理
为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。
删除项目
- In the Google Cloud console, go to the Manage resources page.
- 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.
删除教程资源
在 Google Cloud 控制台中,转到 Cloud Scheduler 页面。
选择作业旁边的复选框。
点击
Delete(删除),然后确认删除操作。在 Google Cloud 控制台中,转到 Cloud Run 页面。
选中相应服务旁边的复选框。
点击
Delete(删除),然后确认删除。在 Google Cloud 控制台中,转到服务账号页面。
选中您创建的服务账号旁边的复选框。
点击
Delete(删除),然后确认删除操作。
后续步骤
- 使用 Cloud Scheduler 按计划触发 Cloud Run
- 编写 CloudEvents 函数,以便安排函数响应项目中的事件。