将 HTTP 目标任务添加到 Cloud Tasks 队列

本快速入门介绍了如何使用 Cloud Tasks API 向 Cloud Tasks 队列添加 HTTP 目标任务

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  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. 确保您的 Google Cloud 项目已启用结算功能

  6. Enable the Cloud Tasks API:

    gcloud services enable 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. 确保您的 Google Cloud 项目已启用结算功能

  12. Enable the Cloud Tasks API:

    gcloud services enable 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.

创建 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 队列添加任务

使用 gcloud tasks create-http-task 命令创建以 HTTP 端点为目标的任务,并将该任务添加到队列中。

  1. 创建任务,将其添加到您创建的队列中,然后将该任务交付给 HTTP 端点。

    gcloud tasks create-http-task \
        --queue=QUEUE_NAME \
        --url=URL_PATH \
        --method=GET \
        --location=REGION \
        --project=PROJECT_ID
    

    URL_PATH 替换为请求将发送到的完整网址路径。例如 https://www.google.com

    路径必须以 http://https:// 开头。

  2. 通过读取日志来验证任务是否已成功执行。

    gcloud logging read --limit=3
    

    日志应类似于以下内容:

    jsonPayload:
    '@type': type.googleapis.com/google.cloud.tasks.logging.v1.TaskActivityLog
    task: projects/PROJECT_ID/locations/REGION/queues/QUEUE_NAME/tasks/TASK_ID
    taskCreationLog:
       scheduleTime: '2024-07-04T19:00:27.801837Z'
       status: OK
       targetAddress: GET https://www.google.com/
       targetType: HTTP
    

清理

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

Delete a Google Cloud project:

gcloud projects delete PROJECT_ID

或者,您也可以删除 Cloud Tasks 队列:

gcloud tasks queues delete QUEUE_NAME \
    --location=REGION

后续步骤