Google Cloud Functions

Google Cloud Functions 适配器使 Spring Cloud Function 應用程式能夠在 Google Cloud Functions 無伺服器平台上執行。您可以選擇在本機使用開放原始碼的 Google Functions Framework for Java 或在 GCP 上執行函式。

專案依賴性

首先將 spring-cloud-function-adapter-gcp 依賴性新增至您的專案。

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-function-adapter-gcp</artifactId>
	</dependency>

	...
</dependencies>

此外,新增 spring-boot-maven-plugin,它將建置要部署之函式的 JAR 檔。

請注意,我們也將 spring-cloud-function-adapter-gcp 引用為 spring-boot-maven-plugin 的依賴性。這是必要的,因為它修改了外掛程式,以正確的 JAR 格式封裝您的函式,以便在 Google Cloud Functions 上部署。
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<configuration>
		<outputDirectory>target/deploy</outputDirectory>
	</configuration>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-function-adapter-gcp</artifactId>
		</dependency>
	</dependencies>
</plugin>

最後,新增作為 Google Functions Framework for Java 一部分提供的 Maven 外掛程式。這可讓您透過 mvn function:run 在本機測試您的函式。

函式目標應始終設定為 org.springframework.cloud.function.adapter.gcp.GcfJarLauncher;這是一個适配器類別,作為從 Google Cloud Functions 平台到您的 Spring Cloud Function 的進入點。
<plugin>
	<groupId>com.google.cloud.functions</groupId>
	<artifactId>function-maven-plugin</artifactId>
	<version>0.9.1</version>
	<configuration>
		<functionTarget>org.springframework.cloud.function.adapter.gcp.GcfJarLauncher</functionTarget>
		<port>8080</port>
	</configuration>
</plugin>

Spring Cloud Functions GCP 範例中可以找到可運作的 pom.xml 完整範例。

HTTP 函式

Google Cloud Functions 支援部署 HTTP 函式,這些函式由 HTTP 請求調用。以下章節說明將 Spring Cloud Function 部署為 HTTP 函式的指示。

開始使用

讓我們從一個簡單的 Spring Cloud Function 範例開始

@SpringBootApplication
public class CloudFunctionMain {

	public static void main(String[] args) {
		SpringApplication.run(CloudFunctionMain.class, args);
	}

	@Bean
	public Function<String, String> uppercase() {
		return value -> value.toUpperCase();
	}
}

resources/META-INF/MANIFEST.MF 中指定您的組態主要類別。

Main-Class: com.example.CloudFunctionMain

然後在本機執行函式。這由專案依賴性章節中描述的 Google Cloud Functions function-maven-plugin 提供。

mvn function:run

調用 HTTP 函式

curl http://localhost:8080/ -d "hello"

建置與部署至 GCP

首先封裝您的應用程式。

mvn package

如果您新增了上面定義的自訂 spring-boot-maven-plugin 外掛程式,您應該會在 target/deploy 目錄中看到產生的 JAR 檔。此 JAR 檔的格式正確,可部署到 Google Cloud Functions。

接下來,請確保您已安裝 Cloud SDK CLI

從專案根目錄執行以下命令進行部署。

gcloud functions deploy function-sample-gcp-http \
--entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \
--runtime java11 \
--trigger-http \
--source target/deploy \
--memory 512MB

調用 HTTP 函式

curl https://REGION-PROJECT_ID.cloudfunctions.net/function-sample-gcp-http -d "hello"

設定自訂 HTTP statusCode

Functions can specify a custom HTTP response code by setting the `FunctionInvoker.HTTP_STATUS_CODE` header.
@Bean
public Function<String, Message<String>> function() {

	String payload = "hello";

	Message<String> message = MessageBuilder.withPayload(payload).setHeader(FunctionInvoker.HTTP_STATUS_CODE, 404).build();

	return input -> message;
};

背景函式

Google Cloud Functions 也支援部署 背景函式,這些函式會因應事件間接調用,例如 Cloud Pub/Sub 主題上的訊息、Cloud Storage 儲存桶中的變更或 Firebase 事件。

spring-cloud-function-adapter-gcp 也允許將函式部署為背景函式。

以下章節描述了撰寫 Cloud Pub/Sub 主題背景函式的流程。但是,有許多不同類型的事件可以觸發背景函式執行,此處未討論這些事件;這些事件在背景函式觸發條件文件中進行了描述。

GCP 開始使用

讓我們從一個簡單的 Spring Cloud Function 開始,它將作為 GCF 背景函式執行

@SpringBootApplication
public class BackgroundFunctionMain {

	public static void main(String[] args) {
		SpringApplication.run(BackgroundFunctionMain.class, args);
	}

	@Bean
	public Consumer<PubSubMessage> pubSubFunction() {
		return message -> System.out.println("The Pub/Sub message data: " + message.getData());
	}
}

此外,在專案中建立具有以下定義的 PubSubMessage 類別。此類別代表 Pub/Sub 事件結構,該結構會在 Pub/Sub 主題事件上傳遞到您的函式。

public class PubSubMessage {

	private String data;

	private Map<String, String> attributes;

	private String messageId;

	private String publishTime;

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public Map<String, String> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, String> attributes) {
		this.attributes = attributes;
	}

	public String getMessageId() {
		return messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	public String getPublishTime() {
		return publishTime;
	}

	public void setPublishTime(String publishTime) {
		this.publishTime = publishTime;
	}

}

resources/META-INF/MANIFEST.MF 中指定您的組態主要類別。

Main-Class: com.example.BackgroundFunctionMain

然後在本機執行函式。這由專案依賴性章節中描述的 Google Cloud Functions function-maven-plugin 提供。

mvn function:run

調用 HTTP 函式

curl localhost:8080 -H "Content-Type: application/json" -d '{"data":"hello"}'

透過檢視日誌,驗證函式是否已調用。

部署至 GCP

為了將您的背景函式部署到 GCP,請先封裝您的應用程式。

mvn package

如果您新增了上面定義的自訂 spring-boot-maven-plugin 外掛程式,您應該會在 target/deploy 目錄中看到產生的 JAR 檔。此 JAR 檔的格式正確,可部署到 Google Cloud Functions。

接下來,請確保您已安裝 Cloud SDK CLI

從專案根目錄執行以下命令進行部署。

gcloud functions deploy function-sample-gcp-background \
--entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \
--runtime java11 \
--trigger-topic my-functions-topic \
--source target/deploy \
--memory 512MB

現在,每次將訊息發佈到 --trigger-topic 指定的主題時,Google Cloud Function 都會調用該函式。

如需測試和驗證背景函式的逐步指南,請參閱執行 GCF 背景函式範例 的指示。

範例函式

專案提供以下範例函式作為參考