Reactive Distributed Applications with Vert.x

52
REACTIVE DISTRIBUTED SYSTEMS WITH VERT.X CLEMENT ESCOFFIER RED HAT

Transcript of Reactive Distributed Applications with Vert.x

Page 1: Reactive Distributed Applications with Vert.x

REACTIVEDISTRIBUTEDSYSTEMSWITHVERT.X

CLEMENTESCOFFIERREDHAT

Page 2: Reactive Distributed Applications with Vert.x

VERT.XISATOOLKITTOBUILDDISTRIBUTEDANDREACTIVESYSTEMS

ONTOPOFTHEJVMUSINGANASYNCHRONOUSNON-BLOCKING

DEVELOPMENTMODEL.

Page 3: Reactive Distributed Applications with Vert.x

TOOLKITVert.xisaplainboringjarVert.xcomponentsareplainboringjarsYourapplicationdependsonthissetofjars(classpath,fat-jar,...)

Page 4: Reactive Distributed Applications with Vert.x

DISTRIBUTED“Youknowyouhaveadistributed

systemwhenthecrashofacomputeryou'veneverheardsofstopsyou

fromgettinganyworkdone.”(LeslieLamport)

Page 5: Reactive Distributed Applications with Vert.x

REACTIVESYSTEMSResponsive-theyrespondinanacceptabletimeElastic-theyscaleupanddownResilient-theyaredesignedtohandlefailuresgracefullyAsynchronous-theyinteractusingasyncmessages

http://www.reactivemanifesto.org/

Page 6: Reactive Distributed Applications with Vert.x

REACTIVESYSTEMS!=REACTIVEPROGRAMMING

Page 7: Reactive Distributed Applications with Vert.x

REACTIVESYSTEMS+REACTIVEPROGRAMMING

Page 8: Reactive Distributed Applications with Vert.x

POLYGLOTVert.xapplicationscanbedevelopedusing

JavaGroovyRuby(JRuby)JavaScript(Nashorn)CeylonScalaKotlin

Page 9: Reactive Distributed Applications with Vert.x

VERT.XAtoolkittobuilddistributedsystems

Page 10: Reactive Distributed Applications with Vert.x

VERT.XBuilddistributedsystems:

DonothidethecomplexityFailureasfirst-classcitizenProvidethebuildingblocks,notanall-in-onesolution

Page 11: Reactive Distributed Applications with Vert.x

WHATDOESVERT.XPROVIDE?TCP,UDP,HTTP1&2serversandclients(non-blocking)DNSclientClusteringEventbus(messaging)Distributeddatastructures(built-in)Load-balancing(built-in)Fail-overPluggableservicediscovery,circuit-breakerMetrics,Shell

Page 12: Reactive Distributed Applications with Vert.x

REACTIVEBuildreactivedistributedsystems:

Responsive-fast,isabletohandlealargenumberofevents/connectionsElastic-scaleupanddownbyjuststartingandstoppingnodes,round-robinResilient-failureasfirst-classcitizen,fail-overAsynchronousmessage-passing-asynchronousandnon-blockingdevelopmentmodel

Page 13: Reactive Distributed Applications with Vert.x

ASYNCHRONOUS&NON-BLOCKING

Page 14: Reactive Distributed Applications with Vert.x

ASYNCHRONOUS&NON-BLOCKING// Synchronous development modelX x = doSomething(a, b);

// Asynchronous development model - callback variantdoSomething(a, b, // Params ar -> { // Last param is a Handler<AsyncResult<X>> // Result handler });

// Asynchronous development model - future variantFuture<X> future = doSomething(a, b);future.setHandler( ar -> { /* Completion handler */ });

Page 15: Reactive Distributed Applications with Vert.x

REQUEST-REPLYINTERACTIONS

HTTP,TCP,RPC...

Page 16: Reactive Distributed Applications with Vert.x

