将 App Engine 任务添加到 Cloud Tasks 队列

本快速入门介绍了如何使用 Cloud Tasks API 将 App Engine 任务添加到 Cloud Tasks 队列。

准备工作

  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 Resource Manager and Cloud Tasks API:

    gcloud services enable cloudresourcemanager.googleapis.com tasks.googleapis.com
  7. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

  8. Install the Google Cloud CLI.
  9. To initialize the gcloud CLI, run the following command:

    gcloud init
  10. 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.

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

  12. Enable the Cloud Resource Manager and Cloud Tasks API:

    gcloud services enable cloudresourcemanager.googleapis.com tasks.googleapis.com
  13. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

  14. 当您使用 App Engine 时,系统会自动创建App Engine 默认服务账号。您可以在试用本快速入门时使用此服务账号。不过,默认服务账号可能不会自动获得项目的 Editor 角色,具体取决于您的组织政策配置。如果是这种情况,您必须向服务账号授予以下角色:
    1. Artifact Registry Administrator (roles/artifactregistry.admin)
    2. Artifact Registry Create-on-Push Writer (roles/artifactregistry.createOnPushWriter)
    3. Compute Admin (roles/compute.admin)
    4. Logs Writer (roles/logging.logWriter)
    5. Storage Object Viewer (roles/storage.objectViewer)

添加 App Engine 应用

在定位到 App Engine 任务后,您必须先向项目添加 App Engine 应用,然后才能将应用部署到 App Engine 标准环境。

  1. 在 Google Cloud 控制台中,进入 App Engine 页面。

    前往 App Engine

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

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

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

  3. 为您的应用选择一个区域,并记下该区域。

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

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

  5. 点击下一步

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

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

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

安装和部署示例

本快速入门中使用的 Node.js 示例应用包含两个文件:createTask.js 作为命令行工具在本地运行,用于创建任务并将任务添加到 Tasks 队列;server.js 作为工作器服务部署在 App Engine 上,用于处理任务。

  1. 在终端中,将示例应用代码库克隆到本地机器。

    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
    
  2. 前往包含示例代码的目录。

    cd nodejs-docs-samples/cloud-tasks/snippets
    
  3. 使用 Node.js 软件包管理器安装所有依赖项。

    您可以使用 NPM 执行以下操作:

    npm install
    

    或者,您也可以使用 Yarn:

    yarn install
    
  4. 将工作器服务 (server.js) 部署到 App Engine 标准环境。

    gcloud app deploy app.yaml
    
  5. 确保包含该服务的应用正在运行。

    gcloud app browse
    
  6. 在浏览器中,前往所提供的链接。例如:

    https://PROJECT_ID.uc.r.appspot.com/
    

    您应该会看到 Hello, World! 显示。

创建 Cloud Tasks 队列

使用 gcloud tasks queues create 命令在您准备好的环境中创建队列。

  1. 在终端中,创建一个用于记录所有操作的队列。

    gcloud tasks queues create QUEUE_NAME \
        --log-sampling-ratio=1.0 \
        --location=REGION
    

    替换以下内容:

    • QUEUE_NAME:Cloud Tasks 队列的名称
    • REGION:您部署应用所在的区域
  2. 等待队列初始化,然后验证队列是否已成功创建。

    gcloud tasks queues describe QUEUE_NAME \
        --location=REGION
    

    输出应类似如下所示:

     name: projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_NAME
     rateLimits:
       maxBurstSize: 100
       maxConcurrentDispatches: 1000
       maxDispatchesPerSecond: 500.0
     retryConfig:
       maxAttempts: 100
       maxBackoff: 3600s
       maxDoublings: 16
       minBackoff: 0.100s
     state: RUNNING
    

向 Cloud Tasks 队列添加任务

创建任务,将其添加到您创建的队列,然后将该任务提交给工作器服务。

  1. 设置以下环境变量。客户端使用此信息来创建请求。

    export PROJECT_ID=PROJECT_ID
    export LOCATION_ID=REGION
    export QUEUE_ID=QUEUE_NAME
    
  2. 使用载荷 hello 创建任务,并将该任务添加到队列中。负载可以是工作器服务处理任务所需的请求中的任何数据。

    node createTask.js $PROJECT_ID $QUEUE_ID $LOCATION_ID hello
    
  3. 通过显示工作器服务的日志来验证是否已执行任务。

    gcloud app logs read
    

    日志应如下所示:

    2024-06-20 15:00:00 default[20240620t143852]  "POST /log_payload HTTP/1.1" 200
    2024-06-20 15:00:00 default[20240620t143852]  App listening on port 8081
    2024-06-20 15:00:00 default[20240620t143852]  Press Ctrl+C to quit.
    2024-06-20 15:00:00 default[20240620t143852]  Received task with payload: hello
    

清理

为避免因本页面中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的 Google Cloud 项目。

Delete a Google Cloud project:

gcloud projects delete PROJECT_ID

或者,您也可以删除您创建的资源:

  1. 删除 Cloud Tasks 队列:

    gcloud tasks queues delete QUEUE_NAME \
        --location=REGION
    
  2. 停用 App Engine 应用

后续步骤