使用 Functions Framework 執行函式

Cloud Run 函式會使用開放原始碼的 Functions Framework 程式庫,將部署的函式包裝在持續性 HTTP 應用程式中。

Functions Framework 也可在支援該語言的任何其他平台上執行,包括本機、內部部署伺服器、Compute EngineCloud Run

安裝依附元件

在函式的目錄中,安裝您語言的 Functions Framework 程式庫:

詳情請參閱 Java Functions Framework 程式庫

Node.js

npm install --save-dev @google-cloud/functions-framework

Python

pip install functions-framework

Go

go install github.com/GoogleCloudPlatform/functions-framework-go/funcframework

Java

Maven

如果您使用 Maven,請將下列指令新增到 pom.xml 檔案中:

<dependency>
  <groupId>com.google.cloud.functions</groupId>
  <artifactId>functions-framework-api</artifactId>
  <version>1.1.0</version>
  <scope>provided</scope>
</dependency>

Gradle

如果您使用 Gradle,請將下列指令新增到 build.gradle 檔案中:

dependencies {
  // Every function needs this dependency to get the Functions Framework API.
  compileOnly 'com.google.cloud.functions:functions-framework-api:1.1.0'

  // To run function locally using Functions Framework's local invoker
  invoker 'com.google.cloud.functions.invoker:java-function-invoker:1.3.1'
}

C#

下列指令會使用 .NET 範本,以 .NET Functions Framework 程式庫做為依附元件,建立新的.NET Cloud Run 函式程式碼集:

# HTTP functions
dotnet new gcf-http

# CloudEvent functions
dotnet new gcf-event

Ruby

在 Ruby 中,您必須將 Functions Framework 新增至函式的依附元件,才能將函式部署至 Cloud Run 函式:

bundle add functions_framework

PHP

composer require google/cloud-functions-framework

設定 Functions Framework

使用 Functions Framework 執行函式前,您需要先指定要執行的函式類型和名稱。這些屬性可以指定為指令列介面 (CLI) 旗標或環境變數。

支援的函式類型

Functions Framework 支援 Cloud Run functions 第 1 代支援的所有三種函式。所有語言執行階段都支援 http,以及 eventcloudevent 其中一種。

函式類型 簽章類型 說明 支援的執行階段
HTTP 觸發函式 http 接收及回應 HTTP 要求的函式。 所有執行階段
背景函式 event Cloud Run functions 專屬事件格式 Node.jsPythonGoJava
CloudEvent 函式 cloudevent 業界標準活動格式 .NETRubyPHP

指定要執行的函式

使用 Functions Framework 執行函式前,您必須先指定要執行的程式碼函式。在大多數語言中,您可以指定目標函式的方法名稱,如下表所示。(例外狀況包括 Java.NET,詳情請見下表。)

各語言的操作說明

如需各語言支援的設定選項清單,請參閱下表。

Node.js

CLI 引數 環境變數 說明
--port PORT 要接聽要求的通訊埠。(預設值:8080)
--target FUNCTION_TARGET 要叫用的 exported 函式名稱。(預設值: function)
--signature-type FUNCTION_SIGNATURE_TYPE 函式使用的簽章類型。可以是 http (預設值)、eventcloudevent

Python

CLI 引數 環境變數 說明
--port PORT 要接聽要求的通訊埠。(預設值:8080)
--target FUNCTION_TARGET 要叫用的 exported 函式名稱。(預設值: function)
--signature-type FUNCTION_SIGNATURE_TYPE 函式使用的簽章類型。可以是 http (預設值)、eventcloudevent

Go

環境變數 說明
PORT 要接聽要求的通訊埠。(預設值:8080)

Java

引數名稱 環境變數 說明
run.port PORT 要接聽要求的通訊埠。(預設值:8080)
run.functionTarget FUNCTION_TARGET 要叫用的 exported 函式名稱。(預設值: function)

C#

CLI 引數 環境變數 說明
--port PORT 要接聽要求的通訊埠。(預設值:8080)
--target (或僅限引數) FUNCTION_TARGET 要叫用函式的 classname。(預設值: function)

