!1
Serverless with
Spring Cloud Function, Knative and riff
2018-11-06
Toshiaki Maki (@making / tmaki@pivotal.io)
SpringOne Tour #s1t
Who am I ?
2
Toshiaki Maki (@making) https://blog.ik.am
Advisory Solutions Architect @Pivotal Japan
Spring / Cloud Foundry / Concourse / Kubernetes / BOSH
Agenda
!3
• Serverless?
• Spring Cloud Function
• Knative and riff
Agenda
!4
• Serverless?
• Spring Cloud Function
• Knative and riff
Serverless??
!5
• Dynamic resource utilization, "scale to N"
and "scale to zero"
• Easy integration with platform services
• Billing per message
• Focus on business logic
Serverless??
!6
• Dynamic resource utilization, "scale to N"
and "scale to zero"
• Easy integration with platform services
• Billing per message
• Focus on business logic
Knative an riff
Spring Cloud Function
Cloud Abstraction
!7
Cloud Abstraction
!8
Serverless? FaaS?
!9
Serverless
FaaS
λ
Agenda
!10
• Serverless?
• Spring Cloud Function
• Knative and riff
Spring Cloud Function
!11
• 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
!12
HTTP Message
Spring Boot
Spring Cloud Function
Web
Spring Cloud Stream
λ λ λ λ λ
!13
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();
}
!14
Write a function
package functions;
import java.util.function.Function;
public class Hello
implements Function<String, String> {
public String apply(String s) {
return "Hello " + s + "!";
}
}
Invoke a function via HTTP
!15
λ
HTTP
Spring Cloud Function
!16
HTTP Message
Spring Boot
Spring Cloud Function
Web
Spring Cloud Stream
λ λ λ λ λ
!17
Spring Cloud Function (Web)
<dependency>
<groupId>org.springframework.cloud<groupId>
<artifactId>spring-cloud-starter-function-web</artifactId>
</dependency>
!18
Spring Cloud Function
@SpringBootApplication
public class App {
@Bean
public Hello hello() {
return new Hello();
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
!19
Spring Cloud Function
@SpringBootApplication
public class App {
@Bean
public Function<String, String> hello() {
return name -> "Hello " + s + "!";
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
!20
Spring Cloud Function (Web)
$ curl localhost:8080/hello 
-w 'n' 
-H "Content-Type: text/plain" 
-d "Spring"
Hello Spring!
DEMO
https://github.com/making/hello-function
!22
λ
Message
Invoke a function via Messaging
Spring Cloud Function
!23
HTTP Message
Spring Boot
Spring Cloud Function
Web
Spring Cloud Stream
λ λ λ λ λ
!24
Spring Cloud Stream + Function
<dependency>
<groupId>org.springframework.cloud<groupId>
<artifactId>spring-cloud-function-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud<groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
!25
Spring Cloud Function Spring Cloud Stream
j.u.f.Supplier Source
j.u.f.Function Processor
j.u.f.Consumer Sink
j.u.f = java.util.functions
!26
Spring Cloud Function + Stream
@SpringBootApplication
@EnableBinding(Processor.class)
public class App {
@Bean
public Function<String, String> hello() {
return name -> "Hello " + s + "!";
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
!27
Spring Cloud Function + Stream
spring.cloud.stream.bindings.input.destination=demo-in
spring.cloud.stream.bindings.input.group=hello
spring.cloud.stream.bindings.output.destination=demo-out
spring.cloud.stream.function.definition=hello
DEMO
https://github.com/making/hello-function/tree/rabbit
!29
demo-out
demo-in.hello
λ
Exchange
Exchange
Queue
demo-in Provisioned by
Spring Cloud Stream
!30
demo-out
demo-in.hello
λ print
Exchange
Exchange
Queue
Queue
demo-in
!31
demo-out
demo-in.hello
λ print
Exchange
Exchange
Queue
Queue
demo-in
λ
!32
demo-out
demo-in.hello
λ print
Exchange
Exchange
Queue
Queue
demo-in
λ
spring.cloud.stream.function.definition=hello|uppercase
hello uppercase
Serverless Platform Adapters
!33
HTTP Message
Spring Cloud Function Adapter
Faas Provider
λ λ λ λ λ
Serverless Platform Adapters
!34
HTTP Message
Spring Cloud Function Adapter
Faas Provider
λ λ λ λ λ
Amazon AWS Lambda Apache OpenWhisk Azure Functions Project Riff
!35
<dependency>
<groupId>org.springframework.cloud<groupId>
<artifactId>spring-cloud-function-adapter-azure</artifactId>
</dependency>
Example: Azure Functions
Example: Azure Functions
!36
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotations.*;
import org.springframework.cloud.function.adapter.azure.*;
public class AzureHandler extends
AzureSpringBootRequestHandler<String, String> {
@FunctionName("hello")
public String
execute(@HttpTrigger(name="req",method=...)) {
return super.handleRequest(s, ctx);
}
}
DEMO
https://github.com/making/hello-function/tree/azure
Agenda
!38
• Serverless?
• Spring Cloud Function
• Knative and riff
Serverless? FaaS?
!39
Serverless
FaaS
λ
Serverless? FaaS?
!40
Serverless
FaaS
λ
Knative
!41
Knative (pronounced kay-nay-tiv) extends
Kubernetes to provide a set of middleware
components that are essential to build modern,
source-centric, and container-based applications
that can run anywhere: on premises, in the cloud,
or even in a third-party data center.
(from https://github.com/knative/docs)
Overview of Knative
!42
Kubernetes-based platform to build, deploy,

and manage modern serverless workloads
Three core components:
• Serving - Request-driven compute that can scale to zero
• Build - Source-to-container build orchestration
• Eventing - Management and delivery of events
100% open source. Created by Google with contributions
from Pivotal and others
Knative Custom Resource Definitions
!43
BuildTemplate
Source
Channel
Build
Flow
manages
refers to
refers to
creates and
registers
instance of
Eventing
Serving
Build
Knative Custom Resource Definitions
!44
BuildTemplate
Source
Channel
Build
Flow
manages
refers to
refers to
creates and
registers
instance of
Eventing
Serving
Build
Run app/function on Knative Serving
!45
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: hello
namespace: default
spec:
runLatest:
configuration:
revisionTemplate:
spec:
container:
image: making/hello-function
Run app/function on Knative Serving
!46
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: hello
namespace: default
spec:
runLatest:
configuration:
revisionTemplate:
spec:
container:
image: making/hello-function
http://hello.default.<your-domain>
!47
$ curl ${INGRESS_IP} 
-w 'n' 
-H "Host: hello.default.<your-domain>" 
-H "Content-Type: text/plain" 
-d "Knative"
Hello Knative!
Knative Custom Resource Definitions
!48
BuildTemplate
Source
Channel
Build
Flow
manages
refers to
refers to
creates and
registers
instance of
Eventing
Serving
Build
Source to URL
!49
apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: hello
namespace: default
spec:
runLatest:
configuration:
build:
source:
git: {"url": "https://github.com/making/hello-function.git"}
template
name: my-build-template
arguments: {"name":"IMAGE", "value":"making/hello-function"}
configuration:
# .. same as previous example
BuildTemplate
!50
apiVersion: serving.knative.dev/v1alpha1
kind: BuildTemplate
metadata:
name: my-build-template
namespace: default
spec:
parameters:
- {"name":"IMAGE", "description":"The name of the image to push"}
- ...
steps:
- name: step1
image: foobar/an-image-for-build1
args: ["..."]
- name: step2
image: foobar/an-image-for-build2
args: ["..."]
https://github.com/knative/build-templates
BuildTemplate
!51
apiVersion: serving.knative.dev/v1alpha1
kind: BuildTemplate
metadata:
name: my-build-template
namespace: default
spec:
parameters:
- {"name":"IMAGE", "description":"The name of the image to push"}
- ...
steps:
- name: step1
image: foobar/an-image-for-build1
args: ["..."]
- name: step2
image: foobar/an-image-for-build2
args: ["..."]
• Kaniko
• Jib
• Bazel
• Buildpack
• and so on
https://github.com/knative/build-templates
So complex !?
!52
Overview of riff
!53
riff provides developers with a service for executing
functions in response to events.
Features:
! Kubernetes-native -> now built on Knative
! polyglot
○ available: Java, node.js, shell commands
○ coming soon: python, ruby, go
! event streaming
https://projectriff.io/
riff on Knative
!54
Community-driven
ecosystem of
Sources,
Channels,
Functions,
Invokers, 

BuildTemplates,
etc.
Kubernetes
BuildTemplates
Channels
CLI
Invokers
CLI
Invokers
Build
Topics
riff on Knative
!55
Community-driven
ecosystem of
Sources,
Channels,
Functions,
Invokers, 

BuildTemplates,
etc.
Kubernetes
BuildTemplates
Channels
CLI
Invokers
CLI
Invokers
Build
Topics
As of 2018
Function packaging with riff
!56
functions are packaged as
containers
! Developers are responsible
only for the business logic.
! Dependencies in base
image layers can be
managed independently.
○ simpler
○ more secure
○ more efficient
!57
#!/bin/sh
xargs echo -n hello
hello.sh
module.exports = (x) => x ** 2
square.js
Developers write functions
!58
package functions;
import java.util.function.Function;
public class Hello
implements Function<String, String> {
public String apply(String s) {
return "Hello " + s + "!";
}
}
Developers write functions
Source to URL
!59
$ riff function create java hello 
--git-repo https://github.com/making/hello-function.git 
--image making/hello-function 
--verbose
!60
$ riff service invoke hello 
--text -- -d Riff -w 'n'
Hello Riff!
DEMO
https://github.com/making/hello-function
Deploy a Plain Old Function
!62
$ riff function create java fizzbuzz 
--git-repo https://github.com/making/fizz-buzz.git 
--git-revision develop 
--image making/fizzbuzz 
--verbose
!63
$ riff service invoke fizzbuzz 
--text -- -d 100 -w 'n'
Buzz!
Deploy a Knative Service
!64
$ riff service create hello 
--image making/hello-function
!65
$ riff service invoke hello 
--text -- -d Riff -w 'n'
Hello Riff!
Cloud Abstraction
!66
Pivotal Function Service (PFS)
!67
https://pivotal.io/platform/pivotal-function-service Coming Soon!
Riff / Knative hands-on workshop will be open soon!
!68
https://pivotal-japan.connpass.com/
Wrap up: Serverless??
!69
• Dynamic resource utilization, "scale to N"
and "scale to zero"
• Easy integration with platform services
• Billing per message
• Focus on business logic
Wrap up: Serverless??
!70
• Dynamic resource utilization, "scale to N"
and "scale to zero"
• Easy integration with platform services
• Billing per message
• Focus on business logic
Knative an riff
Spring Cloud Function
Thanks for your attention!
!71
https://projectriff.io/

Serverless with Spring Cloud Function, Knative and riff #SpringOneTour #s1t