Spring Cloud Function &Project riff
Japan Spring User Group 2018-02-06Toshiaki Maki (@making) / Pivotal
• Keynote day2• https://www.youtube.com/watch?
v=mlRSp9Z3zwY• Serverless Spring
• https://www.youtube.com/watch?v=mPOl3024R4s
Featured Sessions
CONTAINERSEVENT-DRIVEN
FUNCTIONS
DATA SERVICESMICROSERVICES
Batches
MONOLITHIC APPLICATIONS
Companies have many ways to package and run their workloads in the cloud
Their goal: pick the right runtime for each workload
CONTAINERSEVENT-DRIVEN
FUNCTIONS
DATA SERVICESMICROSERVICES
Batches
MONOLITHIC APPLICATIONS
IaaS
ContainerOrchestrator (CaaS)
ApplicationPlatform(PaaS)
Serverless Functions(FaaS)
Why Serverless?
8
1. Narrowly-scoped units of code, and the simplicity of built-in event
integration, contribute to software development efficiencies.
2. Functions which don't consume resources when idle can provide
significant resource efficiencies.
3. Applying serverless to distributed computing brings operational
efficiencies based on automated event-based scheduling and self-scaling.
Who is offering FaaS?Hosted• AWS Lambda• Azure Functions• Google Cloud FunctionsOn-Prem / OSS• riff (https://github.com/projectriff )• Oracle fn• OpenWhisk • Fission• Kubeless• OpenFaaS
• Focus on the implementation of business logic via functions
• Decouple the business logic from any specific runtime (HTTP, Message)
• Support a uniform programming model across FaaS providers
• Run standalone (locally or in a PaaS)• Enable Spring Boot features on FaaS providers
Spring Cloud Function
Spring Cloud Function
• FaaS Portable • Run in Spring Boot• REST, Tasks, or Streams
https://github.com/spring-cloud/spring-cloud-function
HTTP Message
Functions
Spring Cloud Function Web
Spring Cloud Function Message
Spring Boot
HTTP Message
Functions
Spring Cloud Function Adapter
Faas Provider
HTTP Message
Functions
Spring Cloud Function Adapter
Faas Provider
Currently • AWS lambda • Azure Function • Apache OpenWhisk are supported
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();}
Write a functionpackage functions;import java.util.function.Function;public class Greeter implements Function<String, String> {
public String apply(String name) { return "Hello " + name; }
}
Spring Cloud Function (Web)
<dependency> <groupId>org.springframework.cloud<groupId> <artifactId>spring-cloud-function-web</artifactId> <version>1.0.0.M3</version></dependency>
Spring Cloud Function (Message)
<dependency> <groupId>org.springframework.cloud<groupId> <artifactId>spring-cloud-function-message</artifactId> <version>1.0.0.M3</version></dependency>
Spring Cloud Function@SpringBootApplicationpublic class App { @Bean public Greeter greeter() { return new Greeter(); } public static void main(String[] args) { SpringApplication.run(App.class, args); }}
Spring Cloud Function
@SpringBootApplication@FunctionScanpublic class App { public static void main(String[] args) { SpringApplication.run(App.class, args); }}
Spring Cloud Function (Web)
$ curl localhost:8080/greeter \ -w '\n' \ -H "Content-Type: text/plain" \ -d "JSUG"
Hello JSUG
DEMOhttps://github.com/making/demo-function
Project riffhttps://projectriff.io/
riff is for functions
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
what is riff?
24
riff provides developers with a service for executing Functions in response to Events.Features★ event streaming★ polyglot★ Kubernetes-native
Eventsf(x)
f(x)
f(x)
Functionsf(x)
riff
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 25
🚧
ImmediateInstant-on
ConsistentImmutableContainers
EfficientScale to Zero
Choice of Compromises:
• Launch Function containers on demand => Slow start
• Inject Function code into running containers=> Breaks container immutability
• Keep Function containers running => Pay for idle resources
ICEWant serverless?... pick 2
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
functions are packaged as containers
26
Function Invoker
Function Code
Container Base Image
Function Layer
Container Registry
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
functions are packaged as containers
26
Function Invoker
Function Code
Container Base Image
Function Layer
Container Registry
Currently • Java • JavaScript • Bash • Python are supported
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
sidecars connect functions with event brokers
27
Function Pod
Function ContainerSidecar Container
EventBroker
broker-specific API
binder dispatcher invoker function
HTTP gRPC stdio pipes
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
sidecars connect functions with event brokers
27
Function Pod
Function ContainerSidecar Container
EventBroker
broker-specific API
binder dispatcher invoker function
HTTP gRPC stdio pipes
+ maybe rsocket?
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 28
functions and topics are Kubernetes resources
Function Controller
Function YAML - name - input / output topics - artifact / params
Topic YAML - name - params
KafkaHttp Gateway
Topic Controller
Sc Fn
Pod
Sc Fn
Pod
Sc Fn
Pod
k8s API
Unless otherwise indicated, these slides are © 2013-2017 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
functions scale with eventsriff function controller★ interacts with K8s API ★ scales functions 0-1 and 1-N★ monitors event-lag in Kafka
29
Function Controller
Event Broker
f(x)f(x)
f(x)
k8s API
kubectl -n kube-system create serviceaccount tiller kubectl create clusterrolebinding tiller \ --clusterrole cluster-admin \ --serviceaccount=kube-system:tiller helm init --service-account=tiller helm repo add riffrepo https://riff-charts.storage.googleapis.com helm repo update
Set up helm
Installing riff via helm
helm install riffrepo/riff --name demo \ --version 0.0.3-rbac \ --set httpGateway.service.type=NodePort
Installing riff via helmSet up riff
DEMOhttps://github.com/making/demo-riff