An introduction to Spring Cloud Function

Spring Cloud Function is a project which helps developers bring the power of Spring Boot features (auto-configure, metrics, ..) on Serverless platforms, but not only.

More importantly, it abstracts all the details concerning the infrastructure, the transport and the provider.

The following function :

@Bean
public Function<String, String> uppercase() {
    return String::toUpperCase;
}

can be exposed as an http endpoint, a message consumer or a task processor and be run locally or on any of the serverless platform providers like AWS or even as a Knative service which will take care of scaling down your function app to zero in case of inactivity.


High Level Concepts

In a nutshell, the Spring Cloud Function project will detect and wrap your beans of type Function, Consumer or Supplier and expose as http endpoint and/or message stream listeners/publishers with the most common message brokers such as RabbitMQ or Kafka.


Getting Started

The best way to get started is to initiate a project on start.spring.io . We will expose our function to the web layer, so you will need to select the following dependencies :

  • Function
  • Spring Web

Click on generate, download the zipfile and extract it.

The content will be a standard spring boot application and you can immediately start writing your function as a bean and start the spring boot application.


Amend the main Application class and add the following bean :

@Bean
public Function<String, String> uppercase() {
    return String::toUpperCase;
}

Launch the application :

mvn spring-boot:run

In another terminal window, you can make an http request passing a string to be uppercased by the function :

echo 'hello' | http localhost:8080

---

$ echo 'hello' | http localhost:8080
HTTP/1.1 200
Content-Length: 6
Content-Type: application/json
Date: Sat, 16 Jan 2021 20:24:11 GMT
Keep-Alive: timeout=60
accept: application/json, */*;q=0.5
accept-encoding: gzip, deflate
connection: keep-alive, keep-alive
contenttype: application/json
host: localhost:8080
scf-func-name: uppercase
timestamp: 1610828651105
user-agent: HTTPie/2.2.0

HELLO

Since we’re having only one Function, she is automatically mapped to the root path / , we’ll see in a further blog post how to route functions.


Let’s add another bean of type Consumer :

@Bean
public Consumer<String> consume() {
    return str -> System.out.println("Received " + str);
}

It will automatically be mapped under the /consume endpoint. Let’s try it out :

echo 'hello' | http localhost:8080/consume
2021-01-16 21:31:10.845  INFO 48020 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-01-16 21:31:10.846  INFO 48020 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
hello



Type Conversion

There are default Message Converters that can transform the input of the transport layer to the type specified as input to the function, for example let’s take the following function :

@Bean
public Function<Person, String> fullName() {
	return person -> String.format("%s %s", person.getFirstName(), person.getLastName());
}

Where the Person class is :

package com.cwillemsen.cloudnative.springcloudfunction;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Person {
    private String firstName;
    private String lastName;
}

The following http request will return the desired output from the function :

http POST localhost:8080/fullName firstName=john lastName=doe

HTTP/1.1 200
Content-Length: 8
Content-Type: application/json
Date: Sun, 17 Jan 2021 15:45:24 GMT
Keep-Alive: timeout=60
accept: application/json, */*;q=0.5
accept-encoding: gzip, deflate
connection: keep-alive, keep-alive
contenttype: application/json
host: localhost:8080
scf-func-name: fullName
timestamp: 1610898324430
user-agent: HTTPie/2.2.0

john doe

Summary

We’ve seen the basic concepts of the Spring Cloud Function project and how it enables developers to only focus on writing the business logic by implementing it as Functions.

In further blog posts, we’ll see how to route, compose and deploy functions.

You can find the source code used in this post here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s