將 HTTP 目標工作新增至 Cloud Tasks 佇列

本快速入門導覽課程說明如何使用 Cloud Tasks API,將 HTTP 目標工作新增至 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. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  4. To initialize the gcloud CLI, run the following command:

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

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

  7. Enable the Cloud Tasks API:

    gcloud services enable cloudtasks.googleapis.com
  8. 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.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  9. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/cloudtasks.admin, roles/logging.viewer

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
    • Replace PROJECT_ID with your project ID.
    • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:myemail@example.com.

    • Replace ROLE with each individual role.
  10. Install the Google Cloud CLI.

  11. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  12. To initialize the gcloud CLI, run the following command:

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

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

  15. Enable the Cloud Tasks API:

    gcloud services enable cloudtasks.googleapis.com
  16. 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.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  17. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/cloudtasks.admin, roles/logging.viewer

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
    • Replace PROJECT_ID with your project ID.
    • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:myemail@example.com.

    • Replace ROLE with each individual role.

建立 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

後續步驟