设置您的环境

在安排和运行 Cron 作业之前,您必须 Google Cloud为 Cloud Scheduler 设置项目和环境。

启用 Cloud Scheduler API

按照以下说明创建或选择 Google Cloud 项目并启用 Cloud Scheduler API。

控制台

  1. 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.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Scheduler API.

    Enable the API

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

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Scheduler API.

    Enable the API

gcloud

  1. 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.
  2. Install the Google Cloud CLI.
  3. To initialize the gcloud CLI, run the following command:

    gcloud init
  4. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  5. Make sure that billing is enabled for your Google Cloud project.

  6. Enable the Cloud Scheduler API:

    gcloud services enable scheduler.googleapis.com
  7. Install the Google Cloud CLI.
  8. To initialize the gcloud CLI, run the following command:

    gcloud init
  9. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  10. Make sure that billing is enabled for your Google Cloud project.

  11. Enable the Cloud Scheduler API:

    gcloud services enable scheduler.googleapis.com

设置身份验证

如何向 Cloud Scheduler 进行身份验证取决于您用于访问 API 的界面以及运行代码的环境。

如需了解详情,请参阅对 Cloud Scheduler 进行身份验证对 HTTP 目标使用身份验证

添加 App Engine 应用

如果您要使用 App Engine HTTP target,则必须向项目添加 App Engine 应用。此应用既是 Cloud Scheduler 服务的位置,也是作业处理程序本身的位置,它会执行与 Cloud Scheduler 作业相关的工作,并返回确认回复。

如果您希望使用在项目中的 App Engine 作业处理程序,则应改为选择 HTTP 目标。在这种情况下,您无需在项目中添加 App Engine 应用。

控制台

  1. 在 Google Cloud 控制台中,前往 App Engine 页面。

    前往 App Engine

  2. 欢迎使用 App Engine 对话框中,执行以下操作之一:

    • 如果您已创建 App Engine 应用,并且系统显示了 Your App Engine application has been created 消息,则可以跳过本部分中的其余步骤。

    • 如果您尚未创建 App Engine 应用,请点击创建应用,然后继续执行本部分中的其余步骤。

  3. 为您的应用选择一个区域

    请注意,europe-westus-central 在 Cloud Scheduler 命令中分别称为 europe-west1us-central1

    为 App Engine 应用设置区域后,您无法更改它

  4. 请勿选择服务账号;系统会使用默认的 App Engine 服务账号。

  5. 点击下一步

    应用已配置并创建。此过程可能需要几分钟的时间。

  6. 请勿下载 Cloud SDK,而是点击以后再说

    您应该会看到一条消息,告知您 您的 App Engine 应用已创建

gcloud

  1. 如需创建 App Engine 应用,请运行 gcloud app create 命令:

    gcloud app create --region=REGION
    

    REGION 替换为应用运行的位置。请注意,europe-westus-central 在 Cloud Scheduler 命令中分别称为 europe-west1us-central1

    为 App Engine 应用设置区域后,您无法更改它

  2. 您可以使用以下命令检查现有应用的区域:

    gcloud app describe
    

    locationId 表示区域。例如:locationId: us-central