289 lines
9.4 KiB
HTML
289 lines
9.4 KiB
HTML
<!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};
|
||
}
|
||
|
||
function globalSwitch() {
|
||
$('.switch--item').each(function() {
|
||
$(this).off('click');
|
||
$(this).on('click', function() {
|
||
selectedText = $(this).text()
|
||
selectedIndex = $(this).index()
|
||
$(".switch--item").filter(function() { return ($(this).text() === selectedText) }).each(function() {
|
||
$(this).addClass('selected');
|
||
$(this).siblings().removeClass('selected');
|
||
selectedContent = $(this).parent().siblings(".content").eq(selectedIndex)
|
||
selectedContent.removeClass('hidden');
|
||
selectedContent.siblings().addClass('hidden');
|
||
});
|
||
});
|
||
});
|
||
}
|
||
|
||
$(addBlockSwitches);
|
||
$(globalSwitch);
|
||
|
||
</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.1.0.BUILD-SNAPSHOT</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>Let’s 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<String, String> uppercase() {
|
||
return value -> 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"><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>
|
||
<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>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>
|
||
</div>
|
||
<div class="listingblock">
|
||
<div class="content">
|
||
<pre>mvn package</pre>
|
||
</div>
|
||
</div>
|
||
<div class="paragraph">
|
||
<p>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>
|
||
</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.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 -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/github.min.css">
|
||
<script src="js/highlight/highlight.min.js"></script>
|
||
<script>hljs.initHighlighting()</script>
|
||
</body>
|
||
</html> |