diff --git a/docs/src/main/asciidoc/adapters/aws-readme.adoc b/docs/src/main/asciidoc/adapters/aws-readme.adoc new file mode 100644 index 000000000..391072ae4 --- /dev/null +++ b/docs/src/main/asciidoc/adapters/aws-readme.adoc @@ -0,0 +1,33 @@ +This project provides an adapter layer for a Spring Cloud Function application onto AWS Lambda. You can write an app with a single `@Bean` of type `Function`, `Consumer` or `Supplier` and it will be deployable in AWS if you get the JAR file laid out right. The best way to make it work is to include `spring-cloud-function-context` as a dependency, but not the higher level adapters (e.g. `spring-cloud-function-stream`). + +The adapter has a couple of generic request handlers that you can use. The most generic is `SpringBootStreamHandler`, which uses a Jackson `ObjectMapper` provided by Spring Boot to serialize and deserialize the objects in the function. There is also a `SpringBootRequestHandler` 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). + +If your app has more than one `@Bean` of type `Function` etc. then you can choose the one to use by configuring `function.name` (e.g. as `FUNCTION_NAME` environment variable in AWS). The functions are extracted from the Spring Cloud `FunctionCatalog` (searching first for `Function` then `Consumer` and finally `Supplier`). + +=== Notes on JAR Layout + +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 here). The sample app creates 2 jar files, one with an `aws` classifier for deploying in Lambda, and one executable (thin) jar that includes `spring-cloud-function-web` at runtime. Spring Cloud Function will try and locate a "main class" for you from the JAR file manifest, using the `Start-Class` attribute (which will be added for you by the Spring Boot tooling if you use the starter parent). If there is no `Start-Class` in your manifest you can use an environment variable `MAIN_CLASS` when you deploy the function to AWS. + +== Build + +---- +./mvnw -U clean package +---- + +== Upload + +Build the sample under `spring-cloud-function-samples/function-sample-aws` and upload the `-aws` jar file to Lambda. The handler can be `example.Handler` or `org.springframework.cloud.function.adapter.aws.SpringBootStreamHandler` (FQN of the class, _not_ a method reference, although Lambda does accept method references). + +Using the AWS command line tools it looks like this: + +---- +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 +---- + +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: + +---- +{ + "value": "test" +} +---- \ No newline at end of file diff --git a/docs/src/main/asciidoc/adapters/azure-readme.adoc b/docs/src/main/asciidoc/adapters/azure-readme.adoc new file mode 100644 index 000000000..630092b67 --- /dev/null +++ b/docs/src/main/asciidoc/adapters/azure-readme.adoc @@ -0,0 +1,72 @@ +This work is experimental. +This project provides an adapter layer for a Spring Cloud Function application onto Azure. +You can write an app with a single `@Bean` of type `Function` and it will be deployable in Azure if you get the JAR file laid out right. + +The adapter has a generic http request handler that you can use. +There is a `AzureSpringBootRequestHandler` 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). + +If your app has more than one `@Bean` of type `Function` etc. then you can choose the one to use by configuring `function.name`. +The functions are extracted from the Spring Cloud `FunctionCatalog`. + +=== Notes on JAR Layout + +You don'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 `azure` classifier for deploying in Azure. + +=== JSON Configuration + +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't work yet with the adapter in its current form. There is an example `function.json` in the sample which hooks the function up as an HTTP endpoint: + +``` +{ + "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 +} +``` + + +== Build + +---- +./mvnw -U clean package +---- + +== Running the sample + +You can run the sample locally, just like the other Spring Cloud Function samples: + +--- +./mvnw spring-boot:run +--- + +and `curl -H "Content-Type: text/plain" localhost:8080/function -d '{"value": "hello foobar"}'`. + +You will need the `az` CLI app and some node.js fu (see https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-java-maven for more detail). To deploy the function on Azure runtime: + +---- +$ az login +$ mvn azure-functions:deploy +---- + +On another terminal try this: `curl https:///api/uppercase -d '{"value": "hello foobar!"}'`. 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"). + +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: + +---- +{ + "value": "foobar" +} +---- \ No newline at end of file diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-openwhisk/README.md b/docs/src/main/asciidoc/adapters/openwhisk-readme.adoc similarity index 73% rename from spring-cloud-function-adapters/spring-cloud-function-adapter-openwhisk/README.md rename to docs/src/main/asciidoc/adapters/openwhisk-readme.adoc index e06a0a440..90605318a 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-openwhisk/README.md +++ b/docs/src/main/asciidoc/adapters/openwhisk-readme.adoc @@ -78,17 +78,12 @@ wsk action invoke example --result --param payload foo "result": "FOO" } ``` -# Examples +== Examples -The following examples are built based on the details and explanations above, on how to deploy Spring Cloud Functions on to [OpenWhisk](https://openwhisk.apache.org/) +The following examples are built based on the details and explanations above, on how to deploy Spring Cloud Functions on to https://openwhisk.apache.org/[OpenWhisk] -* [Spring Cloud Function PoF Example](https://github.com/redhat-developer-demos/ow-scf-fruiteason) +* https://github.com/redhat-developer-demos/ow-scf-fruiteason[Spring Cloud Function PoF Example]. This example shows how to use Spring Cloud Functions by defining simple Plain Old Function (POF) -This example shows how to use Spring Cloud Functions by defining simple Plain Old Function (POF) +* https://github.com/redhat-developer-demos/ow-scf-greeter[Spring Cloud Function Application Example]. This example shows how to use Spring Cloud Functions with a complete Spring Boot Application that has functions defined by extending `java.util.function.Function` interfaces. -* [Spring Cloud Function Application Example](https://github.com/redhat-developer-demos/ow-scf-greeter) - -This example shows how to use Spring Cloud Functions with a complete Spring Boot Application -that has functions defined by extending `java.util.function.Function` interfaces. - -The base docker images used for above examples is available [here](https://github.com/redhat-developer-demos/openwhisk-scf-docker). \ No newline at end of file +The base docker images used for above examples is available https://github.com/redhat-developer-demos/openwhisk-scf-docker[here]. \ No newline at end of file diff --git a/docs/src/main/asciidoc/spring-cloud-function.adoc b/docs/src/main/asciidoc/spring-cloud-function.adoc index 87fa74740..3f3072405 100644 --- a/docs/src/main/asciidoc/spring-cloud-function.adoc +++ b/docs/src/main/asciidoc/spring-cloud-function.adoc @@ -110,13 +110,17 @@ NOTE: some binders will fail on startup if the message broker is not available a 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 https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-adapters/spring-cloud-function-adapter-aws[AWS Lambda], https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-adapters/spring-cloud-function-adapter-azure[Azure], and https://github.com/spring-cloud/spring-cloud-function/tree/master/spring-cloud-function-adapters/spring-cloud-function-adapter-openwhisk[Apache -OpenWhisk]. The Oracle Fn platform has its own Spring Cloud Function adapter. +OpenWhisk]. The https://github.com/fnproject/fn[Oracle Fn platform] +has its own Spring Cloud Function adapter. And +https://projectriff.io[Riff] supports Java functions and its +https://github.com/projectriff/java-function-invoker[Java Function +Invoker] acts natively is an adapter for Spring Cloud Function jars. == Deploying a Packaged Function diff --git a/pom.xml b/pom.xml index 9558f5bfc..c737d2ac3 100644 --- a/pom.xml +++ b/pom.xml @@ -177,6 +177,54 @@ + + + docs + + ${main.basedir}/spring-cloud-function-adapters + + + + + org.apache.maven.plugins + maven-antrun-plugin + + + adapters + process-resources + + run + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + spring-snapshots diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/README.adoc b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/README.adoc index 391072ae4..636daf25e 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/README.adoc +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/README.adoc @@ -1,3 +1,5 @@ +// Do not edit this file (e.g. go instead to src/main/asciidoc) + This project provides an adapter layer for a Spring Cloud Function application onto AWS Lambda. You can write an app with a single `@Bean` of type `Function`, `Consumer` or `Supplier` and it will be deployable in AWS if you get the JAR file laid out right. The best way to make it work is to include `spring-cloud-function-context` as a dependency, but not the higher level adapters (e.g. `spring-cloud-function-stream`). The adapter has a couple of generic request handlers that you can use. The most generic is `SpringBootStreamHandler`, which uses a Jackson `ObjectMapper` provided by Spring Boot to serialize and deserialize the objects in the function. There is also a `SpringBootRequestHandler` 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). diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-azure/README.adoc b/spring-cloud-function-adapters/spring-cloud-function-adapter-azure/README.adoc index 630092b67..8155de4c7 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-azure/README.adoc +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-azure/README.adoc @@ -1,3 +1,5 @@ +// Do not edit this file (e.g. go instead to src/main/asciidoc) + This work is experimental. This project provides an adapter layer for a Spring Cloud Function application onto Azure. You can write an app with a single `@Bean` of type `Function` and it will be deployable in Azure if you get the JAR file laid out right. diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-openwhisk/README.adoc b/spring-cloud-function-adapters/spring-cloud-function-adapter-openwhisk/README.adoc new file mode 100644 index 000000000..d9c8394fa --- /dev/null +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-openwhisk/README.adoc @@ -0,0 +1,91 @@ +// Do not edit this file (e.g. go instead to src/main/asciidoc) + +Implement a POF (be sure to use the `functions` package): + +``` +package functions; + +import java.util.function.Function; + +public class Uppercase implements Function { + + public String apply(String input) { + return input.toUpperCase(); + } +} +``` + +Install it into your local Maven repository: + +``` +./mvnw clean install +``` + +Create a `function.properties` file that provides its Maven coordinates. For example: + +``` +dependencies.function: com.example:pof:0.0.1-SNAPSHOT +``` + +Copy the openwhisk runner JAR to the working directory (same directory as the properties file): + +``` +cp spring-cloud-function-adapters/spring-cloud-function-adapter-openwhisk/target/spring-cloud-function-adapter-openwhisk-1.0.0.BUILD-SNAPSHOT.jar runner.jar +``` + +Generate a m2 repo from the `--thin.dryrun` of the runner JAR with the above properties file: + +``` +java -jar -Dthin.root=m2 runner.jar --thin.name=function --thin.dryrun +``` + +Use the following Dockerfile: + +``` +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 +``` + +> NOTE: 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 `ENTRYPOINT` above and add `--spring.main.sources=com.example.SampleApplication`. + +Build the Docker image: + +``` +docker build -t [username/appname] . +``` + +Push the Docker image: + +``` +docker push [username/appname] +``` + +Use the OpenWhisk CLI (e.g. after `vagrant ssh`) to create the action: + +``` +wsk action create example --docker [username/appname] +``` + +Invoke the action: + +``` +wsk action invoke example --result --param payload foo +{ + "result": "FOO" +} +``` +== Examples + +The following examples are built based on the details and explanations above, on how to deploy Spring Cloud Functions on to https://openwhisk.apache.org/[OpenWhisk] + +* https://github.com/redhat-developer-demos/ow-scf-fruiteason[Spring Cloud Function PoF Example]. This example shows how to use Spring Cloud Functions by defining simple Plain Old Function (POF) + +* https://github.com/redhat-developer-demos/ow-scf-greeter[Spring Cloud Function Application Example]. This example shows how to use Spring Cloud Functions with a complete Spring Boot Application that has functions defined by extending `java.util.function.Function` interfaces. + +The base docker images used for above examples is available https://github.com/redhat-developer-demos/openwhisk-scf-docker[here]. \ No newline at end of file