671 lines
48 KiB
XML
671 lines
48 KiB
XML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<?asciidoc-toc?>
|
|
<?asciidoc-numbered?>
|
|
<book xmlns="http://docbook.org/ns/docbook" xmlns:xl="http://www.w3.org/1999/xlink" version="5.0" xml:lang="en">
|
|
<info>
|
|
<title>Spring Cloud Function</title>
|
|
<date>2019-01-24</date>
|
|
</info>
|
|
<preface>
|
|
<title></title>
|
|
<simpara>Mark Fisher, Dave Syer, Oleg Zhurakousky</simpara>
|
|
<simpara><?asciidoc-hr?></simpara>
|
|
</preface>
|
|
<chapter xml:id="_introduction">
|
|
<title>Introduction</title>
|
|
<simpara>Spring Cloud Function is a project with the following high-level goals:</simpara>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>Promote the implementation of business logic via functions.</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>Decouple the development lifecycle of business logic from any specific runtime target so that the same code can run as a web endpoint, a stream processor, or a task.</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>Support a uniform programming model across serverless providers, as well as the ability to run standalone (locally or in a PaaS).</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>Enable Spring Boot features (auto-configuration, dependency injection, metrics) on serverless providers.</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<simpara>It abstracts away all of the transport details and
|
|
infrastructure, allowing the developer to keep all the familiar tools
|
|
and processes, and focus firmly on business logic.</simpara>
|
|
<simpara>Here’s a complete, executable, testable Spring Boot application
|
|
(implementing a simple string manipulation):</simpara>
|
|
<programlisting language="java" linenumbering="unnumbered">@SpringBootApplication
|
|
public class Application {
|
|
|
|
@Bean
|
|
public Function<Flux<String>, Flux<String>> uppercase() {
|
|
return flux -> flux.map(value -> value.toUpperCase());
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
SpringApplication.run(Application.class, args);
|
|
}
|
|
}</programlisting>
|
|
<simpara>It’s just a Spring Boot application, so it can be built, run and
|
|
tested, locally and in a CI build, the same way as any other Spring
|
|
Boot application. The <literal>Function</literal> is from <literal>java.util</literal> and <literal>Flux</literal> is a
|
|
<link xl:href="http://www.reactive-streams.org/">Reactive Streams</link> <literal>Publisher</literal> from
|
|
<link xl:href="https://projectreactor.io/">Project Reactor</link>. The function can be
|
|
accessed over HTTP or messaging.</simpara>
|
|
<simpara>Spring Cloud Function has 4 main features:</simpara>
|
|
<orderedlist numeration="arabic">
|
|
<listitem>
|
|
<simpara>Wrappers for <literal>@Beans</literal> of type <literal>Function</literal>, <literal>Consumer</literal> and
|
|
<literal>Supplier</literal>, exposing them to the outside world as either HTTP
|
|
endpoints and/or message stream listeners/publishers with RabbitMQ, Kafka etc.</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>Compiling strings which are Java function bodies into bytecode, and
|
|
then turning them into <literal>@Beans</literal> that can be wrapped as above.</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>Deploying a JAR file containing such an application context with an
|
|
isolated classloader, so that you can pack them together in a single
|
|
JVM.</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>Adapters for <link xl:href="https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-adapters/spring-cloud-function-adapter-aws">AWS Lambda</link>, <link xl:href="https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-adapters/spring-cloud-function-adapter-azure">Azure</link>, <link xl:href="https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-adapters/spring-cloud-function-adapter-openwhisk">Apache OpenWhisk</link> and possibly other "serverless" service providers.</simpara>
|
|
</listitem>
|
|
</orderedlist>
|
|
<note>
|
|
<simpara>Spring Cloud is released under the non-restrictive Apache 2.0 license. If you would like to contribute to this section of the documentation or if you find an error, please find the source code and issue trackers in the project at <link xl:href="https://github.com/spring-cloud/spring-cloud-function/tree/master/docs/src/main/asciidoc">github</link>.</simpara>
|
|
</note>
|
|
</chapter>
|
|
<chapter xml:id="_getting_started">
|
|
<title>Getting Started</title>
|
|
<simpara>Build from the command line (and "install" the samples):</simpara>
|
|
<screen>$ ./mvnw clean install</screen>
|
|
<simpara>(If you like to YOLO add <literal>-DskipTests</literal>.)</simpara>
|
|
<simpara>Run one of the samples, e.g.</simpara>
|
|
<screen>$ java -jar spring-cloud-function-samples/function-sample/target/*.jar</screen>
|
|
<simpara>This runs the app and exposes its functions over HTTP, so you can
|
|
convert a string to uppercase, like this:</simpara>
|
|
<screen>$ curl -H "Content-Type: text/plain" localhost:8080/uppercase -d Hello
|
|
HELLO</screen>
|
|
<simpara>You can convert multiple strings (a <literal>Flux<String></literal>) by separating them
|
|
with new lines</simpara>
|
|
<screen>$ curl -H "Content-Type: text/plain" localhost:8080/uppercase -d 'Hello
|
|
> World'
|
|
HELLOWORLD</screen>
|
|
<simpara>(You can use <literal><superscript>Q</superscript>J</literal> in a terminal to insert a new line in a literal
|
|
string like that.)</simpara>
|
|
</chapter>
|
|
<chapter xml:id="_building_and_running_a_function">
|
|
<title>Building and Running a Function</title>
|
|
<simpara>The sample <literal>@SpringBootApplication</literal> above has a function that can be
|
|
decorated at runtime by Spring Cloud Function to be an HTTP endpoint,
|
|
or a Stream processor, for instance with RabbitMQ, Apache Kafka or
|
|
JMS.</simpara>
|
|
<simpara>The <literal>@Beans</literal> can be <literal>Function</literal>, <literal>Consumer</literal> or <literal>Supplier</literal> (all from
|
|
<literal>java.util</literal>), and their parametric types can be String or POJO.</simpara>
|
|
<simpara>Functions can also be of <literal>Flux<String></literal> or <literal>Flux<Pojo></literal> and Spring
|
|
Cloud Function takes care of converting the data to and from the
|
|
desired types, as long as it comes in as plain text or (in the case of
|
|
the POJO) JSON. There is also support for <literal>Message<Pojo></literal> where the
|
|
message headers are copied from the incoming event, depending on the
|
|
adapter. The web adapter also supports conversion from form-encoded
|
|
data to a <literal>Map</literal>, and if you are using the function with Spring Cloud
|
|
Stream then all the conversion and coercion features for message
|
|
payloads will be applicable as well.</simpara>
|
|
<simpara>Functions can be grouped together in a single application, or deployed
|
|
one-per-jar. It’s up to the developer to choose. An app with multiple
|
|
functions can be deployed multiple times in different "personalities",
|
|
exposing different functions over different physical transports.</simpara>
|
|
</chapter>
|
|
<chapter xml:id="_function_catalog_and_flexible_function_signatures">
|
|
<title>Function Catalog and Flexible Function Signatures</title>
|
|
<simpara>One of the main features of Spring Cloud Function is to adapt and support a range of type signatures for user-defined functions,
|
|
while providing a consistent execution model.
|
|
That’s why all user defined functions are transformed into a canonical representation by <literal>FunctionCatalog</literal>, using primitives
|
|
defined by the <link xl:href="https://projectreactor.io/">Project Reactor</link> (i.e., <literal>Flux<T></literal> and <literal>Mono<T></literal>).
|
|
Users can supply a bean of type <literal>Function<String,String></literal>, for instance, and the <literal>FunctionCatalog</literal> will wrap it into a
|
|
<literal>Function<Flux<String>,Flux<String>></literal>.</simpara>
|
|
<simpara>Using Reactor based primitives not only helps with the canonical representation of user defined functions, but it also
|
|
facilitates a more robust and flexible(reactive) execution model.</simpara>
|
|
<simpara>While users don’t normally have to care about the <literal>FunctionCatalog</literal> at all, it is useful to know what
|
|
kind of functions are supported in user code.</simpara>
|
|
<section xml:id="_java_8_function_support">
|
|
<title>Java 8 function support</title>
|
|
<simpara>Generally speaking users can expect that if they write a function for
|
|
a plain old Java type (or primitive wrapper), then the function
|
|
catalog will wrap it to a <literal>Flux</literal> of the same type. If the user writes
|
|
a function using <literal>Message</literal> (from spring-messaging) it will receive and
|
|
transmit headers from any adapter that supports key-value metadata
|
|
(e.g. HTTP headers). Here are the details.</simpara>
|
|
<informaltable frame="all" rowsep="1" colsep="1">
|
|
<tgroup cols="3">
|
|
<colspec colname="col_1" colwidth="33.3333*"/>
|
|
<colspec colname="col_2" colwidth="33.3333*"/>
|
|
<colspec colname="col_3" colwidth="33.3334*"/>
|
|
<thead>
|
|
<row>
|
|
<entry align="left" valign="top">User Function</entry>
|
|
<entry align="left" valign="top">Catalog Registration</entry>
|
|
<entry align="left" valign="top"></entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara><literal>Function<S,T></literal></simpara></entry>
|
|
<entry align="left" valign="top"><simpara><literal>Function<Flux<S>, Flux<T>></literal></simpara></entry>
|
|
<entry align="left" valign="top"></entry>
|
|
</row>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara><literal>Function<Message<S>,Message<T>></literal></simpara></entry>
|
|
<entry align="left" valign="top"><simpara><literal>Function<Flux<Message<S>>, Flux<Message<T>>></literal></simpara></entry>
|
|
<entry align="left" valign="top"></entry>
|
|
</row>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara><literal>Function<Flux<S>, Flux<T>></literal></simpara></entry>
|
|
<entry align="left" valign="top"><simpara><literal>Function<Flux<S>, Flux<T>></literal> (pass through)</simpara></entry>
|
|
<entry align="left" valign="top"></entry>
|
|
</row>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara><literal>Supplier<T></literal></simpara></entry>
|
|
<entry align="left" valign="top"><simpara><literal>Supplier<Flux<T>></literal></simpara></entry>
|
|
<entry align="left" valign="top"></entry>
|
|
</row>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara><literal>Supplier<Flux<T>></literal></simpara></entry>
|
|
<entry align="left" valign="top"><simpara><literal>Supplier<Flux<T>></literal></simpara></entry>
|
|
<entry align="left" valign="top"></entry>
|
|
</row>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara><literal>Consumer<T></literal></simpara></entry>
|
|
<entry align="left" valign="top"><simpara><literal>Function<Flux<T>, Mono<Void>></literal></simpara></entry>
|
|
<entry align="left" valign="top"></entry>
|
|
</row>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara><literal>Consumer<Message<T>></literal></simpara></entry>
|
|
<entry align="left" valign="top"><simpara><literal>Function<Flux<Message<T>>, Mono<Void>></literal></simpara></entry>
|
|
<entry align="left" valign="top"></entry>
|
|
</row>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara><literal>Consumer<Flux<T>></literal></simpara></entry>
|
|
<entry align="left" valign="top"><simpara><literal>Consumer<Flux<T>></literal></simpara></entry>
|
|
<entry align="left" valign="top"></entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
<simpara>Consumer is a little bit special because it has a <literal>void</literal> return type,
|
|
which implies blocking, at least potentially. Most likely you will not
|
|
need to write <literal>Consumer<Flux<?>></literal>, but if you do need to do that,
|
|
remember to subscribe to the input flux. If you declare a <literal>Consumer</literal>
|
|
of a non publisher type (which is normal), it will be converted to a
|
|
function that returns a publisher, so that it can be subscribed to in
|
|
a controlled way.</simpara>
|
|
</section>
|
|
<section xml:id="_kotlin_lambda_support">
|
|
<title>Kotlin Lambda support</title>
|
|
<simpara>We also provide support for Kotlin lambdas (since v2.0).
|
|
Consider the following:</simpara>
|
|
<programlisting language="java" linenumbering="unnumbered">@Bean
|
|
open fun kotlinSupplier(): () -> String {
|
|
return { "Hello from Kotlin" }
|
|
}
|
|
|
|
@Bean
|
|
open fun kotlinFunction(): (String) -> String {
|
|
return { it.toUpperCase() }
|
|
}
|
|
|
|
@Bean
|
|
open fun kotlinConsumer(): (String) -> Unit {
|
|
return { println(it) }
|
|
}</programlisting>
|
|
<simpara>The above represents Kotlin lambdas configured as Spring beans. The signature of each maps to a Java equivalent of
|
|
<literal>Supplier</literal>, <literal>Function</literal> and <literal>Consumer</literal>, and thus supported/recognized signatures by the framework.
|
|
While mechanics of Kotlin-to-Java mapping are outside of the scope of this documentation, it is important to understand that the
|
|
same rules for signature transformation outlined in "Java 8 function support" section are applied here as well.</simpara>
|
|
<simpara>To enable Kotlin support all you need is to add <literal>spring-cloud-function-kotlin</literal> module to your classpath which contains the appropriate
|
|
autoconfiguration and supporting classes.</simpara>
|
|
</section>
|
|
</chapter>
|
|
<chapter xml:id="_standalone_web_applications">
|
|
<title>Standalone Web Applications</title>
|
|
<simpara>The <literal>spring-cloud-function-web</literal> module has autoconfiguration that
|
|
activates when it is included in a Spring Boot web application (with
|
|
MVC support). There is also a <literal>spring-cloud-starter-function-web</literal> to
|
|
collect all the optional dependencies in case you just want a simple
|
|
getting started experience.</simpara>
|
|
<simpara>With the web configurations activated your app will have an MVC
|
|
endpoint (on "/" by default, but configurable with
|
|
<literal>spring.cloud.function.web.path</literal>) that can be used to access the
|
|
functions in the application context. The supported content types are
|
|
plain text and JSON.</simpara>
|
|
<informaltable frame="all" rowsep="1" colsep="1">
|
|
<tgroup cols="5">
|
|
<colspec colname="col_1" colwidth="20*"/>
|
|
<colspec colname="col_2" colwidth="20*"/>
|
|
<colspec colname="col_3" colwidth="20*"/>
|
|
<colspec colname="col_4" colwidth="20*"/>
|
|
<colspec colname="col_5" colwidth="20*"/>
|
|
<thead>
|
|
<row>
|
|
<entry align="left" valign="top">Method</entry>
|
|
<entry align="left" valign="top">Path</entry>
|
|
<entry align="left" valign="top">Request</entry>
|
|
<entry align="left" valign="top">Response</entry>
|
|
<entry align="left" valign="top">Status</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara>GET</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>/{supplier}</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>-</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>Items from the named supplier</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>200 OK</simpara></entry>
|
|
</row>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara>POST</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>/{consumer}</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>JSON object or text</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>Mirrors input and pushes request body into consumer</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>202 Accepted</simpara></entry>
|
|
</row>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara>POST</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>/{consumer}</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>JSON array or text with new lines</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>Mirrors input and pushes body into consumer one by one</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>202 Accepted</simpara></entry>
|
|
</row>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara>POST</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>/{function}</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>JSON object or text</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>The result of applying the named function</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>200 OK</simpara></entry>
|
|
</row>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara>POST</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>/{function}</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>JSON array or text with new lines</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>The result of applying the named function</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>200 OK</simpara></entry>
|
|
</row>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara>GET</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>/{function}/{item}</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>-</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>Convert the item into an object and return the result of applying the function</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>200 OK</simpara></entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
<simpara>As the table above shows the behaviour of the endpoint depends on the method and also the type of incoming request data. When the incoming data is single valued, and the target function is declared as obviously single valued (i.e. not returning a collection or <literal>Flux</literal>), then the response will also contain a single value. For multi-valued responses the client can ask for a server-sent event stream by sending `Accept: text/event-stream". If there is only one function (consumer etc.) then the name in the path is optional. Composite functions can be addressed using pipes or commas to separate function names (pipes are legal in URL paths, but a bit awkward to type on the command line).</simpara>
|
|
<simpara>Functions and consumers that are declared with input and output in <literal>Message<?></literal> will see the request headers on the input messages, and the output message headers will be converted to HTTP headers.</simpara>
|
|
<simpara>When POSTing text the response format might be different with Spring Boot 2.0 and older versions, depending on the content negotiation (provide content type and accpt headers for the best results).</simpara>
|
|
</chapter>
|
|
<chapter xml:id="_standalone_streaming_applications">
|
|
<title>Standalone Streaming Applications</title>
|
|
<simpara>To send or receive messages from a broker (such as RabbitMQ or Kafka) you can leverage <literal>spring-cloud-stream</literal> project and it’s integration with Spring Cloud Function.
|
|
Please refer to <link xl:href="https://docs.spring.io/spring-cloud-stream/docs/current/reference/htmlsingle/#_spring_cloud_function">Spring Cloud Function</link> section of the Spring Cloud Stream reference manual for more details and examples.</simpara>
|
|
</chapter>
|
|
<chapter xml:id="_deploying_a_packaged_function">
|
|
<title>Deploying a Packaged Function</title>
|
|
<simpara>Spring Cloud Function provides a "deployer" library that allows you to launch a jar file (or exploded archive, or set of jar files) with an isolated class loader and expose the functions defined in it. This is quite a powerful tool that would allow you to, for instance, adapt a function to a range of different input-output adapters without changing the target jar file. Serverless platforms often have this kind of feature built in, so you could see it as a building block for a function invoker in such a platform (indeed the <link xl:href="https://projectriff.io">Riff</link> Java function invoker uses this library).</simpara>
|
|
<simpara>The standard entry point of the API is the Spring configuration annotation <literal>@EnableFunctionDeployer</literal>. If that is used in a Spring Boot application the deployer kicks in and looks for some configuration to tell it where to find the function jar. At a minimum the user has to provide a <literal>function.location</literal> which is a URL or resource location for the archive containing the functions. It can optionally use a <literal>maven:</literal> prefix to locate the artifact via a dependency lookup (see <literal>FunctionProperties</literal> for complete details). A Spring Boot application is bootstrapped from the jar file, using the <literal>MANIFEST.MF</literal> to locate a start class, so that a standard Spring Boot fat jar works well, for example. If the target jar can be launched successfully then the result is a function registered in the main application’s <literal>FunctionCatalog</literal>. The registered function can be applied by code in the main application, even though it was created in an isolated class loader (by deault).</simpara>
|
|
</chapter>
|
|
<chapter xml:id="_functional_bean_definitions">
|
|
<title>Functional Bean Definitions</title>
|
|
<simpara>Spring Cloud Function supports a "functional" style of bean declarations for small apps where you need fast startup. The functional style of bean declaration was a feature of Spring Framework 5.0 with significant enhancements in 5.1.</simpara>
|
|
<section xml:id="_comparing_functional_with_traditional_bean_definitions">
|
|
<title>Comparing Functional with Traditional Bean Definitions</title>
|
|
<simpara>Here’s a vanilla Spring Cloud Function application from with the
|
|
familiar <literal>@Configuration</literal> and <literal>@Bean</literal> declaration style:</simpara>
|
|
<programlisting language="java" linenumbering="unnumbered">@SpringBootApplication
|
|
public class DemoApplication {
|
|
|
|
@Bean
|
|
public Function<String, String> uppercase() {
|
|
return value -> value.toUpperCase();
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
SpringApplication.run(DemoApplication.class, args);
|
|
}
|
|
|
|
}</programlisting>
|
|
<simpara>You can run the above in a serverless platform, like AWS Lambda or Azure Functions, or you can run it in its own HTTP server just by including <literal>spring-cloud-function-starter-web</literal> on the classpath. Running the main method would expose an endpoint that you can use to ping that <literal>uppercase</literal> function:</simpara>
|
|
<screen>$ curl localhost:8080 -d foo
|
|
FOO</screen>
|
|
<simpara>The web adapter in <literal>spring-cloud-function-starter-web</literal> uses Spring MVC, so you needed a Servlet container. You can also use Webflux where the default server is netty (even though you can still use Servlet containers if you want to) - just include the <literal>spring-cloud-starter-function-webflux</literal> dependency instead. The functionality is the same, and the user application code can be used in both.</simpara>
|
|
<simpara>Now for the functional beans: the user application code can be recast into "functional"
|
|
form, like this:</simpara>
|
|
<programlisting language="java" linenumbering="unnumbered">@SpringBootConfiguration
|
|
public class DemoApplication implements ApplicationContextInitializer<GenericApplicationContext> {
|
|
|
|
public static void main(String[] args) {
|
|
FunctionalSpringApplication.run(DemoApplication.class, args);
|
|
}
|
|
|
|
public Function<String, String> uppercase() {
|
|
return value -> value.toUpperCase();
|
|
}
|
|
|
|
@Override
|
|
public void initialize(GenericApplicationContext context) {
|
|
context.registerBean("demo", FunctionRegistration.class,
|
|
() -> new FunctionRegistration<>(uppercase())
|
|
.type(FunctionType.from(String.class).to(String.class)));
|
|
}
|
|
|
|
}</programlisting>
|
|
<simpara>The main differences are:</simpara>
|
|
<itemizedlist>
|
|
<listitem>
|
|
<simpara>The main class is an <literal>ApplicationContextInitializer</literal>.</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>The <literal>@Bean</literal> methods have been converted to calls to <literal>context.registerBean()</literal></simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>The <literal>@SpringBootApplication</literal> has been replaced with
|
|
<literal>@SpringBootConfiguration</literal> to signify that we are not enabling Spring
|
|
Boot autoconfiguration, and yet still marking the class as an "entry
|
|
point".</simpara>
|
|
</listitem>
|
|
<listitem>
|
|
<simpara>The <literal>SpringApplication</literal> from Spring Boot has been replaced with a
|
|
<literal>FunctionalSpringApplication</literal> from Spring Cloud Function (it’s a
|
|
subclass).</simpara>
|
|
</listitem>
|
|
</itemizedlist>
|
|
<simpara>The business logic beans that you register in a Spring Cloud Function app are of type <literal>FunctionRegistration</literal>. This is a wrapper that contains both the function and information about the input and output types. In the <literal>@Bean</literal> form of the application that information can be derived reflectively, but in a functional bean registration some of it is lost unless we use a <literal>FunctionRegistration</literal>.</simpara>
|
|
<simpara>An alternative to using an <literal>ApplicationContextInitializer</literal> and <literal>FunctionRegistration</literal> is to make the application itself implement <literal>Function</literal> (or <literal>Consumer</literal> or <literal>Supplier</literal>). Example (equivalent to the above):</simpara>
|
|
<programlisting language="java" linenumbering="unnumbered">@SpringBootConfiguration
|
|
public class DemoApplication implements Function<String, String> {
|
|
|
|
public static void main(String[] args) {
|
|
FunctionalSpringApplication.run(DemoApplication.class, args);
|
|
}
|
|
|
|
@Override
|
|
public String uppercase(String value) {
|
|
return value.toUpperCase();
|
|
}
|
|
|
|
}</programlisting>
|
|
<simpara>It would also work if you add a separate, standalone class of type <literal>Function</literal> and register it with the <literal>SpringApplication</literal> using an alternative form of the <literal>run()</literal> method. The main thing is that the generic type information is available at runtime through the class declaration.</simpara>
|
|
<simpara>The app runs in its own HTTP server if you add <literal>spring-cloud-starter-function-webflux</literal> (it won’t work with the MVC starter at the moment because the functional form of the embedded Servlet container hasn’t been implemented). The app also runs just fine in AWS Lambda or Azure Functions, and the improvements in startup time are dramatic.</simpara>
|
|
<note>
|
|
<simpara>The "lite" web server has some limitations for the range of <literal>Function</literal> signatures - in particular it doesn’t (yet) support <literal>Message</literal> input and output, but POJOs and any kind of <literal>Publisher</literal> should be fine.</simpara>
|
|
</note>
|
|
</section>
|
|
<section xml:id="_testing_functional_applications">
|
|
<title>Testing Functional Applications</title>
|
|
<simpara>Spring Cloud Function also has some utilities for integration testing that will be very familiar to Spring Boot users. For example, here is an integration test for the HTTP server wrapping the app above:</simpara>
|
|
<programlisting language="java" linenumbering="unnumbered">@RunWith(SpringRunner.class)
|
|
@FunctionalSpringBootTest
|
|
@AutoConfigureWebTestClient
|
|
public class FunctionalTests {
|
|
|
|
@Autowired
|
|
private WebTestClient client;
|
|
|
|
@Test
|
|
public void words() throws Exception {
|
|
client.post().uri("/").body(Mono.just("foo"), String.class).exchange()
|
|
.expectStatus().isOk().expectBody(String.class).isEqualTo("FOO");
|
|
}
|
|
|
|
}</programlisting>
|
|
<simpara>This test is almost identical to the one you would write for the <literal>@Bean</literal> version of the same app - the only difference is the <literal>@FunctionalSpringBootTest</literal> annotation, instead of the regular <literal>@SpringBootTest</literal>. All the other pieces, like the <literal>@Autowired</literal> <literal>WebTestClient</literal>, are standard Spring Boot features.</simpara>
|
|
<simpara>Or you could write a test for a non-HTTP app using just the <literal>FunctionCatalog</literal>. For example:</simpara>
|
|
<programlisting language="java" linenumbering="unnumbered">@RunWith(SpringRunner.class)
|
|
@FunctionalSpringBootTest
|
|
public class FunctionalTests {
|
|
|
|
@Autowired
|
|
private FunctionCatalog catalog;
|
|
|
|
@Test
|
|
public void words() throws Exception {
|
|
Function<Flux<String>, Flux<String>> function = catalog.lookup(Function.class,
|
|
"function");
|
|
assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
|
|
}
|
|
|
|
}</programlisting>
|
|
<simpara>(The <literal>FunctionCatalog</literal> always returns functions from <literal>Flux</literal> to <literal>Flux</literal>, even if the user declares them with a simpler signature.)</simpara>
|
|
</section>
|
|
<section xml:id="_limitations_of_functional_bean_declaration">
|
|
<title>Limitations of Functional Bean Declaration</title>
|
|
<simpara>Most Spring Cloud Function apps have a relatively small scope compared to the whole of Spring Boot, so we are able to adapt it to these functional bean definitions easily. If you step outside that limited scope, you can extend your Spring Cloud Function app by switching back to <literal>@Bean</literal> style configuration, or by using a hybrid approach. If you want to take advantage of Spring Boot autoconfiguration for integrations with external datastores, for example, you will need to use <literal>@EnableAutoConfiguration</literal>. Your functions can still be defined using the functional declarations if you want (i.e. the "hybrid" style), but in that case you will need to explicitly switch off the "full functional mode" using <literal>spring.functional.enabled=false</literal> so that Spring Boot can take back control.</simpara>
|
|
</section>
|
|
</chapter>
|
|
<chapter xml:id="_dynamic_compilation">
|
|
<title>Dynamic Compilation</title>
|
|
<simpara>There is a sample app that uses the function compiler to create a
|
|
function from a configuration property. The vanilla "function-sample"
|
|
also has that feature. And there are some scripts that you can run to
|
|
see the compilation happening at run time. To run these examples,
|
|
change into the <literal>scripts</literal> directory:</simpara>
|
|
<screen>cd scripts</screen>
|
|
<simpara>Also, start a RabbitMQ server locally (e.g. execute <literal>rabbitmq-server</literal>).</simpara>
|
|
<simpara>Start the Function Registry Service:</simpara>
|
|
<screen>./function-registry.sh</screen>
|
|
<simpara>Register a Function:</simpara>
|
|
<screen>./registerFunction.sh -n uppercase -f "f->f.map(s->s.toString().toUpperCase())"</screen>
|
|
<simpara>Run a REST Microservice using that Function:</simpara>
|
|
<screen>./web.sh -f uppercase -p 9000
|
|
curl -H "Content-Type: text/plain" -H "Accept: text/plain" localhost:9000/uppercase -d foo</screen>
|
|
<simpara>Register a Supplier:</simpara>
|
|
<screen>./registerSupplier.sh -n words -f "()->Flux.just(\"foo\",\"bar\")"</screen>
|
|
<simpara>Run a REST Microservice using that Supplier:</simpara>
|
|
<screen>./web.sh -s words -p 9001
|
|
curl -H "Accept: application/json" localhost:9001/words</screen>
|
|
<simpara>Register a Consumer:</simpara>
|
|
<screen>./registerConsumer.sh -n print -t String -f "System.out::println"</screen>
|
|
<simpara>Run a REST Microservice using that Consumer:</simpara>
|
|
<screen>./web.sh -c print -p 9002
|
|
curl -X POST -H "Content-Type: text/plain" -d foo localhost:9002/print</screen>
|
|
<simpara>Run Stream Processing Microservices:</simpara>
|
|
<simpara>First register a streaming words supplier:</simpara>
|
|
<screen>./registerSupplier.sh -n wordstream -f "()->Flux.interval(Duration.ofMillis(1000)).map(i->\"message-\"+i)"</screen>
|
|
<simpara>Then start the source (supplier), processor (function), and sink (consumer) apps
|
|
(in reverse order):</simpara>
|
|
<screen>./stream.sh -p 9103 -i uppercaseWords -c print
|
|
./stream.sh -p 9102 -i words -f uppercase -o uppercaseWords
|
|
./stream.sh -p 9101 -s wordstream -o words</screen>
|
|
<simpara>The output will appear in the console of the sink app (one message per second, converted to uppercase):</simpara>
|
|
<screen>MESSAGE-0
|
|
MESSAGE-1
|
|
MESSAGE-2
|
|
MESSAGE-3
|
|
MESSAGE-4
|
|
MESSAGE-5
|
|
MESSAGE-6
|
|
MESSAGE-7
|
|
MESSAGE-8
|
|
MESSAGE-9
|
|
...</screen>
|
|
</chapter>
|
|
<chapter xml:id="_serverless_platform_adapters">
|
|
<title>Serverless Platform Adapters</title>
|
|
<simpara>As well as being able to run as a standalone process, a Spring Cloud
|
|
Function application can be adapted to run one of the existing
|
|
serverless platforms. In the project there are adapters for
|
|
<link xl:href="https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-adapters/spring-cloud-function-adapter-aws">AWS
|
|
Lambda</link>,
|
|
<link xl:href="https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-adapters/spring-cloud-function-adapter-azure">Azure</link>,
|
|
and
|
|
<link xl:href="https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-adapters/spring-cloud-function-adapter-openwhisk">Apache
|
|
OpenWhisk</link>. The <link xl:href="https://github.com/fnproject/fn">Oracle Fn platform</link>
|
|
has its own Spring Cloud Function adapter. And
|
|
<link xl:href="https://projectriff.io">Riff</link> supports Java functions and its
|
|
<link xl:href="https://github.com/projectriff/java-function-invoker">Java Function
|
|
Invoker</link> acts natively is an adapter for Spring Cloud Function jars.</simpara>
|
|
<section xml:id="_aws_lambda">
|
|
<title>AWS Lambda</title>
|
|
<simpara>The <link xl:href="https://aws.amazon.com/">AWS</link> adapter takes a Spring Cloud Function app and converts it to a form that can run in AWS Lambda.</simpara>
|
|
<section xml:id="_introduction_2">
|
|
<title>Introduction</title>
|
|
<simpara>The adapter has a couple of generic request handlers that you can use. The most generic is <literal>SpringBootStreamHandler</literal>, which uses a Jackson <literal>ObjectMapper</literal> provided by Spring Boot to serialize and deserialize the objects in the function. There is also a <literal>SpringBootRequestHandler</literal> 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).</simpara>
|
|
<simpara>If your app has more than one <literal>@Bean</literal> of type <literal>Function</literal> etc. then you can choose the one to use by configuring <literal>function.name</literal> (e.g. as <literal>FUNCTION_NAME</literal> environment variable in AWS). The functions are extracted from the Spring Cloud <literal>FunctionCatalog</literal> (searching first for <literal>Function</literal> then <literal>Consumer</literal> and finally <literal>Supplier</literal>).</simpara>
|
|
</section>
|
|
<section xml:id="_notes_on_jar_layout">
|
|
<title>Notes on JAR Layout</title>
|
|
<simpara>You don’t need the Spring Cloud Function Web or Stream adapter at runtime in Lambda, so you might need to exclude those before you create the JAR you send to AWS. A Lambda application has to be shaded, but a Spring Boot standalone application does not, so you can run the same app using 2 separate jars (as per the sample). The sample app creates 2 jar files, one with an <literal>aws</literal> classifier for deploying in Lambda, and one executable (thin) jar that includes <literal>spring-cloud-function-web</literal> at runtime. Spring Cloud Function will try and locate a "main class" for you from the JAR file manifest, using the <literal>Start-Class</literal> attribute (which will be added for you by the Spring Boot tooling if you use the starter parent). If there is no <literal>Start-Class</literal> in your manifest you can use an environment variable <literal>MAIN_CLASS</literal> when you deploy the function to AWS.</simpara>
|
|
</section>
|
|
<section xml:id="_upload">
|
|
<title>Upload</title>
|
|
<simpara>Build the sample under <literal>spring-cloud-function-samples/function-sample-aws</literal> and upload the <literal>-aws</literal> jar file to Lambda. The handler can be <literal>example.Handler</literal> or <literal>org.springframework.cloud.function.adapter.aws.SpringBootStreamHandler</literal> (FQN of the class, <emphasis>not</emphasis> a method reference, although Lambda does accept method references).</simpara>
|
|
<screen>./mvnw -U clean package</screen>
|
|
<simpara>Using the AWS command line tools it looks like this:</simpara>
|
|
<screen>aws lambda create-function --function-name Uppercase --role arn:aws:iam::[USERID]:role/service-role/[ROLE] --zip-file fileb://function-sample-aws/target/function-sample-aws-2.0.0.BUILD-SNAPSHOT-aws.jar --handler org.springframework.cloud.function.adapter.aws.SpringBootStreamHandler --description "Spring Cloud Function Adapter Example" --runtime java8 --region us-east-1 --timeout 30 --memory-size 1024 --publish</screen>
|
|
<simpara>The input type for the function in the AWS sample is a Foo with a single property called "value". So you would need this to test it:</simpara>
|
|
<screen>{
|
|
"value": "test"
|
|
}</screen>
|
|
<note>
|
|
<simpara>The AWS sample app is written in the "functional" style (as an <literal>ApplicationContextInitializer</literal>). This is much faster on startup in Lambda than the traditional <literal>@Bean</literal> style, so if you don’t need <literal>@Beans</literal> (or <literal>@EnableAutoConfiguration</literal>) it’s a good choice. Warm starts are not affected.</simpara>
|
|
</note>
|
|
</section>
|
|
<section xml:id="_platfom_specific_features">
|
|
<title>Platfom Specific Features</title>
|
|
<section xml:id="_http_and_api_gateway">
|
|
<title>HTTP and API Gateway</title>
|
|
<simpara>AWS has some platform-specific data types, including batching of messages, which is much more efficient than processing each one individually. To make use of these types you can write a function that depends on those types. Or you can rely on Spring to extract the data from the AWS types and convert it to a Spring <literal>Message</literal>. To do this you tell AWS that the function is of a specific generic handler type (depending on the AWS service) and provide a bean of type <literal>Function<Message<S>,Message<T>></literal>, where <literal>S</literal> and <literal>T</literal> are your business data types. If there is more than one bean of type <literal>Function</literal> you may also need to configure the Spring Boot property <literal>function.name</literal> to be the name of the target bean (e.g. use <literal>FUNCTION_NAME</literal> as an environment variable).</simpara>
|
|
<simpara>The supported AWS services and generic handler types are listed below:</simpara>
|
|
<informaltable frame="all" rowsep="1" colsep="1">
|
|
<tgroup cols="4">
|
|
<colspec colname="col_1" colwidth="25*"/>
|
|
<colspec colname="col_2" colwidth="25*"/>
|
|
<colspec colname="col_3" colwidth="25*"/>
|
|
<colspec colname="col_4" colwidth="25*"/>
|
|
<thead>
|
|
<row>
|
|
<entry align="left" valign="top">Service</entry>
|
|
<entry align="left" valign="top">AWS Types</entry>
|
|
<entry align="left" valign="top">Generic Handler</entry>
|
|
<entry align="left" valign="top"></entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara>API Gateway</simpara></entry>
|
|
<entry align="left" valign="top"><simpara><literal>APIGatewayProxyRequestEvent</literal>, <literal>APIGatewayProxyResponseEvent</literal></simpara></entry>
|
|
<entry align="left" valign="top"><simpara><literal>org.springframework.cloud.function.adapter.aws.SpringBootApiGatewayRequestHandler</literal></simpara></entry>
|
|
<entry align="left" valign="top"></entry>
|
|
</row>
|
|
<row>
|
|
<entry align="left" valign="top"><simpara>Kinesis</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>KinesisEvent</simpara></entry>
|
|
<entry align="left" valign="top"><simpara>org.springframework.cloud.function.adapter.aws.SpringBootKinesisEventHandler</simpara></entry>
|
|
<entry align="left" valign="top"></entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
<simpara>For example, to deploy behind an API Gateway, use <literal>--handler org.springframework.cloud.function.adapter.aws.SpringBootApiGatewayRequestHandler</literal> in your AWS command line (in via the UI) and define a <literal>@Bean</literal> of type <literal>Function<Message<Foo>,Message<Bar>></literal> where <literal>Foo</literal> and <literal>Bar</literal> are POJO types (the data will be marshalled and unmarshalled by AWS using Jackson).</simpara>
|
|
</section>
|
|
</section>
|
|
</section>
|
|
<section xml:id="_azure_functions">
|
|
<title>Azure Functions</title>
|
|
<simpara>The <link xl:href="https://azure.microsoft.com">Azure</link> adapter bootstraps a Spring Cloud Function context and channels function calls from the Azure framework into the user functions, using Spring Boot configuration where necessary. Azure Functions has quite a unique, but invasive programming model, involving annotations in user code that are specific to the platform. The easiest way to use it with Spring Cloud is to extend a base class and write a method in it with the <literal>@FunctionName</literal> annotation which delegates to a base class method.</simpara>
|
|
<simpara>This project provides an adapter layer for a Spring Cloud Function application onto Azure.
|
|
You can write an app with a single <literal>@Bean</literal> of type <literal>Function</literal> and it will be deployable in Azure if you get the JAR file laid out right.</simpara>
|
|
<simpara>There is an <literal>AzureSpringBootRequestHandler</literal> which you must extend, and provide the input and output types as annotated method parameters (enabling Azure to inspect the class and create JSON bindings). The base class has two useful methods (<literal>handleRequest</literal> and <literal>handleOutput</literal>) to which you can delegate the actual function call, so mostly the function will only ever have one line.</simpara>
|
|
<simpara>Example:</simpara>
|
|
<programlisting language="java" linenumbering="unnumbered">public class FooHandler extends AzureSpringBootRequestHandler<Foo, Bar> {
|
|
@FunctionName("uppercase")
|
|
public Bar execute(
|
|
@HttpTrigger(name = "req", methods = { HttpMethod.GET,
|
|
HttpMethod.POST }, authLevel = AuthorizationLevel.ANONYMOUS)
|
|
Foo foo,
|
|
ExecutionContext context) {
|
|
return handleRequest(foo, context);
|
|
}
|
|
}</programlisting>
|
|
<simpara>This Azure handler will delegate to a <literal>Function<Foo,Bar></literal> bean (or a <literal>Function<Publisher<Foo>,Publisher<Bar>></literal>). Some Azure triggers (e.g. <literal>@CosmosDBTrigger</literal>) result in a input type of <literal>List</literal> and in that case you can bind to <literal>List</literal> in the Azure handler, or <literal>String</literal> (the raw JSON). The <literal>List</literal> input delegates to a <literal>Function</literal> with input type <literal>Map<String,Object></literal>, or <literal>Publisher</literal> or <literal>List</literal> of the same type. The output of the <literal>Function</literal> can be a <literal>List</literal> (one-for-one) or a single value (aggregation), and the output binding in the Azure declaration should match.</simpara>
|
|
<simpara>If your app has more than one <literal>@Bean</literal> of type <literal>Function</literal> etc. then you can choose the one to use by configuring <literal>function.name</literal>. Or if you make the <literal>@FunctionName</literal> in the Azure handler method match the function name it should work that way (also for function apps with multiple functions). The functions are extracted from the Spring Cloud <literal>FunctionCatalog</literal> so the default function names are the same as the bean names.</simpara>
|
|
<section xml:id="_notes_on_jar_layout_2">
|
|
<title>Notes on JAR Layout</title>
|
|
<simpara>You don’t need the Spring Cloud Function Web at runtime in Azure, so you can exclude this before you create the JAR you deploy to Azure, but it won’t be used if you include it so it doesn’t hurt to leave it in. A function application on Azure is an archive generated by the Maven plugin. The function lives in the JAR file generated by this project. The sample creates it as an executable jar, using the thin layout, so that Azure can find the handler classes. If you prefer you can just use a regular flat JAR file. The dependencies should <emphasis role="strong">not</emphasis> be included.</simpara>
|
|
</section>
|
|
<section xml:id="_build">
|
|
<title>Build</title>
|
|
<screen>./mvnw -U clean package</screen>
|
|
</section>
|
|
<section xml:id="_running_the_sample">
|
|
<title>Running the sample</title>
|
|
<simpara>You can run the sample locally, just like the other Spring Cloud Function samples:</simpara>
|
|
<simpara><?asciidoc-hr?></simpara>
|
|
<simpara><?asciidoc-hr?></simpara>
|
|
<simpara>and <literal>curl -H "Content-Type: text/plain" localhost:8080/function -d '{"value": "hello foobar"}'</literal>.</simpara>
|
|
<simpara>You will need the <literal>az</literal> CLI app (see <link xl:href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-java-maven">https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-java-maven</link> for more detail). To deploy the function on Azure runtime:</simpara>
|
|
<screen>$ az login
|
|
$ mvn azure-functions:deploy</screen>
|
|
<simpara>On another terminal try this: <literal>curl <link xl:href="https://<azure-function-url-from-the-log>/api/uppercase">https://<azure-function-url-from-the-log>/api/uppercase</link> -d '{"value": "hello foobar!"}'</literal>. Please ensure that you use the right URL for the function above. Alternatively you can test the function in the Azure Dashboard UI (click on the function name, go to the right hand side and click "Test" and to the bottom right, "Run").</simpara>
|
|
<simpara>The input type for the function in the Azure sample is a Foo with a single property called "value". So you need this to test it with something like below:</simpara>
|
|
<screen>{
|
|
"value": "foobar"
|
|
}</screen>
|
|
<note>
|
|
<simpara>The Azure sample app is written in the "non-functional" style (using <literal>@Bean</literal>). The functional style (with just <literal>Function</literal> or <literal>ApplicationContextInitializer</literal>) is much faster on startup in Azure than the traditional <literal>@Bean</literal> style, so if you don’t need <literal>@Beans</literal> (or <literal>@EnableAutoConfiguration</literal>) it’s a good choice. Warm starts are not affected.</simpara>
|
|
</note>
|
|
</section>
|
|
</section>
|
|
<section xml:id="_apache_openwhisk">
|
|
<title>Apache Openwhisk</title>
|
|
<simpara>The <link xl:href="https://openwhisk.apache.org/">OpenWhisk</link> adapter is in the form of an executable jar that can be used in a a docker image to be deployed to Openwhisk. The platform works in request-response mode, listening on port 8080 on a specific endpoint, so the adapter is a simple Spring MVC application.</simpara>
|
|
<section xml:id="_quick_start">
|
|
<title>Quick Start</title>
|
|
<simpara>Implement a POF (be sure to use the <literal>functions</literal> package):</simpara>
|
|
<screen>package functions;
|
|
|
|
import java.util.function.Function;
|
|
|
|
public class Uppercase implements Function<String, String> {
|
|
|
|
public String apply(String input) {
|
|
return input.toUpperCase();
|
|
}
|
|
}</screen>
|
|
<simpara>Install it into your local Maven repository:</simpara>
|
|
<screen>./mvnw clean install</screen>
|
|
<simpara>Create a <literal>function.properties</literal> file that provides its Maven coordinates. For example:</simpara>
|
|
<screen>dependencies.function: com.example:pof:0.0.1-SNAPSHOT</screen>
|
|
<simpara>Copy the openwhisk runner JAR to the working directory (same directory as the properties file):</simpara>
|
|
<screen>cp spring-cloud-function-adapters/spring-cloud-function-adapter-openwhisk/target/spring-cloud-function-adapter-openwhisk-2.0.0.BUILD-SNAPSHOT.jar runner.jar</screen>
|
|
<simpara>Generate a m2 repo from the <literal>--thin.dryrun</literal> of the runner JAR with the above properties file:</simpara>
|
|
<screen>java -jar -Dthin.root=m2 runner.jar --thin.name=function --thin.dryrun</screen>
|
|
<simpara>Use the following Dockerfile:</simpara>
|
|
<screen>FROM openjdk:8-jdk-alpine
|
|
VOLUME /tmp
|
|
COPY m2 /m2
|
|
ADD runner.jar .
|
|
ADD function.properties .
|
|
ENV JAVA_OPTS=""
|
|
ENTRYPOINT [ "java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "runner.jar", "--thin.root=/m2", "--thin.name=function", "--function.name=uppercase"]
|
|
EXPOSE 8080</screen>
|
|
<blockquote>
|
|
<note>
|
|
<simpara>you could use a Spring Cloud Function app, instead of just a jar with a POF in it, in which case you would have to change the way the app runs in the container so that it picks up the main class as a source file. For example, you could change the <literal>ENTRYPOINT</literal> above and add <literal>--spring.main.sources=com.example.SampleApplication</literal>.</simpara>
|
|
</note>
|
|
</blockquote>
|
|
<simpara>Build the Docker image:</simpara>
|
|
<screen>docker build -t [username/appname] .</screen>
|
|
<simpara>Push the Docker image:</simpara>
|
|
<screen>docker push [username/appname]</screen>
|
|
<simpara>Use the OpenWhisk CLI (e.g. after <literal>vagrant ssh</literal>) to create the action:</simpara>
|
|
<screen>wsk action create example --docker [username/appname]</screen>
|
|
<simpara>Invoke the action:</simpara>
|
|
<screen>wsk action invoke example --result --param payload foo
|
|
{
|
|
"result": "FOO"
|
|
}</screen>
|
|
</section>
|
|
</section>
|
|
</chapter>
|
|
</book> |