Spring Cloud Function

Introduction

Spring Cloud Function is a project with the following high-level goals:

  • Promote the implementation of business logic via functions.
  • Decouple the development lifecycle of business logic from any specific runtime target so that the same code can run as a web endpoint, a stream processor, or a task.
  • Support a uniform programming model across serverless providers, as well as the ability to run standalone (locally or in a PaaS).
  • Enable Spring Boot features (auto-configuration, dependency injection, metrics) on serverless providers.

It abstracts away all of the transport details and infrastructure, allowing the developer to keep all the familiar tools and processes, and focus firmly on business logic.

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-function-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-function-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-function-web</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>

Customer.java

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Customer {
private Long id;
private String firstName;
private String lastName;
private LocalDateTime birthdate;
}

MainApp.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

@SpringBootApplication
public class SpringCloudFunctionExampleApplication {

public static void main(String[] args) {
SpringApplication.run(SpringCloudFunctionExampleApplication.class, args);
}

List<Customer> customers;

public SpringCloudFunctionExampleApplication() {
customers = new ArrayList<>();
customers.add(Customer.builder().id(1L).firstName("John").lastName("Doe").birthdate(LocalDateTime.now()).build());
customers.add(Customer.builder().id(2L).firstName("Jane").lastName("Doe").birthdate(LocalDateTime.now()).build());
customers.add(Customer.builder().id(3L).firstName("Mike").lastName("Harford").birthdate(LocalDateTime.now()).build());
customers.add(Customer.builder().id(4L).firstName("Alex").lastName("Kerr").birthdate(LocalDateTime.now()).build());
customers.add(Customer.builder().id(5L).firstName("Matt").lastName("Wixson").birthdate(LocalDateTime.now()).build());
}

@Bean
public Supplier<Flux<Customer>> getCustomers() {
return () -> Flux.fromIterable(customers);
}

@Bean
public Function<Long, Customer> getCustomerById() {
return id -> {
System.out.println("Received request for customer - " + id);
return customers.stream()
.filter(customer -> id.equals(customer.getId()))
.findAny()
.orElse(null);
};
}

@Bean
public Function<Customer, Mono<Void>> processCustomerReactive(){
return customer -> {
System.out.println("Received reactive customer firstName and LastName : "
+ customer.getFirstName() + ":" + customer.getLastName());
customers.add(customer);
return Mono.empty();
};
}

@Bean
public Consumer<Flux<Customer>> processListOfCustomersReactive(){
return customerFlux -> {
List<Customer> customers = customerFlux
.collectList()
.doOnNext(System.out::println)
.block();
this.customers.addAll(customers);
};
}
}

Endpoint Response:

#

GET `http://localhost:8080/getCustomers`

GET `http://localhost:8080/getCustomerById/1`

POST ->

```
curl --location --request POST 'http://localhost:8080/processCustomerReactive' \
--header 'Content-Type: application/json' \
--data-raw '{
"id": 6,
"firstName": "Laxmi",
"lastName": "Dekate",
"birthdate": "2022-08-03T18:00:00.727953"
}'
```

POST ->

```
curl --location --request POST 'http://localhost:8080/processListOfCustomersReactive' \
--header 'Content-Type: application/json' \
--data-raw '[
{
"id": 7,
"firstName": "Neha",
"lastName": "Parate",
"birthdate": "2022-08-03T18:00:00.727953"
},
{
"id": 8,
"firstName": "Sachin",
"lastName": "Nimje",
"birthdate": "2022-08-03T18:00:00.727953"
}
]'
```

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
PA

PA

Java Developer and enthusiast