Reactive Spring
Reactive Spring
using
Spring WebFlux/SpringBoot
Dilip
About Me
• Dilip
• Monolith Applications
• Microservices Applications
• No Downtime is expected
Spring MVC
App
Tomcat
Dispatcher
Client Servlet
Controller Service Dao
DB
Thread
pool API Client API
External
Service
DB
1
Embedded Server 3
{API}
• Let’s say you have a use case to support 10000 concurrent users.
• No
Thread and its Limitations
• Thread is an expensive resource
Spring MVC
App
Tomcat
Dispatcher
Client Servlet
Controller Service Dao
DB
Thread
pool
Embedded Server 3
{API}
• Callbacks
• Futures
Callbacks
Callbacks
• Asynchronous methods that accept a callback as a parameter and invokes it
when the blocking call completes.
• Writing code with Callbacks are hard to compose and di cult to read and
maintain
• Callbackhell
ffi
Future
Concurrency APIs in Java
Future CompletableFuture
• Released in Java 5
• Released in Java8
fi
Drawbacks of Spring MVC
• Concurrency is limited in Spring MVC
• 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 ?
App 2 {API}
Embedded Server 3
{API}
• Spring WebFlux uses the Netty and Project Reactor for building non blocking or
reactive APIs
Reactive Streams
Reactive Streams are the foundation
for Reactive programming.
Reactive Streams
• Reactive Streams Speci cation is created by engineers from multiple
organizations:
• Lightbend
• Net ix
• VmWare (Pivotal)
fl
fi
Reactive Streams Specification
• Reactive Streams Speci cation:
• Publisher
• Subscriber
• Subscription
• Processor
fi
Publisher
public interface Publisher<T>
• Database
• RemoteService etc.,
}
Subscriber
public interface Subscriber<T>
requestForData( )
onNext(1)
DB
App onNext(2)
.
.
onNext(n)
onComplete( )
Subscription
public interface Subscription
requestForData( )
request(2 )
DB
App onNext(1)
onNext(2)
cancel( )
Success Scenario
subscribe( this ) 1
DB
onSubscribe( ) 2
Subscription
request( n ) 3
onNext( 1 ) 4
Subscriber Publisher
onNext( 2 ) 4.1
.
4.n
onNext( 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}
Flow API
• Release as part of Java 9
Dao
APIClient {API}
Options for
Reactive RestFul API
Using
Spring WebFlux
Section Overview
Section Overview
•
Spring WebFlux
Spring WebFlux
Annotated Functional
Controllers Endpoints
What are we going to build in this
course ?
Movies Application using MicroServices Pattern
Annotated Controller
MoviesInfo
Service
Annotated Controller
MoviesReview
Service
Streaming Endpoint
Using
Spring Webflux
Streaming Endpoint
• Streaming Endpoint is a kind of Endpoint which continuously sends updates
to the clients as the new data arrives
Automated Tests
• Automated Tests plays a vital role in delivering quality Software
• Integration Tests
• Unit Tests
Integration Tests
• Integration test is a kind of test which actually test the application end
to end
1 2 3
Integration
Controller Service Repository DB
Test
Unit Tests
• Unit test is a kind of test which tests only the class and method of interest
and mocks the next layer of the code
MoviesInfo
Service
Annotated Controller
MoviesReview
Service
Integration In-Memory
Controller Service Repository DB
Mongo
Test 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 interest
and mocks the next layer of the code
Mockito
Benefits of Unit Tests
• Unit Tests are faster compared to Integration Tests
Spring WebFlux
App 2
1
Dao
APIClient {API}
How does
Netty
handle the request?
Channel
Client
Netty
Req1
Req2
• This EventLoop is responsible for handling the di erent events that occurs in
ff
Channel Lifecycle
1 ChannelUnregistered Channel is Created and its not registered with the Eventloop
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
Functional Web
• This is an alternative programming model for building RESTFUL APIs in
Spring WebFlux
• Lambdas
• Method References
• Functional Interfaces
Functional Web
Router Handler
fi
• All the RestFul APIs endpoints are con gured in one single le
• Challenges:
MoviesInfo
Service
Annotated Controller
MovieReview
Service
MoviesInfo
Service
Annotated Controller
MovieReview
WebClient Service
Webclient
WebClient
• It is a reactive non-blocking Rest Client
MoviesInfo
Service
MoviesService
MovieReview
Service
Http Failures
• Http Failure falls in to two categories:
MoviesInfo
Service
MoviesService
MovieReview
Service
Mock
MoviesInfo
MoviesInfo
Service
Service
MoviesService
Mock
MoviesReview
MovieReview
Service
Service
Benefits of WireMock
• Easy to test the success scenarios (2xx)
• Serialization/Deserialization
• 4xx
• 5xx
MoviesInfo
Service
MoviesService
MovieReview
Service
• Slow Network
Server
Client
(API)