Move Layout into adapter class

pr comments, mv package, make work locally.

revert pom change to parent

Rm provided scope - this is problematic for spring-boot-maven plugin to build jar

Detach sample from spring-cloud-function-parent

Update readmes

cleanup

Add comment

fix javadoc
Resolves #518
This commit is contained in:
dzou
2020-05-07 19:55:28 -04:00
committed by Oleg Zhurakousky
parent 8ca1b45c81
commit 17c3f185bc
8 changed files with 211 additions and 89 deletions

View File

@@ -36,7 +36,7 @@ Start by adding the Maven plugin provided as part of the Google Functions Framew
<artifactId>function-maven-plugin</artifactId>
<version>0.9.1</version>
<configuration>
<functionTarget>org.springframework.cloud.function.adapter.gcp.FunctionInvoker</functionTarget>
<functionTarget>org.springframework.cloud.function.adapter.gcp.GcfJarLauncher</functionTarget>
<port>8080</port>
</configuration>
</plugin>
@@ -66,67 +66,44 @@ curl http://localhost:8080/ -d "hello"
As of March 2020, Google Cloud Functions for Java is in Alpha.
You can get on the https://docs.google.com/forms/d/e/1FAIpQLScC98jGi7CfG0n3UYlj7Xad8XScvZC8-BBOg7Pk3uSZx_2cdQ/viewform[whitelist] to try it out.
To deploy to Google Cloud Function, you need to produce a fat jar using the Shade plugin, rather than the Spring Boot plugin.
In order to use the adapter, first add the dependency to your pom.xml:
[source, 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.
First, if you already have the Spring Boot plugin in your `pom.xml`, *remove* it:
[source, xml]
----
<!-- Remove this block by deleting or commenting it out -->
<!--
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
-->
----
Then, *add* the Shade Plugin configuration to generate a fat jar when you run the `mvn package` command.
[source, xml]
----
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<outputDirectory>target/deploy</outputDirectory>
<shadedClassifierName>gcp</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.CloudFunctionMain</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
<configuration>
<outputDirectory>target/deploy</outputDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-gcp</artifactId>
</dependency>
</dependencies>
</plugin>
----
IMPORTANT: 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's unusable in Google Cloud Function. Don't forget to remove the Spring Boot plugin!
Package the application.
----
mvn package
----
You should see the fat jar in `target/deploy` directory.
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 https://cloud.google.com/sdk/install[Cloud SDK CLI] installed.
@@ -134,7 +111,7 @@ From the project base directory run the following command to deploy.
----
gcloud alpha functions deploy function-sample-gcp \
--entry-point org.springframework.cloud.function.adapter.gcp.FunctionInvoker \
--entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \
--runtime java11 \
--trigger-http \
--source target/deploy \

View File

@@ -26,7 +26,6 @@
<groupId>com.google.cloud.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>${google.cloud.functions.api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
@@ -36,6 +35,17 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-context</artifactId>
</dependency>
<!-- Jar Layout dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader-tools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader</artifactId>
</dependency>
<!-- Test-only dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.adapter.gcp;
import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import org.springframework.boot.loader.JarLauncher;
import org.springframework.boot.loader.jar.JarFile;
/**
* The launcher class written at the top-level of the output JAR to be deployed to
* Google Cloud Functions. This is the entry point to the function when run from JAR.
*
* @author Ray Tsang
* @author Daniel Zou
*/
public class GcfJarLauncher extends JarLauncher implements HttpFunction {
private final ClassLoader loader;
private final HttpFunction delegate;
public GcfJarLauncher() throws Exception {
JarFile.registerUrlProtocolHandler();
this.loader = createClassLoader(getClassPathArchivesIterator());
Class<?> clazz = this.loader
.loadClass("org.springframework.cloud.function.adapter.gcp.FunctionInvoker");
this.delegate = (HttpFunction) clazz.getConstructor().newInstance();
}
@Override
public void service(HttpRequest httpRequest, HttpResponse httpResponse) throws Exception {
Thread.currentThread().setContextClassLoader(this.loader);
delegate.service(httpRequest, httpResponse);
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.adapter.gcp.layout;
import java.io.IOException;
import org.springframework.boot.loader.tools.CustomLoaderLayout;
import org.springframework.boot.loader.tools.Layouts;
import org.springframework.boot.loader.tools.LoaderClassesWriter;
import org.springframework.cloud.function.adapter.gcp.GcfJarLauncher;
/**
* A custom JAR Layout that writes GCF adapter classes to the top-level of the output JAR
* for deploying to GCF.
*
* @author Ray Tsang
* @author Daniel Zou
*/
public class GcfJarLayout extends Layouts.Jar implements CustomLoaderLayout {
private static final String LAUNCHER_NAME = GcfJarLauncher.class.getCanonicalName();
@Override
public String getLauncherClassName() {
return LAUNCHER_NAME;
}
@Override
public boolean isExecutable() {
return false;
}
@Override
public void writeLoadedClasses(LoaderClassesWriter writer) throws IOException {
writer.writeLoaderClasses();
String jarName = LAUNCHER_NAME.replaceAll("\\.", "/") + ".class";
writer.writeEntry(
jarName, GcfJarLauncher.class.getResourceAsStream("/" + jarName));
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.adapter.gcp.layout;
import java.io.File;
import org.springframework.boot.loader.tools.Layout;
import org.springframework.boot.loader.tools.LayoutFactory;
/**
* Factory boilerplate class that constructs {@link GcfJarLayout}.
*
* @author Ray Tsang
* @author Daniel Zou
*/
public class GcfJarLayoutFactory implements LayoutFactory {
@Override
public Layout getLayout(File source) {
return new GcfJarLayout();
}
}

View File

@@ -0,0 +1,2 @@
org.springframework.boot.loader.tools.LayoutFactory=\
org.springframework.cloud.function.adapter.gcp.layout.GcfJarLayoutFactory

View File

@@ -35,7 +35,7 @@ Run the following command from the project root to deploy.
----
gcloud alpha functions deploy function-sample-gcp \
--entry-point org.springframework.cloud.function.adapter.gcp.FunctionInvoker \
--entry-point org.springframework.cloud.function.adapter.gcp.GcfJarLauncher \
--runtime java11 \
--trigger-http \
--source target/deploy \

View File

@@ -2,21 +2,28 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.spring.sample</groupId>
<artifactId>function-sample-gcp</artifactId>
<version>2.0.0.RELEASE</version>
<packaging>jar</packaging>
<name>function-sample-gcp</name>
<parent>
<artifactId>spring-cloud-function-samples</artifactId>
<groupId>org.springframework.cloud</groupId>
<version>3.1.0.BUILD-SNAPSHOT</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-adapter-gcp</artifactId>
<version>${project.version}</version>
<version>3.1.0.BUILD-SNAPSHOT</version>
</dependency>
<!-- test dependencies -->
@@ -45,49 +52,30 @@
<build>
<plugins>
<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>
<version>3.1.0.BUILD-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
<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.FunctionInvoker</functionTarget>
<functionTarget>org.springframework.cloud.function.adapter.gcp.GcfJarLauncher</functionTarget>
<port>8080</port>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<outputDirectory>target/deploy</outputDirectory>
<shadedClassifierName>gcp</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.CloudFunctionMain</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>