VERT.XHELLOWORLDVertx vertx = Vertx.vertx();vertx.createHttpServer() .requestHandler(request -> { // Handler receiving requests request.response().end("World !"); }) .listen(8080, ar -> { // Handler receiving start sequence completion (AsyncResult) if (ar.succeeded()) { System.out.println("Server started on port " + ar.result().actualPort()); } else { ar.cause().printStackTrace(); } });

Page 17: Reactive Distributed Applications with Vert.x

VERT.XHELLOWORLDInvoke

Page 18: Reactive Distributed Applications with Vert.x

EVENTLOOPS

Page 19: Reactive Distributed Applications with Vert.x

VERT.XASYNCHTTPCLIENTHttpClient client = vertx.createHttpClient( new HttpClientOptions() .setDefaultHost("localhost") .setDefaultPort(8081));

client.getNow("/", response -> { // Handler receiving the response

// Get the content response.bodyHandler(buffer -> { // Handler to read the content });});

Page 20: Reactive Distributed Applications with Vert.x

CHAINEDHTTPREQUESTS

Invoke

Page 21: Reactive Distributed Applications with Vert.x

INTERACTINGWITHBLOCKINGSYSTEMS

Page 22: Reactive Distributed Applications with Vert.x

MESSAGINGTheeventbus-thespineofVert.xapplications...

Page 23: Reactive Distributed Applications with Vert.x

THEEVENTBUSTheeventbusisthenervoussystemofvert.x:

Allowsdifferentcomponentstocommunicateregardless

theimplementationlanguageandtheirlocationwhethertheyrunonvert.xornot(usingbridges)

Address:MessagesaresenttoanaddressHandler:MessagesarereceivedbyHandlers.

Page 24: Reactive Distributed Applications with Vert.x

POINTTOPOINT

vertx.eventBus().send("address", "message");vertx.eventBus().consumer("address", message -> {});

Page 25: Reactive Distributed Applications with Vert.x

PUBLISH/SUBSCRIBE

vertx.eventBus().publish("address", "message");vertx.eventBus().consumer("address", message -> {});

Page 26: Reactive Distributed Applications with Vert.x

REQUEST/RESPONSE

vertx.eventBus().send("address", "message", reply -> {});vertx.eventBus().consumer("address", message -> { message.reply("response"); });

Page 27: Reactive Distributed Applications with Vert.x

FROMLOCALTOCLUSTEREDVert.xinstancesformacluster

Theeventbusisdistributedonalltheclustermembers

Vertx.clusteredVertx(new VertxOptions(), result -> { if (result.failed()) { System.err.println("Cannot create a clustered vert.x : " + result.cause()); } else { Vertx vertx = result.result(); // ... }});

Page 28: Reactive Distributed Applications with Vert.x

DISTRIBUTEDEVENTBUSAlmostanythingcansendandreceivemessages

Page 29: Reactive Distributed Applications with Vert.x

DISTRIBUTEDEVENTBUSLet'shaveajava(Vert.x)app,andanodeappsending

datajusthere:

Page 30: Reactive Distributed Applications with Vert.x

DISTRIBUTEDEVENTBUS

Page 31: Reactive Distributed Applications with Vert.x

EVENTBUSCLIENTSANDBRIDGESBridges

SockJS:browser,node.jsTCP:languages/systemsabletoopenaTCPsocketStompAMQPCamel

Clients:

Go,C#,C,Python...

Page 32: Reactive Distributed Applications with Vert.x

RELIABILITYPATTERNSDon'tbefool,bepreparedtofail

Page 33: Reactive Distributed Applications with Vert.x

RELIABILITY

It'snotaboutbeingbug-freeorbulletproof,weahumans.

It'saboutbeingpreparedtofail,andhandlingthesefailures.

Page 34: Reactive Distributed Applications with Vert.x

MANAGINGFAILURESDistributedcommunicationmayfail

AsyncResultletsusmanagethesefailures:doSomethingAsync(param1, param2, ar -> { if (ar.failed()) { System.out.println("D'oh, it has failed !"); } else { System.out.println("Everything fine ! "); }});

Page 35: Reactive Distributed Applications with Vert.x

MANAGINGFAILURESAddingtimeouts

vertx.eventbus().send(..., ..., new DeliveryOptions().setSendTimeout(1000), reply -> { if (reply.failed()) { System.out.println("D'oh, he did not reply to me !"); } else { System.out.println("Got a mail " + reply.result().body()); }});

Page 36: Reactive Distributed Applications with Vert.x

CIRCUITBREAKER

Page 37: Reactive Distributed Applications with Vert.x

CIRCUITBREAKERcb.executeWithFallback(future -> { // Async operation client.get("/", response -> { response.bodyHandler(buffer -> { future.complete("Ola " + buffer.toString()); }); }) .exceptionHandler(future::fail) .end(); },

// Fallback t -> "Sorry... " + t.getMessage() + " (" + cb.state() + ")") // Handler called when the operation has completed .setHandler(content -> /* ... */);

Page 38: Reactive Distributed Applications with Vert.x

CIRCUITBREAKER

Invoke

Page 39: Reactive Distributed Applications with Vert.x

VERTICLEFAIL-OVERVerticlesarechunkofcodethatgetdeployedandrunbyVert.xVerticlescandeployotherverticlesVerticlescanbewritteninJava,Groovy,JavaScript,Ruby,Ceylon...

Page 40: Reactive Distributed Applications with Vert.x

VERTICLEFAIL-OVERInHigh-Availabilitymode,verticlesdeployedona

nodethatcrashesareredeployedonasanenodeofthecluster.

Page 41: Reactive Distributed Applications with Vert.x

VERTICLEFAIL-OVERInvoke

Page 42: Reactive Distributed Applications with Vert.x

ELASTICITYPATTERNSBepreparedtobefamous

Page 43: Reactive Distributed Applications with Vert.x

ELASTICITYPATTERNS

Page 44: Reactive Distributed Applications with Vert.x

BALANCINGTHELOADWhenseveralconsumerslistentothesameaddress,Vert.xdispatchesthesentmessagesusingaround

robin.

So,toimprovethescalability,justspawnanewnode!

Page 45: Reactive Distributed Applications with Vert.x

BALANCINGTHELOAD

Page 46: Reactive Distributed Applications with Vert.x

BALANCINGTHELOADInvoke

Page 47: Reactive Distributed Applications with Vert.x

SCALINGHTTP

Page 48: Reactive Distributed Applications with Vert.x

WHATABOUTPERFORMANCES?

Becausewedoitwell,andwedoitfast

Page 49: Reactive Distributed Applications with Vert.x

TECHEMPOWER-FORTUNERequest->JDBC(query)->Templateengine->

Response

Page 50: Reactive Distributed Applications with Vert.x

THISISNOTTHEEND();ButthefirststepontheVert.xpath

Page 51: Reactive Distributed Applications with Vert.x
Page 52: Reactive Distributed Applications with Vert.x

HOWTOSTART?http://vertx.iohttp://vertx.io/blog/posts/introduction-to-vertx.htmlhttp://escoffier.me/vertx-hol/(HOL3180)ReactiveMicroserviceswithVert.x(CON5389)

https://jaxlondon.com/jax-awards/