Files
spring-cloud-static/spring-cloud-function/3.0.6.RELEASE/reference/html/gcp.html
2020-04-22 14:39:00 +00:00

305 lines
11 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.6.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="_getting_started"><a class="link" href="#_getting_started">Getting Started</a></h4>
<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="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">&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.gcloud.FunctionInvoker&lt;/functionTarget&gt;
&lt;port&gt;8080&lt;/port&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;</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:</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>To deploy to Google Cloud Function, you need to produce a fat jar using the Shade plugin, rather than the Spring Boot plugin.</p>
</div>
<div class="paragraph">
<p>First, if you already have the Spring Boot plugin in your <code>pom.xml</code>, <strong>remove</strong> it:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml">&lt;!-- Remove this block by deleting or commenting it out --&gt;
&lt;!--
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
--&gt;</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then, <strong>add</strong> the Shade Plugin configuration to generate a fat jar when you run the <code>mvn package</code> command.</p>
</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.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;phase&gt;package&lt;/phase&gt;
&lt;goals&gt;
&lt;goal&gt;shade&lt;/goal&gt;
&lt;/goals&gt;
&lt;configuration&gt;
&lt;shadedArtifactAttached&gt;true&lt;/shadedArtifactAttached&gt;
&lt;outputDirectory&gt;target/deploy&lt;/outputDirectory&gt;
&lt;shadedClassifierName&gt;gcp&lt;/shadedClassifierName&gt;
&lt;transformers&gt;
&lt;transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"&gt;
&lt;resource&gt;META-INF/spring.handlers&lt;/resource&gt;
&lt;/transformer&gt;
&lt;transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer"&gt;
&lt;resource&gt;META-INF/spring.factories&lt;/resource&gt;
&lt;/transformer&gt;
&lt;transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"&gt;
&lt;resource&gt;META-INF/spring.schemas&lt;/resource&gt;
&lt;/transformer&gt;
&lt;transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/&gt;
&lt;transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"&gt;
&lt;mainClass&gt;com.example.CloudFunctionMain&lt;/mainClass&gt;
&lt;/transformer&gt;
&lt;/transformers&gt;
&lt;/configuration&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;</code></pre>
</div>
</div>
<div class="admonitionblock important">
<table>
<tr>
<td class="icon">
<i class="fa icon-important" title="Important"></i>
</td>
<td class="content">
If both Spring Boot plugin and Shade plugin are present, Shade plugin may be shading a Spring Boot produced JAR, resulting in a Fat JAR that&#8217;s unusable in Google Cloud Function. Don&#8217;t forget to remove the Spring Boot plugin!
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Package the application.</p>
</div>
<div class="listingblock">
<div class="content">
<pre>mvn package</pre>
</div>
</div>
<div class="paragraph">
<p>You should see the fat jar in <code>target/deploy</code> directory.</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>
</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 \
--entry-point org.springframework.cloud.function.adapter.gcloud.FunctionInvoker \
--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 -d "hello"</pre>
</div>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_sample_function"><a class="link" href="#_sample_function">Sample Function</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>
</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>