將 App Engine 工作新增至 Cloud Tasks 佇列
本快速入門導覽課程說明如何使用 Cloud Tasks API,將 App Engine 工作新增至 Cloud Tasks 佇列。
事前準備
- 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager and Cloud Tasks API:
gcloud services enable cloudresourcemanager.googleapis.com tasks.googleapis.com
-
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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager and Cloud Tasks API:
gcloud services enable cloudresourcemanager.googleapis.com tasks.googleapis.com
-
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.
- 使用 App Engine 時,系統會自動建立 App Engine 預設服務帳戶。試用本快速入門導覽課程時,可以使用這個服務帳戶。不過,根據機構政策設定,系統可能不會自動將專案的
Editor
角色授予預設服務帳戶。如果是這種情況,您必須授予服務帳戶下列角色:- Artifact Registry 管理員 (
roles/artifactregistry.admin
) - Artifact Registry 推送建立寫入者 (
roles/artifactregistry.createOnPushWriter
) - Compute 管理員 (
roles/compute.admin
) - 記錄寫入者 (
roles/logging.logWriter
) - Storage 物件檢視者 (
roles/storage.objectViewer
)
- Artifact Registry 管理員 (
新增 App Engine 應用程式
如果您要以 App Engine 工作為目標,且必須先將應用程式部署到 App Engine 標準環境,請先將 App Engine 應用程式新增至專案。
前往 Google Cloud 控制台的「App Engine」頁面。
在「Welcome to App Engine」(歡迎使用 App Engine) 對話方塊中,執行下列任一操作:
如果您已建立 App Engine 應用程式,且系統顯示「Your App Engine application has been created」(已建立 App Engine 應用程式) 訊息,則可略過本節的其餘步驟,直接按照「安裝及部署範例」一節的步驟操作。
或
如果您尚未建立 App Engine 應用程式,請按一下「建立應用程式」,然後繼續完成本節的其餘步驟。
選取應用程式的地區,並記下所選地區。
請注意,在 Cloud Tasks 指令中,
europe-west
和us-central
分別稱為europe-west1
和us-central1
。請勿選取服務帳戶,系統會使用預設的 App Engine 服務帳戶。
點選「下一步」。
應用程式已設定並建立完成。這可能需要幾分鐘的時間。
請勿下載 Cloud SDK,而是點選「I'll do this later」(稍後再執行)。
您應該會看到「已建立 App Engine 應用程式」訊息。
安裝及部署範例
本快速入門導覽課程使用的 Node.js 範例包含兩個檔案:createTask.js
會在本機執行,做為指令列工具,用於建立工作並新增至 Tasks 佇列;server.js
則會部署在 App Engine 上,做為工作站服務來處理工作。
在終端機中,將範例應用程式存放區複製到本機電腦。
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
前往包含範例程式碼的目錄。
cd nodejs-docs-samples/cloud-tasks/snippets
使用 Node.js 套件管理工具安裝所有依附元件。
你可以使用 NPM:
npm install
或者,您可以使用 Yarn:
yarn install
將工作站服務 (
server.js
) 部署到 App Engine 標準環境。gcloud app deploy app.yaml
確認包含該服務的應用程式運作正常。
gcloud app browse
在瀏覽器中前往提供的連結。例如:
https://PROJECT_ID.uc.r.appspot.com/
畫面上應會顯示
Hello, World!
。
建立 Cloud Tasks 佇列
使用 gcloud tasks queues create
指令,在您準備的環境中建立佇列。
在終端機中,建立記錄所有作業的佇列。
gcloud tasks queues create QUEUE_NAME \ --log-sampling-ratio=1.0 \ --location=REGION
更改下列內容:
QUEUE_NAME
:Cloud Tasks 佇列的名稱REGION
:部署應用程式的區域
等待佇列初始化完成,然後確認佇列已建立成功。
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 佇列
建立工作,將工作新增到您建立的佇列,然後將工作遞送到工作站服務。
設定下列環境變數。用戶端會利用這項資訊建立要求。
export PROJECT_ID=PROJECT_ID export LOCATION_ID=REGION export QUEUE_ID=QUEUE_NAME
建立酬載為
hello
的工作,並將工作新增到佇列中。酬載可以是要求中的任何資料,而工作站服務需要這些資料才能處理工作。node createTask.js $PROJECT_ID $QUEUE_ID $LOCATION_ID hello
顯示工作站服務的記錄,確認工作是否已執行。
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
或者,您也可以刪除建立的資源:
刪除 Cloud Tasks 佇列:
gcloud tasks queues delete QUEUE_NAME \ --location=REGION