jfokus-reactive the reactive landscapethe reactive landscape reactive a software showing responses...

48
The Reactive Landscape http://bit.ly/jfokus-reactive Clement Escoffier, Vert.x Core Developer, Red Hat

Upload: others

Post on 22-May-2020

42 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

The Reactive Landscapehttp://bit.ly/jfokus-reactive

Clement Escoffier, Vert.x Core Developer, Red Hat

Page 2: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop2

Reactive all the things ???

System

eXtensionsProgramming

Manifesto

Spring

Streams

Asynchrony

Asynchronous

Scalability

ResilienceElasticityBack-Pressure

Spreadsheets

ActorData Flows

ObservableEvents

Message

ReactorRX Java

Page 3: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop3

https://en.oxforddictionaries.com/definition/reactive

Page 4: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop4

FAKE !

Page 5: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop5

End of the flow of control ?

FAKE !

Page 6: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop6

End of the flow of control ? orjust the

comeback of asynchrony ?

FAKE !

Page 7: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop7

The reactive landscape

ReactiveA software showing responses to stimuli

ReactiveSystems

ReactiveStreams

Akka, Vert.xAkka Streams, RX v2,

Reactor, Vert.x

ReactiveProgramming

Reactor, Reactive Spring, RX, Vert.x

Streams, Flows, Events, SpreadsheetsAsynchronous

Manifesto, ActorMessages, ResilienceElasticity, Scalability,

Asynchronous

Data FlowBack-PressureNon-Blocking,Asynchronous

Page 8: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop8

The reactive landscape

ReactiveA software showing responses to stimuli

ReactiveSystems

ReactiveStreams

Akka, Vert.xAkka Streams, RX v2,

Reactor, Vert.x

ReactiveProgramming

Reactor, Reactive Spring, RX, Vert.x

Streams, Flows, Events, SpreadsheetsAsynchronous

Manifesto, ActorMessages, ResilienceElasticity, Scalability,

Asynchronous

Data FlowBack-PressureNon-Blocking,Asynchronous

Page 9: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop9

Mind the step… Why my simple application is such a mess...

My Application

SomeSoftware

SomeServices

Page 10: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop10

Modern software is distributed...Remote interactions everywhere

My Application

SomeSoftware

SomeServices

Page 11: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop11

Distributed systems failHow to stay responsive in face of failures, under varying workload

My Application

SomeSoftware

SomeServices

Page 12: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop12

Distributed systems failHow to stay responsive in face of failures, under varying workload

My Application

SomeSoftware

SomeServices

Page 13: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

Reactive Systems => Responsive Systems

Page 14: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop14

Reactive Manifestohttp://www.reactivemanifesto.org/

Page 15: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop15

Asynchronous message passing

Sends to an address

Subscribes to the address

Messagebackbone

Page 16: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop16

Asynchronous message passing

Not blocked while waiting

for a reply

Messagebackbone

Elasticity

Resilience

Page 17: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop17

Reactive Systems from the trenches

My Application

SomeSoftware

SomeServices

Page 18: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

Asynchronous development model

Page 19: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop19

Asynchronous execution

Ideal world

Task A Task B Task C

Real world

Blocking I/O

Asynchronousexecution

Async programming modelNon-blocking IO

Task-based concurrency

Page 20: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop20

Asynchronous, welcome to Hollywood

public int compute(int a, int b) { return ...;}

public void compute(int a, int b, Handler<AsyncResult<Integer>> h){ // ...

handler.handle(i);}

int res = compute(1, 2);

