Sync docs from master to gh-pages

This commit is contained in:
buildmaster
2018-06-04 12:35:54 +00:00
parent 8dc4760492
commit ac7f4ab2be
7 changed files with 248 additions and 22 deletions

View File

@@ -16,16 +16,16 @@
<simpara>Spring Cloud Function is a project with the following high-level goals:</simpara>
<itemizedlist>
<listitem>
<simpara><emphasis>Promote the implementation of business logic via functions.</emphasis></simpara>
<simpara>Promote the implementation of business logic via functions.</simpara>
</listitem>
<listitem>
<simpara><emphasis>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.</emphasis></simpara>
<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><emphasis>Support a uniform programming model across serverless providers, as well as the ability to run standalone (locally or in a PaaS).</emphasis></simpara>
<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><emphasis>Enable Spring Boot features (auto-configuration, dependency injection, metrics) on serverless providers.</emphasis></simpara>
<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
@@ -352,21 +352,153 @@ plain text and JSON.</simpara>
<simpara>some binders will fail on startup if the message broker is not available and the function catalog contains suppliers that immediately produce messages when accessed. You can switch off the automatic publishing from suppliers on startup using the <literal>spring.cloud.function.strean.supplier.enabled=false</literal> flag.</simpara>
</note>
</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&#8217;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="_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
servlerless platforms. In the project there are adapters for
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 Oracle Fn platform has its own Spring Cloud Function adapter.</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&#8217;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 default).</simpara>
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&#8217;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-1.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>
</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 Spring Cloud Function Azure adapter trades the convenience of these annotations for portability of the function implementations. Instead of using the annotations you have to write some JSON by hand (at least for now) to guide the platform to call the right methods in the adapter.</simpara>
<simpara>The adapter has a generic http request handler that you can use.
There is a <literal>AzureSpringBootRequestHandler</literal> which you must extend, and provide the input and output types as type parameters (enabling Azure 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>.
The functions are extracted from the Spring Cloud <literal>FunctionCatalog</literal>.</simpara>
<section xml:id="_notes_on_jar_layout_2">
<title>Notes on JAR Layout</title>
<simpara>You don&#8217;t need the Spring Cloud Function Web at runtime in Azure, so you need to exclude this before you create the JAR you deploy to Azure.
A function application on Azure 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 here).
The sample app creates the shaded jar file, with an <literal>azure</literal> classifier for deploying in Azure.</simpara>
</section>
<section xml:id="_json_configuration">
<title>JSON Configuration</title>
<simpara>The Azure tooling needs to find some JSON configuration files to tell it how to deploy and integrate the function (e.g. which Java class to use as the entry point, and which triggers to use). Those files can be created with the Maven plugin for a non-Spring function, but the tooling doesn&#8217;t work yet with the adapter in its current form. There is an example <literal>function.json</literal> in the sample which hooks the function up as an HTTP endpoint:</simpara>
<screen>{
"scriptFile" : "../function-sample-azure-1.0.0.BUILD-SNAPSHOT-azure.jar",
"entryPoint" : "example.FooHandler.execute",
"bindings" : [ {
"type" : "httpTrigger",
"name" : "foo",
"direction" : "in",
"authLevel" : "anonymous",
"methods" : [ "get", "post" ]
}, {
"type" : "http",
"name" : "$return",
"direction" : "out"
} ],
"disabled" : false
}</screen>
</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 and some node.js fu (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://&lt;azure-function-url-from-the-log&gt;/api/uppercase">https://&lt;azure-function-url-from-the-log&gt;/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>
</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&lt;String, String&gt; {
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-1.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>