Serverless Spring@david_syer, 2018
Serverless Spring http://localhost:4000/decks/serverless-spring.html
1 of 30 30/01/2019, 10:04
Agenda
Serverless and functions
Servless platforms, including Riff
Spring Cloud Function
http://github.com/spring-cloud/spring-cloud-function
http://projectreactor.io/
Serverless Spring http://localhost:4000/decks/serverless-spring.html
2 of 30 30/01/2019, 10:04
Serverless
Event driven
Dynamic resource utilization, "scale to zero"
Billing per message
Focus on business logic
Easy integration with platform services
Serverless Spring http://localhost:4000/decks/serverless-spring.html
3 of 30 30/01/2019, 10:04
Serverless Use Cases
Web hooks
Background jobs
Glue code (hands-free integration)
CNCF WG Whitepaper
Serverless Spring http://localhost:4000/decks/serverless-spring.html
4 of 30 30/01/2019, 10:04
No Code is an Island
Credit: Yan Cui, https://theburningmonk.com
Serverless Spring http://localhost:4000/decks/serverless-spring.html
5 of 30 30/01/2019, 10:04
Service Block
Serverless Spring http://localhost:4000/decks/serverless-spring.html
6 of 30 30/01/2019, 10:04
Amazon Lambda
Serverless Spring http://localhost:4000/decks/serverless-spring.html
7 of 30 30/01/2019, 10:04
Serverless Spring http://localhost:4000/decks/serverless-spring.html
8 of 30 30/01/2019, 10:04
Google Cloud Function
Serverless Spring http://localhost:4000/decks/serverless-spring.html
9 of 30 30/01/2019, 10:04
Serverless Spring http://localhost:4000/decks/serverless-spring.html
10 of 30 30/01/2019, 10:04
Microsoft Azure Functions
Serverless Spring http://localhost:4000/decks/serverless-spring.html
11 of 30 30/01/2019, 10:04
Serverless Spring http://localhost:4000/decks/serverless-spring.html
12 of 30 30/01/2019, 10:04
Alibaba Function Compute
Serverless Spring http://localhost:4000/decks/serverless-spring.html
13 of 30 30/01/2019, 10:04
Serverless Spring http://localhost:4000/decks/serverless-spring.html
14 of 30 30/01/2019, 10:04
Riff
Serverless Spring http://localhost:4000/decks/serverless-spring.html
15 of 30 30/01/2019, 10:04
Serverless Spring http://localhost:4000/decks/serverless-spring.html
16 of 30 30/01/2019, 10:04
Serverless Providers
(J) Amazon Lambda
Google Cloud Functions
(J) Azure Function
(J) Alibaba
(J) Riff https://projectriff.io and PFS
(J) IBM OpenWhisk
(J) Oracle Fn
OpenFaaS
Fission
Kubeless
…
(J) = native Java support
Others can run Java, e.g. via custom container or node JRE launcher.
Serverless Spring http://localhost:4000/decks/serverless-spring.html
17 of 30 30/01/2019, 10:04
Java Util Function
public interface Function<T, R> {
R apply(T t);
}
public interface Consumer<T> {
void accept(T t);
}
public interface Supplier<T> {
T get();
}
Serverless Spring http://localhost:4000/decks/serverless-spring.html
18 of 30 30/01/2019, 10:04
Spring Cloud Function
@SpringBootApplication
public class Application {
@Bean
public Function<String, String> uppercase() {
return value -> value.toUpperCase();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Serverless Spring http://localhost:4000/decks/serverless-spring.html
19 of 30 30/01/2019, 10:04
Plain Old Functions
package functions;
public class Uppercase implements Function<String, String> {
String apply(String input) {
return input.toUpperCase();
}
}
Serverless Spring http://localhost:4000/decks/serverless-spring.html
20 of 30 30/01/2019, 10:04
AWS Cold Starts
Lambda throttles CPU resources when memory is constrained
… also billing is proportional to memory usage
… it’s not that simple
Serverless Spring http://localhost:4000/decks/serverless-spring.html
21 of 30 30/01/2019, 10:04
Spring Cloud Function
All the benefits of serverless, but with full access to Spring (dependency injection, integrations,
autoconfiguration) and build tools (testing, continuous delivery, run locally)
For Spring devs: a smaller, more familiar step than using FaaS APIs and UIs natively
For Functionistas: no need to know anything about Spring
Decouple lifecycle of business logic from runtime platform. Run the same code as a web endpoint,
a stream processor, or a task
Uniform programming model across serverless providers, and also able to run standalone
(locally or in a PaaS)
Serverless Spring http://localhost:4000/decks/serverless-spring.html
22 of 30 30/01/2019, 10:04
Project Reactor
public abstract class Flux<T> implements Publisher<T> {
...
}
public abstract class Mono<T> implements Publisher<T> {
...
}
Serverless Spring http://localhost:4000/decks/serverless-spring.html
23 of 30 30/01/2019, 10:04
Spring Cloud Function
@SpringBootApplication
public class Application {
@Bean
public Function<Flux<String>, Flux<String>> uppercase() {
return flux -> flux
.filter(this::isNotRude)
.map(String::toUpperCase);
}
boolean isNotRude(String word) {
...
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Serverless Spring http://localhost:4000/decks/serverless-spring.html
24 of 30 30/01/2019, 10:04
Spring Cloud Function Adapter
Serverless Spring http://localhost:4000/decks/serverless-spring.html
25 of 30 30/01/2019, 10:04
Serverless Spring http://localhost:4000/decks/serverless-spring.html
26 of 30 30/01/2019, 10:04
Spring Cloud Function
Programming model: @Beans of type Function, Consumer and Supplier, with Flux,
Mono, Publisher
1.
Component scan for functions (e.g. execute jar with no dependency on Spring at all)2.
Compile strings which are Java function bodies3.
Bind and invoke from an isolated classloader (JVM packing, platform)4.
Adapters for Spring MVC, Spring Cloud Stream, AWS Lambda, Azure, Riff and other
"serverless" service providers
5.
Serverless Spring http://localhost:4000/decks/serverless-spring.html
27 of 30 30/01/2019, 10:04
Learnings
Spring is not slow or "heavy" on its own, classes loaded is important1.
AWS Lambda containers are slow, Azure even more so2.
Functional bean definitions rule for fast startup3.
Functions are glue code anyway - not latency sensitive4.
Scale to Zero5.
Platform services and integration spaghetti6.
Serverless Spring http://localhost:4000/decks/serverless-spring.html
28 of 30 30/01/2019, 10:04
Links
Spring Cloud Function: https://github.com/spring-cloud/spring-cloud-function
Riff: https://github.com/projectriff/riff
Spring Boot Thin Launcher: https://github.com/dsyer/spring-boot-thin-launcher
Spring Initializr: http://start.spring.io
Spring Cloud: http://cloud.spring.io
Reactor: http://projectreactor.io
Serverless Spring http://localhost:4000/decks/serverless-spring.html
29 of 30 30/01/2019, 10:04
← →
1 / 23
Go to Slide: Go
Serverless Spring http://localhost:4000/decks/serverless-spring.html
30 of 30 30/01/2019, 10:04

Serverless Spring - Dave Syer

  • 1.
    Serverless Spring@david_syer, 2018 ServerlessSpring http://localhost:4000/decks/serverless-spring.html 1 of 30 30/01/2019, 10:04
  • 2.
    Agenda Serverless and functions Servlessplatforms, including Riff Spring Cloud Function http://github.com/spring-cloud/spring-cloud-function http://projectreactor.io/ Serverless Spring http://localhost:4000/decks/serverless-spring.html 2 of 30 30/01/2019, 10:04
  • 3.
    Serverless Event driven Dynamic resourceutilization, "scale to zero" Billing per message Focus on business logic Easy integration with platform services Serverless Spring http://localhost:4000/decks/serverless-spring.html 3 of 30 30/01/2019, 10:04
  • 4.
    Serverless Use Cases Webhooks Background jobs Glue code (hands-free integration) CNCF WG Whitepaper Serverless Spring http://localhost:4000/decks/serverless-spring.html 4 of 30 30/01/2019, 10:04
  • 5.
    No Code isan Island Credit: Yan Cui, https://theburningmonk.com Serverless Spring http://localhost:4000/decks/serverless-spring.html 5 of 30 30/01/2019, 10:04
  • 6.
    Service Block Serverless Springhttp://localhost:4000/decks/serverless-spring.html 6 of 30 30/01/2019, 10:04
  • 7.
    Amazon Lambda Serverless Springhttp://localhost:4000/decks/serverless-spring.html 7 of 30 30/01/2019, 10:04
  • 8.
  • 9.
    Google Cloud Function ServerlessSpring http://localhost:4000/decks/serverless-spring.html 9 of 30 30/01/2019, 10:04
  • 10.
  • 11.
    Microsoft Azure Functions ServerlessSpring http://localhost:4000/decks/serverless-spring.html 11 of 30 30/01/2019, 10:04
  • 12.
  • 13.
    Alibaba Function Compute ServerlessSpring http://localhost:4000/decks/serverless-spring.html 13 of 30 30/01/2019, 10:04
  • 14.
  • 15.
  • 16.
  • 17.
    Serverless Providers (J) AmazonLambda Google Cloud Functions (J) Azure Function (J) Alibaba (J) Riff https://projectriff.io and PFS (J) IBM OpenWhisk (J) Oracle Fn OpenFaaS Fission Kubeless … (J) = native Java support Others can run Java, e.g. via custom container or node JRE launcher. Serverless Spring http://localhost:4000/decks/serverless-spring.html 17 of 30 30/01/2019, 10:04
  • 18.
    Java Util Function publicinterface Function<T, R> { R apply(T t); } public interface Consumer<T> { void accept(T t); } public interface Supplier<T> { T get(); } Serverless Spring http://localhost:4000/decks/serverless-spring.html 18 of 30 30/01/2019, 10:04
  • 19.
    Spring Cloud Function @SpringBootApplication publicclass Application { @Bean public Function<String, String> uppercase() { return value -> value.toUpperCase(); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } Serverless Spring http://localhost:4000/decks/serverless-spring.html 19 of 30 30/01/2019, 10:04
  • 20.
    Plain Old Functions packagefunctions; public class Uppercase implements Function<String, String> { String apply(String input) { return input.toUpperCase(); } } Serverless Spring http://localhost:4000/decks/serverless-spring.html 20 of 30 30/01/2019, 10:04
  • 21.
    AWS Cold Starts Lambdathrottles CPU resources when memory is constrained … also billing is proportional to memory usage … it’s not that simple Serverless Spring http://localhost:4000/decks/serverless-spring.html 21 of 30 30/01/2019, 10:04
  • 22.
    Spring Cloud Function Allthe benefits of serverless, but with full access to Spring (dependency injection, integrations, autoconfiguration) and build tools (testing, continuous delivery, run locally) For Spring devs: a smaller, more familiar step than using FaaS APIs and UIs natively For Functionistas: no need to know anything about Spring Decouple lifecycle of business logic from runtime platform. Run the same code as a web endpoint, a stream processor, or a task Uniform programming model across serverless providers, and also able to run standalone (locally or in a PaaS) Serverless Spring http://localhost:4000/decks/serverless-spring.html 22 of 30 30/01/2019, 10:04
  • 23.
    Project Reactor public abstractclass Flux<T> implements Publisher<T> { ... } public abstract class Mono<T> implements Publisher<T> { ... } Serverless Spring http://localhost:4000/decks/serverless-spring.html 23 of 30 30/01/2019, 10:04
  • 24.
    Spring Cloud Function @SpringBootApplication publicclass Application { @Bean public Function<Flux<String>, Flux<String>> uppercase() { return flux -> flux .filter(this::isNotRude) .map(String::toUpperCase); } boolean isNotRude(String word) { ... } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } Serverless Spring http://localhost:4000/decks/serverless-spring.html 24 of 30 30/01/2019, 10:04
  • 25.
    Spring Cloud FunctionAdapter Serverless Spring http://localhost:4000/decks/serverless-spring.html 25 of 30 30/01/2019, 10:04
  • 26.
  • 27.
    Spring Cloud Function Programmingmodel: @Beans of type Function, Consumer and Supplier, with Flux, Mono, Publisher 1. Component scan for functions (e.g. execute jar with no dependency on Spring at all)2. Compile strings which are Java function bodies3. Bind and invoke from an isolated classloader (JVM packing, platform)4. Adapters for Spring MVC, Spring Cloud Stream, AWS Lambda, Azure, Riff and other "serverless" service providers 5. Serverless Spring http://localhost:4000/decks/serverless-spring.html 27 of 30 30/01/2019, 10:04
  • 28.
    Learnings Spring is notslow or "heavy" on its own, classes loaded is important1. AWS Lambda containers are slow, Azure even more so2. Functional bean definitions rule for fast startup3. Functions are glue code anyway - not latency sensitive4. Scale to Zero5. Platform services and integration spaghetti6. Serverless Spring http://localhost:4000/decks/serverless-spring.html 28 of 30 30/01/2019, 10:04
  • 29.
    Links Spring Cloud Function:https://github.com/spring-cloud/spring-cloud-function Riff: https://github.com/projectriff/riff Spring Boot Thin Launcher: https://github.com/dsyer/spring-boot-thin-launcher Spring Initializr: http://start.spring.io Spring Cloud: http://cloud.spring.io Reactor: http://projectreactor.io Serverless Spring http://localhost:4000/decks/serverless-spring.html 29 of 30 30/01/2019, 10:04
  • 30.
    ← → 1 /23 Go to Slide: Go Serverless Spring http://localhost:4000/decks/serverless-spring.html 30 of 30 30/01/2019, 10:04