fnproject / fn-spring-cloud-function-example

Example project showing Spring Cloud Function support on Fn Project
2 stars 5 forks source link

Example Spring Cloud Function

This is an example spring cloud function project running on Fn using the SpringCloudFunctionInvoker.

Firstly, if you have used fn before you'll want to make sure you have the latest runtime image which includes the Spring support:

$ docker pull fnproject/fdk-java:latest

Then you can build and deploy the app

Once the app is deployed you need to make the route through API Gateway https://docs.oracle.com/en-us/iaas/developer-tutorials/tutorials/functions/func-api-gtw/01-summary.htm

fn build
fn deploy --app <app_name>

Try invoking the image through command

fn invoke <app_name> <function_name>

Double-check that the VCN includes an internet gateway or service gateway. For Oracle Functions to be able to access Oracle Cloud Infrastructure Registry to pull an image, the VCN must include an internet gateway or a service gateway. Follow the link https://docs.oracle.com/en-us/iaas/Content/Functions/Tasks/functionstroubleshooting_topic-Issues-invoking-functions.htm

Under the policy, check for the policy for allowing all the compartment user to have access over the url

ALLOW any-user to use functions-family in <compartment> where ALL {request.principal.type= 'ApiGateway', request.resource.compartment.id = '<compartment_id>'}

Now you can call those functions using fn invoke or curl:

$ echo "Hi there" | fn invoke spring-cloud-fn 
Hello world

$ curl -d "Bob" http://<url-apigateway>/hello
Hello Bob

Code walkthrough

@Configuration

Defines that the class is a Spring configuration class with @Bean definitions inside of it.

@Import(ContextFunctionCatalogAutoConfiguration.class)

Specifies that this configuration uses a InMemoryFunctionCatalog that provides the beans necessary for the SpringCloudFunctionInvoker.

    ...
    @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.

    // 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.

    @Bean
    public Function<String, String> function(){
        return value -> "Hello, " + ((value == null || value.isEmpty()) ? "world"  : value ) + "!";
        }

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.