Ruby

CLI 引數 環境變數 說明
--port PORT 要接聽要求的通訊埠。(預設值:8080)
--target FUNCTION_TARGET 要叫用的 exported 函式名稱。(預設值: function)

PHP

環境變數 說明
FUNCTION_TARGET 要叫用的函式名稱。(預設值: function)
FUNCTION_SIGNATURE_TYPE 函式使用的簽章類型。可以是 http (預設值)、eventcloudevent

請按照下列操作說明設定及執行 Functions Framework:

Node.js

Node.js Functions Framework 可讓您以指令列引數或環境變數的形式,指定函式的名稱和簽章類型。

您也可以在 package.json 建構檔案中指定這些值,方法是新增 start 指令碼,並加入必要的 CLI 引數,如下例所示。

"scripts": {
  "start": "npx functions-framework --target=YOUR_FUNCTION_NAME [--signature-type=YOUR_SIGNATURE_TYPE]"
}

您也可以使用環境變數執行相同操作:

"scripts": {
  "start": "FUNCTION_TARGET=YOUR_FUNCTION_NAME FUNCTION_SIGNATURE_TYPE=YOUR_SIGNATURE_TYPE npx functions-framework"
}

YOUR_FUNCTION_NAME 替換為函式的方法名稱,並將 YOUR_SIGNATURE_TYPE (如適用) 替換為函式的簽章型別,如上表所示。

Python

Python Functions Framework 可讓您將函式的名稱和簽章類型指定為指令列引數或環境變數執行架構時,必須指定指令列引數。

Go

Go 函式架構會使用 funcframework.RegisterHTTPFunctionContext 指定函式目標和簽章類型。

Java

Java Functions Framework 會從三個不同來源接受設定資料,優先順序如下 (從最明確到最不明確):

  • 指令列引數
  • Buildfiles
  • 環境變數

指令列引數

Maven

如要指定要執行的函式,請在 mvn 指令中加入下列指令列介面 (CLI) 標記:

-Drun.functionTarget=YOUR_FUNCTION_NAME

您也可以加入下列 CLI 標記,以類似方式指定目標通訊埠:

-Drun.port=12345

Gradle

Gradle 的 CLI 標記與 Maven 的標記幾乎相同。Gradle 唯一會進行的變更,就是將每個旗標開頭的 -D 換成 -P,如下例所示:

# Maven version
-Drun.functionTarget=...

# Gradle version
-Prun.functionTarget=...

Buildfiles

您也可以在專案的建構檔案中指定要執行的函式。Maven 和 Gradle 的 CLI 標記類似,但建構檔案子句的差異很大。

Maven

Maven 建構檔案名為 pom.xml,請將下列子句新增至這個檔案,指定目標函式:

<plugin>
  <groupId>com.google.cloud.functions</groupId>
  <artifactId>function-maven-plugin</artifactId>
  <version>0.11.0</version>
  <configuration>
    <functionTarget>functions.HelloWorld</functionTarget>
  </configuration>
</plugin>

