36701070-reactive-spring
36701070-reactive-spring
using
Spring WebFlux/SpringBoot
Dilip
About Me
• Dilip
Programming
Blocking) APIs
Source Code
Thank You!
Why Reactive Programming ?
Evolution of Programming
Past (10 -15 ) years ago Current
• No Downtime is expected
Spring MVC
App
Tomcat
Dispatcher
Client Servlet Controller Service Dao
DB
Thread
pool API Client API
Ex
S te
er r n
vi a
ce l
• Concurrency is Thread Per Request model
• This style of building APIs are called Blocking APIs
• Wont scale for today’s application needs
Restful API using Spring Boot/MVC
DB
1
Spring MVC 2
{API
App }
Embedded Server 3
{API
}
• Let’s say you have a use case to support 10000 concurrent users.
• No
Thread and its Limitations
Spring MVC
App
Tomcat
Dispatcher
Client Servlet Controller Service Dao
DB
Thread
pool
Restful API using Spring Boot/MVC
Spring MVC 2
{API
App }
Embedded Server 3
{API
}
• Futures
Callbacks
Callbacks
• Writing code with Callbacks are hard to compose and difficult to read
and maintain
• Callbackhell
Future
Concurrency APIs in Java
Future CompletableFuture
• No
requestForData( )
request(n)
onNext(1) DB
App onNext(2)
..
.
onNext(n)
onComplete( )
requestForData( )
request(n)
onNext(1)
DB
App onNext(2)
..
.
onNext(n)
onComplete( )
requestForData( )
request(2 )
DB
App onNext(1)
onNext(2)
cancel( )
Backpressure
Push-based data flow model
Push-Pull based data flow
model
When to use Reactive
Programming ?
{API
App 2
}
Embedded Server 3
{API
}
• Lightbend
• Netflix
• VmWare (Pivotal)
Reactive Streams Specification
• Publisher
• Subscriber
• Subscription
• Processor
Publisher
public interface Publisher<T> {
1
public void subscribe(Subscriber<? super T> s);
}
requestForData( )
onNext(1)
DB
App onNext(2)
..
.
onNext(n)
onComplete( )
Subscription
public interface Subscription {
requestForData( )
request(2 )
DB
App onNext(1)
onNext(2)
cancel( )
subscribe( this ) 1
DB
onSubscribe( ) 2
Subscription
request( n ) 3
onNext( 1 ) 4
Subscriber Publisher
4.
onNext( 2 ) 1
..
. 4.
onNext( n ) n
{API}
onComplete( ) 5
Reactive Streams - How it works
together ?
Error/Exception Scenario
subscribe( this ) 1
DB
onSubscribe( ) 2
Subscription
request( n ) 3
Subscriber Publisher
onError( ) 4
{API}
JRE
What is a
Nonblocking or Reactive
RestFul API ?
NonBlocking or Reactive RestFul API
• A Non-Blocking or Reactive RestFul API has the behavior of providing
end to end non-blocking communication between the client and
service
Dao
{API
APIClient
}
Options for
Reactive RestFul API
Using
Spring WebFlux
Setting up the Project
For this
Course
Section Overview
Section Overview
Spring WebFlux
Annotated Functional
Controllers Endpoints
What are we going to build in
this course ?
Movies Application using MicroServices
Pattern
Annotated Controller
Name, cast,
MoviesInfo
year
Service
Annotated Controller
MoviesServic
e Functional Endpoints
MoviesRevie
Rating, Review
w
Service
Movies Application using MicroServices
Pattern
Annotated Controller
MoviesInfo
Service
Annotated Controller
MoviesServic
e Functional Endpoints
MoviesRevie
w
Service
Streaming Endpoint
Using
Spring Webflux
Streaming Endpoint
• Integration Tests
• Unit Tests
Integration Tests
• Unit test is a kind of test which tests only the class and method of
Controlle Repositor
Unit Test Service Mock DB
r y
Movies Application using MicroServices
Pattern
Annotated Controller
MoviesInfo
Service
Annotated Controller
MoviesServic
e Functional Endpoints
MoviesRevie
w
Service
Integration Tests using Embedded
MongoDB
In-
Memory
Integrati Controlle Repositor
Service DB
Mongo
onTest r y DB
testImplementation 'de.flapdoodle.embed:de.flapdoodle.embed.mongo'
Unit Tests
• Unit test is a kind of test which tests only the class and method of
Controlle
Unit Test Mock
r
Mockito
Benefits of Unit Tests
Spring WebFlux
App 2
1
Dao
{API
APIClient
}
How does
Netty
handle the request?
Netty (WebFlux’s Default Server)
Channel
Client
Netty
Req1
Req2
ChannelUnregister
1 Channel is Created and its not registered with the Eventloop
ed
3 ChannelActive Channel is active and its now possible to send and receive the data
4 ChannelInActive Channel is not connected to the client anymore and ready to be closed
Client Netty
Req1 Thread1
Response
Router Handler
Is there an advantage in building
RestFul APIs using Functional
Web?
Functional Web
• Benefits:
• All the RestFul APIs endpoints are configured in one single file
• Challenges:
MoviesInfo
Service
Annotated Controller
MoviesServic
e Functional Endpoints
MovieReview
Service
Movies Application using MicroServices
Pattern
Annotated Controller
MoviesInfo
Service
Annotated Controller
MoviesServic
e Functional Endpoints
MovieReview
WebClient Service
Webclient
WebClient
blocking fashion
MoviesInfo
Service
MoviesServic
e
MovieReview
Service
Http Failures
MoviesInfo
Service
MoviesServic
e
MovieReview
Service
Movies Application using MicroServices
Pattern
Mock
MoviesInfo
MoviesInfo
Service
Service
MoviesServic
e
MoviesRevie
Mock
w
MovieReview
Service
MoviesInfo
Service
MoviesServic
e
MovieReview
Service
Server
Client
(API)