<dependencies>
+ <dependency>
+ <groupId>org.springframework.cloud</groupId>
+ <artifactId>spring-cloud-function-adapter-gcp</artifactId>
+ </dependency>
+
+ ...
+</dependencies>
+diff --git a/reference/html/spring-cloud-function.html b/reference/html/spring-cloud-function.html index 3fa5c578e..4f3be3f59 100644 --- a/reference/html/spring-cloud-function.html +++ b/reference/html/spring-cloud-function.html @@ -1496,20 +1496,14 @@ to use. The next section will explain you how you can accomplish just that.
The adapter has a couple of generic request handlers that you can use. The most generic is (and the one we used in the Getting Started section)
-is org.springframework.cloud.function.adapter.aws.FunctionInvoke which is the implementation of AWS’s RequestStreamHandler.
+is org.springframework.cloud.function.adapter.aws.FunctionInvoker which is the implementation of AWS’s RequestStreamHandler.
User doesn’t need to do anything other then specify it as 'handler' on AWS dashborad when deploying function.
It will handle most of the case including Kinesis, streaming etc. .
The most generic is
-SpringBootStreamHandler, which uses a Jackson ObjectMapper provided by Spring Boot to serialize and deserialize the objects
-in the function. There is also a SpringBootRequestHandler which you can extend, and provide the input and output types as type
-parameters (enabling AWS to inspect the class and do the JSON conversions itself).
If your app has more than one @Bean of type Function etc. then you can choose the one to use by configuring function.name
-(e.g. as FUNCTION_NAME environment variable in AWS). The functions are extracted from the Spring Cloud FunctionCatalog
-(searching first for Function then Consumer and finally Supplier).
If your app has more than one @Bean of type Function etc. then you can choose the one to use by configuring spring.cloud.function.definition
+property or environment variable. The functions are extracted from the Spring Cloud FunctionCatalog. In the event you don’t specify spring.cloud.function.definition
+the framework will attempt to find a default following the search order where it searches first for Function then Consumer and finally Supplier).
@Bean
You can either run the function locally using the open source Google Functions Framework for Java or on GCP.
Start by adding the spring-cloud-function-adapter-gcp dependency to your project.
<dependencies>
+ <dependency>
+ <groupId>org.springframework.cloud</groupId>
+ <artifactId>spring-cloud-function-adapter-gcp</artifactId>
+ </dependency>
+
+ ...
+</dependencies>
+In addition, add the spring-boot-maven-plugin which will build the JAR of the function to deploy.
| + + | +
+Notice that we also reference spring-cloud-function-adapter-gcp as a dependency of the spring-boot-maven-plugin. This is necessary because it modifies the plugin to package your function in the correct JAR format for deployment on 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>
+Finally, add the Maven plugin provided as part of the Google Functions Framework for Java.
+This allows you to test your functions locally via mvn function:run.
| + + | +
+The function target should always be set to org.springframework.cloud.function.adapter.gcp.GcfJarLauncher; this is an adapter class which acts as the entry point to your Spring Cloud Function from the Google Cloud Functions platform.
+ |
+
<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>
+A full example of a working pom.xml can be found in the Spring Cloud Functions GCP sample.
Google Cloud Functions supports deploying HTTP Functions, which are functions that are invoked by HTTP request. The sections below describe instructions for deploying a Spring Cloud Function as an HTTP Function.
+Let’s start with a simple Spring Cloud Function example:
Start by adding the Maven plugin provided as part of the Google Functions Framework for Java.
-<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>
-Specify your configuration main class in resources/META-INF/MANIFEST.MF.
Then run the function:
+Then run the function locally.
+This is provided by the Google Cloud Functions function-maven-plugin described in the project dependencies section.
In order to use the adapter, first add the dependency to your pom.xml:
-<dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-function-adapter-gcp</artifactId>
-</dependency>
-Then, add the spring-boot-maven-plugin with spring-cloud-function-adapter-gcp as a dependency.
-The extra dependency is used for spring-boot-maven-plugin to package your function in the correct JAR format for deployment on 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>
-Package the application.
+Start by packaging your application.
spring-boot-maven-plugin to packag
You should see the resulting JAR in target/deploy directory.
+
If you added the custom spring-boot-maven-plugin plugin defined above, you should see the resulting JAR in target/deploy directory.
This JAR is correctly formatted for deployment to Google Cloud Functions.
Make sure that you have the Cloud SDK CLI installed.
+Next, make sure that you have the Cloud SDK CLI installed.
From the project base directory run the following command to deploy.
gcloud alpha functions deploy function-sample-gcp \ +gcloud alpha functions deploy function-sample-gcp-http \ --entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \ --runtime java11 \ --trigger-http \ @@ -2080,15 +2112,176 @@ This JAR is correctly formatted for deployment to Google Cloud Functions.
curl https://REGION-PROJECT_ID.cloudfunctions.net/function-sample-gcp -d "hello"+
curl https://REGION-PROJECT_ID.cloudfunctions.net/function-sample-gcp-http -d "hello"
Go to the function-sample-gcp to try out a sample function that you can test locally or deploy to GCP.
+Google Cloud Functions also supports deploying Background Functions which are invoked indirectly in response to an event, such as a message on a Cloud Pub/Sub topic, a change in a Cloud Storage bucket, or a Firebase event.
+The spring-cloud-function-adapter-gcp allows for functions to be deployed as background functions as well.
The sections below describe the process for writing a Cloud Pub/Sub topic background function. +However, there are a number of different event types that can trigger a background function to execute which are not discussed here; these are described in the Background Function triggers documentation.
+Let’s start with a simple Spring Cloud Function which will run as a GCF background function:
+@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());
+ }
+}
+In addition, create PubSubMessage class in the project with the below definition.
+This class represents the Pub/Sub event structure which gets passed to your function on a Pub/Sub topic event.
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;
+ }
+
+}
+Specify your configuration main class in resources/META-INF/MANIFEST.MF.
Main-Class: com.example.BackgroundFunctionMain
+Then run the function locally.
+This is provided by the Google Cloud Functions function-maven-plugin described in the project dependencies section.
mvn function:run+
Invoke the HTTP function:
+curl localhost:8080 -H "Content-Type: application/json" -d '{"data":"hello"}'
+Verify that the function was invoked by viewing the logs.
+In order to deploy your background function to GCP, first package your application.
+mvn package+
If you added the custom spring-boot-maven-plugin plugin defined above, you should see the resulting JAR in target/deploy directory.
+This JAR is correctly formatted for deployment to Google Cloud Functions.
Next, make sure that you have the Cloud SDK CLI installed.
+From the project base directory run the following command to deploy.
+gcloud alpha 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 will now invoke the function every time a message is published to the topic specified by --trigger-topic.
For a walkthrough on testing and verifying your background function, see the instructions for running the GCF Background Function sample.
+The project provides the following sample functions as reference:
+The function-sample-gcp-http is an HTTP Function which you can test locally and try deploying.
+The function-sample-gcp-background shows an example of a background function that is triggered by a message being published to a specified Pub/Sub topic.
+