2. 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
3. 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
4. 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
5. 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
17. 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
18. 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
19. 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
20. 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
21. 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
22. 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
23. 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
24. 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
25. Spring Cloud Function Adapter
Serverless Spring http://localhost:4000/decks/serverless-spring.html
25 of 30 30/01/2019, 10:04
27. 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
28. 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
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