Adds Spring Cloud Function example (#566)

This commit is contained in:
Matthew Gilliard
2017-12-06 00:02:32 +00:00
committed by Chad Arimura
parent 7cba25c065
commit 77ca7f450f
5 changed files with 207 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
# Example Spring Cloud Function
This is an example [spring cloud function](https://github.com/spring-cloud/spring-cloud-function)
project running on Fn using the
[`SpringCloudFunctionInvoker`](/runtime/src/main/java/com/fnproject/fn/runtime/spring/SpringCloudFunctionInvoker.java).
Firstly, if you have used `fn` before you'll want to make sure you have the latest runtime image which includes the Spring support:
```bash
$ docker pull fnproject/fdk-java:latest
```
Then you can build and deploy the app
```bash
fn build
fn deploy --local --app spring-cloud-fn
# Set up a couple of routes for different functions
fn routes create spring-cloud-fn /upper
fn routes config set spring-cloud-fn /upper FN_SPRING_FUNCTION upperCase
fn routes create spring-cloud-fn /lower
fn routes config set spring-cloud-fn /lower FN_SPRING_FUNCTION lowerCase
```
Now you can call those functions using `fn call` or curl:
```bash
$ echo "Hi there" | fn call spring-cloud-fn /upper
HI THERE
$ curl -d "Hi There" http://localhost:8080/r/spring-cloud-fn/lower
hi there
```
## Code walkthrough
```java
@Configuration
```
Defines that the class is a
[Spring configuration class](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html)
with `@Bean` definitions inside of it.
```java
@Import(ContextFunctionCatalogAutoConfiguration.class)
```
Specifies that this configuration uses a [`InMemoryFunctionCatalog`](https://github.com/spring-cloud/spring-cloud-function/blob/a973b678f1d4d6f703a530e2d9e071b6d650567f/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/InMemoryFunctionCatalog.java)
that provides the beans necessary
for the `SpringCloudFunctionInvoker`.
```java
...
@FnConfiguration
public static void configure(RuntimeContext ctx) {
ctx.setInvoker(new SpringCloudFunctionInvoker(SCFExample.class));
}
```
Sets up the Fn Java FDK to use the SpringCloudFunctionInvoker which performs function discovery and invocation.
```java
// Unused - see https://github.com/fnproject/fdk-java/issues/113
public void handleRequest() { }
```
Currently the runtime expects a method to invoke, however this isn't used in the SpringCloudFunctionInvoker so we declare an empty method simply to keep the runtime happy. This will not be necessary for long - see the linked issue on GitHub.
```java
@Bean
public Function<String, String> upperCase(){
return String::toUpperCase;
}
@Bean
public Function<String, String> lowerCase(){
return String::toLowerCase;
}
```
Finally the heart of the configuration; the bean definitions of the functions to invoke.
Note that these methods are not the functions themselves. They are factory methods which *return* the functions. As the Beans are constructed by Spring it is possible to use `@Autowired` dependency injection.

View File

@@ -0,0 +1,7 @@
name: fn-example/spring-cloud-fn
version: 0.0.1
runtime: java8
cmd: com.fnproject.fn.examples.SCFExample::handleRequest
format: http
timeout: 30
path: /fn

View File

@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<fdk.version>1.0.52</fdk.version>
</properties>
<groupId>com.fnproject.fn.examples</groupId>
<artifactId>spring-fn</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.fnproject.fn</groupId>
<artifactId>api</artifactId>
<version>${fdk.version}</version>
</dependency>
<dependency>
<groupId>com.fnproject.fn</groupId>
<artifactId>fn-spring-cloud-function</artifactId>
<version>${fdk.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>fn-maven-releases</id>
<url>https://dl.bintray.com/fnproject/fnproject</url>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,53 @@
package com.fnproject.fn.examples;
import com.fnproject.fn.api.FnConfiguration;
import com.fnproject.fn.api.RuntimeContext;
import com.fnproject.springframework.function.SpringCloudFunctionInvoker;
import org.springframework.cloud.function.context.ContextFunctionCatalogAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import reactor.core.publisher.Flux;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
@Configuration
@Import(ContextFunctionCatalogAutoConfiguration.class)
public class SCFExample {
@FnConfiguration
public static void configure(RuntimeContext ctx) {
ctx.setInvoker(new SpringCloudFunctionInvoker(SCFExample.class));
}
// Unused - see https://github.com/fnproject/fdk-java/issues/113
public void handleRequest() { }
public static class B{
private String xxx;
public B() {}
public B(String xxx) {
this.xxx = xxx;
}
public String getXxx() {
return xxx;
}
public void setXxx(String xxx) {
this.xxx = xxx;
}
}
@Bean
public Function<B, String> consumer(){
return B::getXxx;
}
}