在 Cloud Run 中建構及建立 Java 工作
事前準備
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
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
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
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
啟用 Cloud Run Admin API 和 Cloud Build API:
gcloud services enable run.googleapis.com \ cloudbuild.googleapis.com
啟用 Cloud Run Admin API 後,系統會自動建立 Compute Engine 預設服務帳戶。
按一下即可查看 Cloud Build 服務帳戶的必要角色
除非您覆寫這項行為,否則 Cloud Build 會自動使用 Compute Engine 預設服務帳戶做為預設 Cloud Build 服務帳戶,以便建構您的原始碼和 Cloud Run 資源。如要讓 Cloud Build 建構來源,請要求管理員將 Cloud Run 建構工具 (roles/run.builder
) 授予專案的 Compute Engine 預設服務帳戶:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=roles/run.builder
請將 PROJECT_NUMBER
替換為您的 Google Cloud專案編號,並將 PROJECT_ID
替換為您的 Google Cloud專案 ID。如需查看如何找出專案 ID 和專案編號的詳細操作說明,請參閱「建立及管理專案」。
將 Cloud Run 建構工具角色授予 Compute Engine 預設服務帳戶後,需要幾分鐘的時間才能套用。
編寫範例工作
如何使用 Java 編寫工作:
建立名為
jobs
的新目錄,然後將目錄變更為該目錄:mkdir jobs cd jobs
在名為
src/main/java/com/example
的子目錄中,為實際工作代碼建立JobsExample.java
檔案。將下列範例行複製到其中:Cloud Run 工作可讓使用者指定工作要執行的工作數量。以下程式碼範例說明如何使用內建的
CLOUD_RUN_TASK_INDEX
環境變數。每個工作都代表一個正在執行的容器副本。請注意,工作通常會並行執行。如果每個工作都能獨立處理部分資料,使用多個工作就很實用。每項工作都會知道自己的索引,並儲存在
CLOUD_RUN_TASK_INDEX
環境變數中。內建的CLOUD_RUN_TASK_COUNT
環境變數包含透過--tasks
參數在工作執行期間提供的工作數量。顯示的程式碼也說明如何使用內建的
CLOUD_RUN_TASK_ATTEMPT
環境變數重試工作,其中包含重試此工作的次數,從第一次嘗試開始,每次重試增加 1,最多可達--max-retries
。您也可以透過這段程式碼產生失敗情況,以便測試重試,並產生錯誤記錄,讓您瞭解這些記錄的樣貌。
使用以下內容建立
pom.xml
檔案:請使用以下內容建立
project.toml
檔案,以便使用 Buildpack 支援的 Java 版本進行建構:
程式碼已完成,可以封裝在容器中。
建構工作容器、將其傳送至 Artifact Registry,然後部署至 Cloud Run
重要事項:本快速入門假設您在用於快速入門的專案中具備擁有者或編輯者角色。否則,請參閱 Cloud Run 原始碼開發人員角色,瞭解從原始碼部署 Cloud Run 資源所需的權限。
本快速入門導覽課程會使用「從來源部署」功能,該功能會建構容器、上傳至 Artifact Registry,並將工作部署至 Cloud Run:
gcloud run jobs deploy job-quickstart \ --source . \ --tasks 50 \ --set-env-vars SLEEP_MS=10000 \ --set-env-vars FAIL_RATE=0.1 \ --max-retries 5 \ --region REGION \ --project=PROJECT_ID
其中 PROJECT_ID 是您的專案 ID,REGION 則是您的區域,例如 us-central1
。請注意,您可以將各種參數變更為要用於測試的任何值。SLEEP_MS
會模擬工作,而 FAIL_RATE
會導致 X
% 的工作失敗,讓您嘗試並行處理和重試失敗的工作。
在 Cloud Run 中執行工作
如要執行剛建立的工作,請按照下列步驟操作:
gcloud run jobs execute job-quickstart --region REGION
將 REGION 替換為您在建立及部署工作時使用的地區,例如 us-central1
。
後續步驟
如要進一步瞭解如何從程式碼來源建構容器,並推送至存放區,請參閱: