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;這是一個介面卡類,作為 Spring Cloud Function 從 Google Cloud Functions 平臺的入口點。
<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>

一個完整的工作 pom.xml 示例可以在 Spring Cloud Functions GCP 示例中找到。

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 https://: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 狀態碼

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

Google Cloud Function 現在將在每次訊息釋出到 --trigger-topic 指定的主題時呼叫該函式。

有關測試和驗證後臺函式的詳細說明,請參閱執行 GCF 後臺函式示例的說明。

示例函式

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

© . This site is unofficial and not affiliated with VMware.