Files
spring-cloud-static/spring-cloud-function/3.0.7.RELEASE/reference/html/gcp.html
2020-05-28 10:03:06 +00:00

469 lines
16 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 1.5.8">
<title>Google Cloud Functions (Alpha)</title>
<link rel="stylesheet" href="css/spring.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.hidden {
display: none;
}
.switch {
border-width: 1px 1px 0 1px;
border-style: solid;
border-color: #7a2518;
display: inline-block;
}
.switch--item {
padding: 10px;
background-color: #ffffff;
color: #7a2518;
display: inline-block;
cursor: pointer;
}
.switch--item:not(:first-child) {
border-width: 0 0 0 1px;
border-style: solid;
border-color: #7a2518;
}
.switch--item.selected {
background-color: #7a2519;
color: #ffffff;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js"></script>
<script type="text/javascript">
function addBlockSwitches() {
$('.primary').each(function() {
primary = $(this);
createSwitchItem(primary, createBlockSwitch(primary)).item.addClass("selected");
primary.children('.title').remove();
});
$('.secondary').each(function(idx, node) {
secondary = $(node);
primary = findPrimary(secondary);
switchItem = createSwitchItem(secondary, primary.children('.switch'));
switchItem.content.addClass('hidden');
findPrimary(secondary).append(switchItem.content);
secondary.remove();
});
}
function createBlockSwitch(primary) {
blockSwitch = $('<div class="switch"></div>');
primary.prepend(blockSwitch);
return blockSwitch;
}
function findPrimary(secondary) {
candidate = secondary.prev();
while (!candidate.is('.primary')) {
candidate = candidate.prev();
}
return candidate;
}
function createSwitchItem(block, blockSwitch) {
blockName = block.children('.title').text();
content = block.children('.content').first().append(block.next('.colist'));
item = $('<div class="switch--item">' + blockName + '</div>');
item.on('click', '', content, function(e) {
$(this).addClass('selected');
$(this).siblings().removeClass('selected');
e.data.siblings('.content').addClass('hidden');
e.data.removeClass('hidden');
});
blockSwitch.append(item);
return {'item': item, 'content': content};
}
$(addBlockSwitches);
</script>
</head>
<body class="book toc2 toc-left">
<div id="header">
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel2">
<li><a href="#_google_cloud_functions_alpha">Google Cloud Functions (Alpha)</a></li>
</ul>
</div>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p><strong>3.0.7.RELEASE</strong></p>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_google_cloud_functions_alpha"><a class="link" href="#_google_cloud_functions_alpha">Google Cloud Functions (Alpha)</a></h3>
<div class="paragraph">
<p>The Google Cloud Functions adapter enables Spring Cloud Function apps to run on the <a href="https://cloud.google.com/functions">Google Cloud Functions</a> serverless platform.
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="_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">&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
&lt;artifactId&gt;spring-cloud-function-adapter-gcp&lt;/artifactId&gt;
&lt;/dependency&gt;
...
&lt;/dependencies&gt;</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">&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;outputDirectory&gt;target/deploy&lt;/outputDirectory&gt;
&lt;/configuration&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
&lt;artifactId&gt;spring-cloud-function-adapter-gcp&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;/plugin&gt;</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">&lt;plugin&gt;
&lt;groupId&gt;com.google.cloud.functions&lt;/groupId&gt;
&lt;artifactId&gt;function-maven-plugin&lt;/artifactId&gt;
&lt;version&gt;0.9.1&lt;/version&gt;
&lt;configuration&gt;
&lt;functionTarget&gt;org.springframework.cloud.function.adapter.gcp.GcfJarLauncher&lt;/functionTarget&gt;
&lt;port&gt;8080&lt;/port&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;</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"><a class="link" href="#_getting_started">Getting Started</a></h5>
<div class="paragraph">
<p>Lets start with a simple Spring Cloud Function example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@SpringBootApplication
public class CloudFunctionMain {
public static void main(String[] args) {
SpringApplication.run(CloudFunctionMain.class, args);
}
@Bean
public Function&lt;String, String&gt; uppercase() {
return value -&gt; value.toUpperCase();
}
}</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.CloudFunctionMain</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 http://localhost:8080/ -d "hello"</pre>
</div>
</div>
</div>
<div class="sect4">
<h5 id="_deploy_to_gcp"><a class="link" href="#_deploy_to_gcp">Deploy to GCP</a></h5>
<div class="paragraph">
<p>As of March 2020, Google Cloud Functions for Java is in Alpha.
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>Start by packaging 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-http \
--entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \
--runtime java11 \
--trigger-http \
--source target/deploy \
--memory 512MB</pre>
</div>
</div>
<div class="paragraph">
<p>Invoke the HTTP function:</p>
</div>
<div class="listingblock">
<div class="content">
<pre>curl https://REGION-PROJECT_ID.cloudfunctions.net/function-sample-gcp-http -d "hello"</pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_background_functions"><a class="link" href="#_background_functions">Background Functions</a></h4>
<div class="paragraph">
<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_2"><a class="link" href="#_getting_started_2">Getting Started</a></h5>
<div class="paragraph">
<p>Lets 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&lt;PubSubMessage&gt; pubSubFunction() {
return message -&gt; 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&lt;String, String&gt; attributes;
private String messageId;
private String publishTime;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Map&lt;String, String&gt; getAttributes() {
return attributes;
}
public void setAttributes(Map&lt;String, String&gt; 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>
</div>
<script type="text/javascript" src="js/tocbot/tocbot.min.js"></script>
<script type="text/javascript" src="js/toc.js"></script>
<link rel="stylesheet" href="js/highlight/styles/atom-one-dark-reasonable.min.css">
<script src="js/highlight/highlight.min.js"></script>
<script>hljs.initHighlighting()</script>
</body>
</html>