請將 <functionTarget> 改成函式的類別名稱。(舉例來說,套件 functions 中的函式,如果類別名稱為 HelloCloudFunctions,則類別名稱會是 functions.HelloCloudFunctions。這與父項建構檔案 (pom.xmlbuild.gradle) 相關。

Gradle

Gradle 建構檔案名為 build.gradle,請在這個檔案中加入下列子句,指定目標函式:

// Register a "runFunction" task to run the function locally
tasks.register("runFunction", JavaExec) {
  main = 'com.google.cloud.functions.invoker.runner.Invoker'
  classpath(configurations.invoker)
  inputs.files(configurations.runtimeClasspath, sourceSets.main.output)
  args(
    '--target', project.findProperty('run.functionTarget') ?: '',
    '--port', project.findProperty('run.port') ?: 8080
  )
  doFirst {
    args('--classpath', files(configurations.runtimeClasspath, sourceSets.main.output).asPath)
  }
}

C#

如果您使用 dotnet new 和上述其中一個範本建立專案,.NET Functions Framework 會自動偵測函式。

如果專案包含多個函式,請參閱「執行架構」一節,瞭解如何執行特定函式。

Ruby

Ruby Functions Framework 可讓您以指令列引數或環境變數的形式,指定函式的名稱和簽章類型。執行架構時,必須指定指令列引數。

PHP

PHP Functions Framework 可讓您將環境變數指定為指令列引數。

您也可以在 composer.json 建構檔案中指定這些值,方法是新增 start 指令碼,如下方範例所示。

"scripts": {
   "start": [
       "Composer\\Config::disableProcessTimeout",
       "FUNCTION_TARGET=YOUR_FUNCTION_NAME php -S localhost:${PORT:-8080} vendor/bin/router.php"
    ]
}

YOUR_FUNCTION_NAME 替換為函式名稱,並將 YOUR_SIGNATURE_TYPE (如適用) 替換為函式的簽章類型,如上表所示。

執行 Functions Framework

使用下列指令,透過 Functions Framework 執行函式。 除非您明確指定 PORT 值,否則函式預設會透過 localhost:8080 存取。

Node.js

npm start

Python

使用指令列引數:

functions-framework --target=YOUR_FUNCTION_NAME

使用環境變數:

FUNCTION_TARGET=YOUR_FUNCTION_NAME functions-framework

YOUR_FUNCTION_NAME 替換為函式的方法名稱。

Go

cd cmd
go build
./cmd

使用環境變數:

cd cmd
go build
PORT=8080 ./cmd

Java

Maven

使用下列指令執行 pom.xml 中指定的函式:

mvn function:run

使用下列指令執行指令列引數中指定的函式:

mvn function:run -Drun.functionTarget=YOUR_FUNCTION_NAME

使用下列指令執行指定為環境變數的函式:

FUNCTION_TARGET=YOUR_FUNCTION_NAME mvn function:run

YOUR_FUNCTION_NAME 替換為函式的類別名稱。

Gradle

使用下列指令執行 build.gradle 中指定的函式:

./gradlew runFunction

使用下列指令執行指令列引數中指定的函式:

./gradlew runFunction -Prun.functionTarget=YOUR_FUNCTION_NAME

使用下列指令執行指定為環境變數的函式:

FUNCTION_TARGET=YOUR_FUNCTION_NAME ./gradlew runFunction

YOUR_FUNCTION_NAME 替換為函式的類別名稱。

C#

如果目前的 .NET 專案中只有一個函式,請使用下列指令執行函式。(這是範本建立專案的預設結構)。

dotnet run

如果 .NET 專案包含多個函式,請使用下列指令執行特定函式。將 YOUR_FUNCTION_CLASSNAME 替換為函式的類別名稱,包括命名空間。

dotnet run YOUR_FUNCTION_CLASSNAME

如要同時執行多個函式,必須執行多個 Functions Framework 執行個體。為避免執行中的架構執行個體發生衝突,每個執行個體都應使用不同的 PORT 值。以下指令顯示如何使用 PORT8080 執行函式。

使用指令列引數:

dotnet run --target YOUR_FUNCTION_CLASSNAME --port 8080

使用環境變數:

FUNCTION_TARGET=YOUR_FUNCTION_CLASSNAME PORT=8080 dotnet run

Ruby

使用指令列引數:

bundle exec functions-framework-ruby --target YOUR_FUNCTION_NAME

使用環境變數:

FUNCTION_TARGET=YOUR_FUNCTION_NAME bundle exec functions-framework-ruby

YOUR_FUNCTION_NAME 替換為函式的方法名稱。

PHP

export FUNCTION_TARGET=YOUR_FUNCTION_NAME
php -S localhost:8080 vendor/bin/router.php

YOUR_FUNCTION_NAME 替換為函式的名稱。

呼叫函式

如需與在本機執行的函式互動的操作說明,請參閱「呼叫本機函式」頁面。

清除所用資源

函式執行完畢後,請按下 Control+C,停止執行中的架構執行個體。