Code Monkey home page Code Monkey logo

fn-spring-cloud-function-example's Introduction

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

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:

$ 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

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

fn-spring-cloud-function-example's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.