compute(1, 2, res -> { // Called with the // async result });

Synchronous

Asynchronous

Don’t wait, we will call you...

Page 21: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop21

Callbacks for notifications and async actionsWeb server example

vertx.createHttpServer() .requestHandler(req -> // Async reaction req.response().end("Reactive greetings") ) .listen(8080, ar -> { // Async operation //... });

Page 22: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop22

Callbacks lead to...Reality check

client.getConnection(conn -> { if (conn.failed()) {/* failure handling */} else { SQLConnection connection = conn.result(); connection.query("SELECT * from PRODUCTS", rs -> { if (rs.failed()) {/* failure handling */} else { List<JsonArray> lines = rs.result().getResults(); for (JsonArray l : lines) { System.out.println(new Product(l)); } connection.close( done -> { if (done.failed()) {/* failure handling */} }); } }); }});

Page 23: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

Reactive Programming...Tame the asynchronous

Page 24: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop24

Are you an “Excel” user?

My Expense Report

Lunch 15€

Coffee 25€

Drinks 45€

Total 85€

Page 25: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop25

Are you an “Excel” user?

My Expense Report

Lunch 15€

Coffee 25€

Drinks 45€

Total =sum(B2:B4)

Observe

Page 26: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop26

Streams (a.k.a Observables, Flowables, Publishers)

My Expense Report

Lunch 15€

Coffee 0€

Drinks 0€

Total 15€

My Expense Report

Lunch 15€

Coffee 25€

Drinks 0€

Total 40€

My Expense Report

Lunch 15€

Coffee 25€

Drinks 45€

Total 85€

time

Page 27: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop27

Reactive ProgrammingPublisher and Subscriber

1 2 3 4

2 3 4 5

5 9

Page 28: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop28

Reactive Extension - RX Java 2

Flowable<Integer> obs1 = Flowable.range(1, 10);

Flowable<Integer> obs2 = obs1.map(i -> i + 1);

Flowable<Integer> obs3 = obs2.window(2) .flatMap(MathFlowable::sumInt);

obs3.subscribe( i -> System.out.println("Computed " + i));

Page 29: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop29

Reactive types & Asynchronous

Flowable / Observable - Stream of data, Async reactionBounded or unbounded stream of valuesData, Error, End of Stream

Singles / Maybe - Async operationStream of one valueData, Error, Completion (maybe)

Completables - Async operation (no return)Stream without a valueCompletion, Error

dd d X?

d|X

X|e

d|eSingle

Maybe

Page 30: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop30

Reactive types & Asynchronous

client.rxGetConnection() // Single(async op) .flatMapPublisher(conn -> conn .rxQueryStream("SELECT * from PRODUCTS") .flatMapPublisher(SQLRowStream::toFlowable) .doAfterTerminate(conn::close) ) // Flowable of Rows .map(Product::new) // Flowable of Products .subscribe(System. out::println);

Q

R R

P P

Cflatmap

flatmap

map

Page 31: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

Back pressure & Reactive streams

Page 32: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop32

What if … the processing can’t keep up

I

J

map

Slow processing

J J J

Streams.getStream() .observeOn(Schedulers.computation()) .map(i -> { Thread.sleep(100); // I'm slow... return i.getLong("id"); }) .blockingSubscribe( x -> System.out.println("Processed:" + x), Throwable::printStackTrace, () -> System.out.println("Completed !") );

I

Stream

Page 33: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop33

Buffer to handle small bumps

I

J

Slow processing

J J J

Streams.getStream() .onBackPressureBuffer(10000) .observeOn(Schedulers.computation()) .map(i -> { Thread.sleep(100); // I'm slow... return i.getLong("id"); }) .blockingSubscribe( x -> System.out.println("Processed:" + x), Throwable::printStackTrace, () -> System.out.println("Completed !") );

I

Stream

buffer

Page 34: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop34

Drop when data loss is acceptable

I

J

Slow processing

J J J

Streams.getStream() .onBackPressureDrop() .observeOn(Schedulers.computation()) .map(i -> { Thread.sleep(100); // I'm slow... return i.getLong("id"); }) .blockingSubscribe( x -> System.out.println("Processed:" + x), Throwable::printStackTrace, () -> System.out.println("Completed !") );

I

Stream

/dev/null

Page 35: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop35

Control Flow and Reactive Streams

I

J J J J

Streams.getStreamWithBackPressure() .observeOn(Schedulers.computation()) .map(i -> { Thread.sleep(100); // I'm slow... return i.getLong("id"); }) .blockingSubscribe( x -> System.out.println("Processed:" + x), Throwable::printStackTrace, () -> System.out.println("Completed !") );

I

request(x)Publisher

Subscriber

Reactive Stream API - http://www.reactive-streams.orgJava 9 Flow - http://download.java.net/java/jdk9/docs/api/java/util/concurrent/Flow.html

Page 36: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop36

Back to our database access

request(x)

client.rxGetConnection() // Single(async op) .flatMapPublisher(conn -> conn .rxQueryStream("SELECT * from PRODUCTS") .flatMapPublisher(SQLRowStream:: toFlowable) .doAfterTerminate(conn::close) ) // Flowable of Rows .map(Product::new) // Flowable of Products.subscribe(p -> {

// do something slow, but it’s fine });

Page 37: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

Eclipse Vert.xUnleash your reactive superpowers

VERT.X

Page 38: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop38

Eclipse Vert.x

Microservice, Web applications, IOT, API Gateway, High-volume event processing, Full-blown backend message bus.

Vert.x is a toolkit to build distributed and reactive systems

Designed with reactive in mind

Asynchronous non-blocking

development modelsSimplified

concurrency (event loop)

Polyglot(java, scala, kotlin,

groovy, ruby, ceylon…)(Clustered) event

bus

Page 39: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop39

Vert.x - the all-in-one toolkitReactive

A software showing responses to stimuli

ReactiveSystems

ReactiveProgrammingReactive

Streams

Page 40: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop40

Taming the asynchronousDistributed systems done right

My Application

SomeSoftware

SomeServices

Page 41: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop41

Reactive Web Applicationprivate void add(RoutingContext rc) { String name = rc.getBodyAsString(); database.insert(name) // Single (async) .subscribe( () -> rc.response().setStatusCode(201).end(), rc::fail );}

private void list(RoutingContext rc) { HttpServerResponse response = rc.response().setChunked(true); database.retrieve() // Flowable<Product> (async) .subscribe( p -> response.write(Json.encode(p) +" \n\n"), rc::fail, response::end);}

My Application

Page 42: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop42

Orchestrating remote interactionsSequential composition

WebClient pricer = ...HttpServerResponse response = rc.response().setChunked(true);database.retrieve() // For each row call... .flatMapSingle(p -> webClient .get("/prices/" + p.getName()) .rxSend() .map(HttpResponse::bodyAsJsonObject) .map(json -> p.setPrice(json.getDouble("price"))) ) .subscribe( p -> response.write(Json.encode(p) + " \n\n"), rc::fail, response::end);

My Application

URL

Page 43: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop43

Orchestrating remote interactionsEvent Bus

WebClient pricer = ...HttpServerResponse response = rc.response().setChunked(true);database.retrieve() // For each row call... .flatMapSingle(p -> vertx.eventBus() .<JsonObject>rxSend("pricer", p.getName()) .map(Message::body) .map(json -> p.setPrice(json.getDouble("price"))) ) .subscribe( p -> response.write(Json.encode(p) + " \n\n"), rc::fail, response::end);

My Application

Address

Page 44: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop44

Push data using event bus bridgesWeb Socket, SSE...

String name = rc.getBodyAsString().trim();database.insert(name) .flatMap(...) .subscribe( p -> { String json = Json.encode(p); rc.response().setStatusCode(201).end(json); vertx.eventBus().publish("products", json); }, rc::fail);

My Application

SockJS

Page 45: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop45

Executing several operations concurrently

database.insert(name) .flatMap(p -> { Single<Product> price = getPriceForProduct(p); Single<Integer> audit = sendActionToAudit(p); return Single.zip(price, audit, (pr, a) -> pr); }) .subscribe( p -> { String json = Json.encode(p); rc.response().setStatusCode(201).end(json); vertx.eventBus().publish("products", json); }, rc::fail);

My Application

Page 46: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

Reactive => Build better systems

Page 47: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

#vertx #reactive #rhoar #jfokus @clementplop47

Welcome to an asynchronous worldReactive

A software showing responses to stimuli

ReactiveSystems

ReactiveProgrammingReactive

Streams

Responsive distributed systems

Back-Pressureprotocol

Asynchronousdevelopment

model

Page 48: jfokus-reactive The Reactive LandscapeThe reactive landscape Reactive A software showing responses to stimuli Reactive Systems Reactive Streams Akka, Vert.x Akka Streams, RX v2, Reactor,

Building Reactive Microservices in JavaUse Eclipse Vert.x to build reactive microservices:

● Explore the elements of reactive microservices and learn how Vert.x

works.

● Build and consume a single microservice to understand how messaging

improves its reactiveness.

● Create an entire microservices system, using stability and resilience

patterns to manage failures.

Download the book from: http://bit.ly/vertx-reactive

[email protected]