Sync docs from master to gh-pages
This commit is contained in:
@@ -1496,20 +1496,14 @@ to use. The next section will explain you how you can accomplish just that.</p>
|
||||
<h4 id="_aws_request_handlers"><a class="link" href="#_aws_request_handlers">AWS Request Handlers</a></h4>
|
||||
<div class="paragraph">
|
||||
<p>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 <code>org.springframework.cloud.function.adapter.aws.FunctionInvoke</code> which is the implementation of AWS’s <code>RequestStreamHandler</code>.
|
||||
is <code>org.springframework.cloud.function.adapter.aws.FunctionInvoker</code> which is the implementation of AWS’s <code>RequestStreamHandler</code>.
|
||||
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. .</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>The most generic is
|
||||
<code>SpringBootStreamHandler</code>, which uses a Jackson <code>ObjectMapper</code> provided by Spring Boot to serialize and deserialize the objects
|
||||
in the function. There is also a <code>SpringBootRequestHandler</code> 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).</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>If your app has more than one <code>@Bean</code> of type <code>Function</code> etc. then you can choose the one to use by configuring <code>function.name</code>
|
||||
(e.g. as <code>FUNCTION_NAME</code> environment variable in AWS). The functions are extracted from the Spring Cloud <code>FunctionCatalog</code>
|
||||
(searching first for <code>Function</code> then <code>Consumer</code> and finally <code>Supplier</code>).</p>
|
||||
<p>If your app has more than one <code>@Bean</code> of type <code>Function</code> etc. then you can choose the one to use by configuring <code>spring.cloud.function.definition</code>
|
||||
property or environment variable. The functions are extracted from the Spring Cloud <code>FunctionCatalog</code>. In the event you don’t specify <code>spring.cloud.function.definition</code>
|
||||
the framework will attempt to find a default following the search order where it searches first for <code>Function</code> then <code>Consumer</code> and finally <code>Supplier</code>).</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect3">
|
||||
@@ -1946,7 +1940,94 @@ The Azure sample app is written in the "non-functional" style (using <code>@Bean
|
||||
You can either run the function locally using the open source <a href="https://github.com/GoogleCloudPlatform/functions-framework-java">Google Functions Framework for Java</a> or on GCP.</p>
|
||||
</div>
|
||||
<div class="sect3">
|
||||
<h4 id="_getting_started_3"><a class="link" href="#_getting_started_3">Getting Started</a></h4>
|
||||
<h4 id="_project_dependencies"><a class="link" href="#_project_dependencies">Project Dependencies</a></h4>
|
||||
<div class="paragraph">
|
||||
<p>Start by adding the <code>spring-cloud-function-adapter-gcp</code> dependency to your project.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml"><dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-function-adapter-gcp</artifactId>
|
||||
</dependency>
|
||||
|
||||
...
|
||||
</dependencies></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>In addition, add the <code>spring-boot-maven-plugin</code> which will build the JAR of the function to deploy.</p>
|
||||
</div>
|
||||
<div class="admonitionblock note">
|
||||
<table>
|
||||
<tr>
|
||||
<td class="icon">
|
||||
<i class="fa icon-note" title="Note"></i>
|
||||
</td>
|
||||
<td class="content">
|
||||
Notice that we also reference <code>spring-cloud-function-adapter-gcp</code> as a dependency of the <code>spring-boot-maven-plugin</code>. This is necessary because it modifies the plugin to package your function in the correct JAR format for deployment on Google Cloud Functions.
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml"><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></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Finally, add the Maven plugin provided as part of the Google Functions Framework for Java.
|
||||
This allows you to test your functions locally via <code>mvn function:run</code>.</p>
|
||||
</div>
|
||||
<div class="admonitionblock note">
|
||||
<table>
|
||||
<tr>
|
||||
<td class="icon">
|
||||
<i class="fa icon-note" title="Note"></i>
|
||||
</td>
|
||||
<td class="content">
|
||||
The function target should always be set to <code>org.springframework.cloud.function.adapter.gcp.GcfJarLauncher</code>; this is an adapter class which acts as the entry point to your Spring Cloud Function from the Google Cloud Functions platform.
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml"><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></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>A full example of a working <code>pom.xml</code> can be found in the <a href="https://github.com/spring-cloud/spring-cloud-function/blob/master/spring-cloud-function-samples/function-sample-gcp-http/pom.xml">Spring Cloud Functions GCP sample</a>.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect3">
|
||||
<h4 id="_http_functions"><a class="link" href="#_http_functions">HTTP Functions</a></h4>
|
||||
<div class="paragraph">
|
||||
<p>Google Cloud Functions supports deploying <a href="https://cloud.google.com/functions/docs/writing/http">HTTP Functions</a>, which are functions that are invoked by HTTP request. The sections below describe instructions for deploying a Spring Cloud Function as an HTTP Function.</p>
|
||||
</div>
|
||||
<div class="sect4">
|
||||
<h5 id="_getting_started_3"><a class="link" href="#_getting_started_3">Getting Started</a></h5>
|
||||
<div class="paragraph">
|
||||
<p>Let’s start with a simple Spring Cloud Function example:</p>
|
||||
</div>
|
||||
@@ -1966,24 +2047,6 @@ public class CloudFunctionMain {
|
||||
}</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect4">
|
||||
<h5 id="_test_locally"><a class="link" href="#_test_locally">Test locally</a></h5>
|
||||
<div class="paragraph">
|
||||
<p>Start by adding the Maven plugin provided as part of the Google Functions Framework for Java.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml"><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></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Specify your configuration main class in <code>resources/META-INF/MANIFEST.MF</code>.</p>
|
||||
</div>
|
||||
@@ -1993,7 +2056,8 @@ public class CloudFunctionMain {
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Then run the function:</p>
|
||||
<p>Then run the function locally.
|
||||
This is provided by the Google Cloud Functions <code>function-maven-plugin</code> described in the project dependencies section.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
@@ -2016,39 +2080,7 @@ public class CloudFunctionMain {
|
||||
You can get on the <a href="https://docs.google.com/forms/d/e/1FAIpQLScC98jGi7CfG0n3UYlj7Xad8XScvZC8-BBOg7Pk3uSZx_2cdQ/viewform">whitelist</a> to try it out.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>In order to use the adapter, first add the dependency to your pom.xml:</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml"><dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-function-adapter-gcp</artifactId>
|
||||
</dependency></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Then, add the <code>spring-boot-maven-plugin</code> with <code>spring-cloud-function-adapter-gcp</code> as a dependency.
|
||||
The extra dependency is used for <code>spring-boot-maven-plugin</code> to package your function in the correct JAR format for deployment on Google Cloud Functions.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml"><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></code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Package the application.</p>
|
||||
<p>Start by packaging your application.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
@@ -2056,18 +2088,18 @@ The extra dependency is used for <code>spring-boot-maven-plugin</code> to packag
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>You should see the resulting JAR in <code>target/deploy</code> directory.
|
||||
<p>If you added the custom <code>spring-boot-maven-plugin</code> plugin defined above, you should see the resulting JAR in <code>target/deploy</code> directory.
|
||||
This JAR is correctly formatted for deployment to Google Cloud Functions.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Make sure that you have the <a href="https://cloud.google.com/sdk/install">Cloud SDK CLI</a> installed.</p>
|
||||
<p>Next, make sure that you have the <a href="https://cloud.google.com/sdk/install">Cloud SDK CLI</a> installed.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>From the project base directory run the following command to deploy.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre>gcloud alpha functions deploy function-sample-gcp \
|
||||
<pre>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.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre>curl https://REGION-PROJECT_ID.cloudfunctions.net/function-sample-gcp -d "hello"</pre>
|
||||
<pre>curl https://REGION-PROJECT_ID.cloudfunctions.net/function-sample-gcp-http -d "hello"</pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect3">
|
||||
<h4 id="_sample_function"><a class="link" href="#_sample_function">Sample Function</a></h4>
|
||||
<h4 id="_background_functions"><a class="link" href="#_background_functions">Background Functions</a></h4>
|
||||
<div class="paragraph">
|
||||
<p>Go to the <a href="https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-samples/function-sample-gcp/">function-sample-gcp</a> to try out a sample function that you can test locally or deploy to GCP.</p>
|
||||
<p>Google Cloud Functions also supports deploying <a href="https://cloud.google.com/functions/docs/writing/background">Background Functions</a> which are invoked indirectly in response to an event, such as a message on a <a href="https://cloud.google.com/pubsub">Cloud Pub/Sub</a> topic, a change in a <a href="https://cloud.google.com/storage">Cloud Storage</a> bucket, or a <a href="https://firebase.google.com/">Firebase</a> event.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>The <code>spring-cloud-function-adapter-gcp</code> allows for functions to be deployed as background functions as well.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>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 <a href="https://cloud.google.com/functions/docs/calling">Background Function triggers documentation</a>.</p>
|
||||
</div>
|
||||
<div class="sect4">
|
||||
<h5 id="_getting_started_4"><a class="link" href="#_getting_started_4">Getting Started</a></h5>
|
||||
<div class="paragraph">
|
||||
<p>Let’s start with a simple Spring Cloud Function which will run as a GCF background function:</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@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());
|
||||
}
|
||||
}</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>In addition, create <code>PubSubMessage</code> class in the project with the below definition.
|
||||
This class represents the <a href="https://cloud.google.com/functions/docs/calling/pubsub#event_structure">Pub/Sub event structure</a> which gets passed to your function on a Pub/Sub topic event.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">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;
|
||||
}
|
||||
|
||||
}</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Specify your configuration main class in <code>resources/META-INF/MANIFEST.MF</code>.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlightjs highlight"><code>Main-Class: com.example.BackgroundFunctionMain</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Then run the function locally.
|
||||
This is provided by the Google Cloud Functions <code>function-maven-plugin</code> described in the project dependencies section.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre>mvn function:run</pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Invoke the HTTP function:</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre>curl localhost:8080 -H "Content-Type: application/json" -d '{"data":"hello"}'</pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Verify that the function was invoked by viewing the logs.</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect4">
|
||||
<h5 id="_deploy_to_gcp_2"><a class="link" href="#_deploy_to_gcp_2">Deploy to GCP</a></h5>
|
||||
<div class="paragraph">
|
||||
<p>In order to deploy your background function to GCP, first package your application.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre>mvn package</pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>If you added the custom <code>spring-boot-maven-plugin</code> plugin defined above, you should see the resulting JAR in <code>target/deploy</code> directory.
|
||||
This JAR is correctly formatted for deployment to Google Cloud Functions.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Next, make sure that you have the <a href="https://cloud.google.com/sdk/install">Cloud SDK CLI</a> installed.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>From the project base directory run the following command to deploy.</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre>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</pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Google Cloud Function will now invoke the function every time a message is published to the topic specified by <code>--trigger-topic</code>.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>For a walkthrough on testing and verifying your background function, see the instructions for running the <a href="https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-samples/function-sample-gcp-background/">GCF Background Function sample</a>.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect3">
|
||||
<h4 id="_sample_functions"><a class="link" href="#_sample_functions">Sample Functions</a></h4>
|
||||
<div class="paragraph">
|
||||
<p>The project provides the following sample functions as reference:</p>
|
||||
</div>
|
||||
<div class="ulist">
|
||||
<ul>
|
||||
<li>
|
||||
<p>The <a href="https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-samples/function-sample-gcp-http/">function-sample-gcp-http</a> is an HTTP Function which you can test locally and try deploying.</p>
|
||||
</li>
|
||||
<li>
|
||||
<p>The <a href="https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-samples/function-sample-gcp-background/">function-sample-gcp-background</a> shows an example of a background function that is triggered by a message being published to a specified Pub/Sub topic.</p>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user