使用 Java (第 1 代) 建立及部署 HTTP Cloud Run 函式
本指南將逐步說明如何使用 Java 執行階段編寫 Cloud Run 函式。Cloud Run 函式分為兩種類型:
- HTTP 函式,可從標準 HTTP 要求叫用。
- 事件驅動函式:用於處理 Cloud 基礎架構中的事件,例如 Pub/Sub 主題上的訊息,或 Cloud Storage 值區中的變更。
本文說明如何建立簡單的 HTTP 函式,並使用 Maven 或 Gradle 建構函式。
事前準備
- 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. Roles required to select or create a project - Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 
      Create a project: To create a project, you need the Project Creator
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Cloud Functions and Cloud Build APIs. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 
    
    
      In the Google Cloud console, on the project selector page, select or create a Google Cloud project. Roles required to select or create a project - Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 
      Create a project: To create a project, you need the Project Creator
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Cloud Functions and Cloud Build APIs. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 安裝並初始化 Google Cloud SDK。
- 更新並安裝 gcloud元件:gcloud components update 
- 準備開發環境。
- 在本機系統上為函式程式碼建立目錄: - Linux 或 Mac OS X: - mkdir ~/helloworld cd ~/helloworld- Windows: - mkdir %HOMEPATH%\helloworld cd %HOMEPATH%\helloworld
- 建立專案結構,內含來源目錄和來源檔案。 - mkdir -p src/main/java/functions touch src/main/java/functions/HelloWorld.java
- 將下列內容新增至 - HelloWorld.java檔案:- 這個範例函式會輸出問候語「Hello World!」。 
- 在本機系統上為函式程式碼建立目錄: - Linux 或 Mac OS X: - mkdir ~/helloworld-gradle cd ~/helloworld-gradle- Windows: - mkdir %HOMEPATH%\helloworld-gradle cd %HOMEPATH%\helloworld-gradle
- 建立專案結構,內含來源目錄和來源檔案。 - mkdir -p src/main/java/functions touch src/main/java/functions/HelloWorld.java
- 將下列內容新增至 - HelloWorld.java檔案:- 這個範例函式會輸出問候語「Hello World!」。 
- 函式完成部署時,請記下 - httpsTrigger.url屬性,或使用下列指令找到這個屬性:- gcloud functions describe my-first-function - https://GCP_REGION-PROJECT_ID.cloudfunctions.net/my-first-function 
- 透過您的瀏覽器造訪這個網址。畫面上應會顯示 - Hello World!訊息。
建立函式
本節說明如何建立函式。
Maven
Gradle
指定依附元件
下一步是設定依附元件:
Maven
將目錄變更為您在上方建立的 helloworld 目錄,然後建立 pom.xml 檔案:
 cd ~/helloworld
 touch pom.xml
如要使用 Maven 管理依附元件,請在專案的 pom.xml 檔案中,指定 <dependencies> 區段內的依附元件。在本練習中,請將下列內容複製到 pom.xml 檔案。
如需以 Maven 為基礎的完整範例,請參閱 helloworld。
Gradle
將目錄變更為您在上方建立的 helloworld-gradle 目錄,然後建立 build.gradle 檔案:
 cd ~/helloworld-gradle
 touch build.gradle
如要使用 Gradle 管理依附元件,請在專案的 build.gradle 檔案中指定依附元件。在本練習中,請將下列內容複製到 build.gradle 檔案。請注意,這個 build.gradle 檔案包含自訂工作,可協助您在本機執行函式。
如需以 Gradle 為基礎的完整範例,請參閱 helloworld-gradle。
在本機建構及測試
部署函式前,您可以在本機建構及測試函式:
Maven
執行下列指令,確認函式是否已建構完成:
mvn compile
您也可以使用 mvn package 指令編譯 Java 程式碼、執行任何測試,然後將程式碼封裝在目標目錄中的 JAR 檔案內。如要進一步瞭解 Maven 建構生命週期,請參閱這篇文章。
如要測試函式,請執行下列指令:
mvn function:run
Gradle
執行下列指令,確認函式是否已建構完成:
gradle build
如要測試函式,請執行下列指令:
gradle runFunction -Prun.functionTarget=functions.HelloWorld
如果測試順利完成,系統會顯示網址,您可以在網路瀏覽器中造訪該網址,查看函式運作情形:
http://localhost:8080/。畫面上應會顯示 Hello World! 訊息。
或者,您也可以透過其他終端機視窗使用 curl,傳送要求至此函式:
curl localhost:8080
# Output: Hello World!
部署函式
Maven
如要使用 HTTP 觸發條件部署函式,請在 helloworld 目錄中執行下列指令:
gcloud functions deploy my-first-function --no-gen2 --entry-point functions.HelloWorld --runtime java17 --trigger-http --memory 512MB --allow-unauthenticated
my-first-function 是註冊名稱,函式會透過這個名稱在 Google Cloud 控制台中識別,而 --entry-point 則指定函式的完整類別名稱 (FQN)。Gradle
如要使用 HTTP 觸發條件部署函式,請在 helloworld-gradle 目錄中執行下列指令:
gcloud functions deploy my-first-function --no-gen2 --entry-point functions.HelloWorld --runtime java17 --trigger-http --memory 512MB --allow-unauthenticated
my-first-function 是註冊名稱,函式會透過這個名稱在 Google Cloud 控制台中識別,而 --entry-point 則指定函式的完整類別名稱 (FQN)。測試已部署的函式
查看記錄
您可以使用 Google Cloud CLI,以及在 Cloud Logging UI 中查看 Cloud Run functions 的記錄。
使用指令列工具
如要透過 gcloud CLI 查看函式的記錄檔,請使用 logs read 指令加上函式的名稱:
gcloud functions logs read my-first-function
輸出應會如下所示:
LEVEL NAME EXECUTION_ID TIME_UTC LOG D my-first-function k2bqgroszo4u 2020-07-24 18:18:01.791 Function execution started D my-first-function k2bqgroszo4u 2020-07-24 18:18:01.958 Function execution took 168 ms, finished with status code: 200 ...
使用記錄資訊主頁
您也可以從Google Cloud 控制台查看 Cloud Run functions 的記錄。