Reactive Polyglot Microservices with OpenShift and Vert.x

53
REACTIVE POLYGLOT MICROSERVICES WITH OPENSHIFT AND VERT.X CLEMENT ESCOFFIER Vert.x Core Developer, Red Hat

Transcript of Reactive Polyglot Microservices with OpenShift and Vert.x

Page 1: Reactive Polyglot Microservices with OpenShift and Vert.x

REACTIVEPOLYGLOTMICROSERVICESWITHOPENSHIFTANDVERT.X

CLEMENTESCOFFIER

Vert.xCoreDeveloper,RedHat

Page 2: Reactive Polyglot Microservices with OpenShift and Vert.x

VERT.XI N 1 0 SL IDES . . .

Page 3: Reactive Polyglot Microservices with OpenShift and Vert.x

VERT.X I S A TOOLK IT TO BU I LDDISTR IBUTED AND REACT IVESYSTEMS ON TOP OF THE JVM US INGAN ASYNCHRONOUS NON-BLOCK INGDEVELOPMENT MODEL .

Page 4: Reactive Polyglot Microservices with OpenShift and Vert.x

TOOLKITVert.xisaplainboringjarYourapplicationdependsonthissetofjars(classpath,fat-jar,...)

Page 5: Reactive Polyglot Microservices with OpenShift and Vert.x

DISTRIBUTED“ Youknowyouhaveadistributedsystemwhenthecrashofacomputeryou'veneverheardsofstopsyoufromgettinganyworkdone.(LeslieLamport)

Page 6: Reactive Polyglot Microservices with OpenShift and Vert.x

REACTIVEReactivesystemsare

Responsive-theyrespondinanacceptabletimeElastic-theyscaleupanddownResilient-theyaredesignedtohandlefailuresgracefullyAsynchronous-theyinteractusingasyncmessages

http://www.reactivemanifesto.org/

Page 7: Reactive Polyglot Microservices with OpenShift and Vert.x

POLYGLOTvertx.createHttpServer() .requestHandler(req -> req.response().end("Hello Java")) .listen(8080);

vertx.createHttpServer() .requestHandler({ req -> req.response().end("Hello Groovy") }) .listen(8080)

vertx.createHttpServer() .requestHandler(function (req) { req.response().end("Hello JavaScript") }) .listen(8080);

vertx.createHttpServer() .requestHandler(req => req.response().end("Hello Scala")) .listen(8080)

Page 8: Reactive Polyglot Microservices with OpenShift and Vert.x

MICROSERVICESREBRAND INGD ISTR IBUTEDAPPL ICAT IONS

Page 9: Reactive Polyglot Microservices with OpenShift and Vert.x

MICROSERVICES“ Themicroservicearchitecturalstyleisanapproachtodevelopingasingleapplicationasasuiteofsmallservices,eachrunninginitsownprocessandcommunicatingwithlightweightmechanisms,oftenanHTTPresourceAPI.Theseservicesarebuiltaroundbusinesscapabilitiesandindependentlydeployablebyfullyautomateddeploymentmachinery.Thereisabareminimumofcentralizedmanagementoftheseservices,whichmaybewrittenindifferentprogramminglanguagesandusedifferentdatastoragetechnologies.(MartinFowler)

Page 10: Reactive Polyglot Microservices with OpenShift and Vert.x

ASUITEOFINDEPENDENTSERVICES:Eachservicerunsinitsownprocess

Sotheyaredistributedapplications

Lightweightinteractions-Loose-coupling

NotonlyHTTPMessagingStreams(async)RPC

Page 11: Reactive Polyglot Microservices with OpenShift and Vert.x

ASUITEOFINDEPENDENTSERVICES:Independentlydeveloped,testedanddeployed

Automatedprocess(Liskov)substitutabilityIt'sallaboutagility

Page 12: Reactive Polyglot Microservices with OpenShift and Vert.x

NOFREELUNCH

Page 13: Reactive Polyglot Microservices with OpenShift and Vert.x

VERT.X&MICROSERVICESWewont'buildregularmicroservices,butreactivemicroservices

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

Page 14: Reactive Polyglot Microservices with OpenShift and Vert.x

PICKYOUROWNDEVELOPMENTMODELvertx.createHttpServer() .requestHandler(request -> { doA("Austin", ar -> { doB("Reactive Summit", ar2 -> { request.response().end(ar.result() + "\n" + ar2.result()); }); }); }).listen(8080);

Page 15: Reactive Polyglot Microservices with OpenShift and Vert.x

PICKYOUROWNDEVELOPMENTMODELvertx.createHttpServer() .requestHandler(request -> { Future<String> a = doA("Austin"); Future<String> b = doB("Reactive Summit"); CompositeFuture.all(a, b).setHandler(ar -> { request.response().end(a.result() + "\n" + b.result()); }); }).listen(8080);

Page 16: Reactive Polyglot Microservices with OpenShift and Vert.x

PICKYOUROWNDEVELOPMENTMODELvertx.createHttpServer() .requestHandler(request -> { CompletableFuture<String> a = doA("Austin"); CompletableFuture<String> b = doB("Reactive Summit"); CompletableFuture.allOf(a, b).thenRun(() -> { request.response().end(a.join() + "\n" + b.join()); }); }).listen(8080);

Page 17: Reactive Polyglot Microservices with OpenShift and Vert.x

PICKYOUROWNDEVELOPMENTMODELHttpServer server = vertx.createHttpServer();server.requestStream().toObservable() .subscribe(req -> { Observable<String> a = doA("Austin"); Observable<String> b = doB("Reactive Summit"); Observable.zip(a, b, (s1, s2) -> s1 + "\n" + s2) .subscribe(s -> req.response().end(s)); });server.listen(8080);

Page 18: Reactive Polyglot Microservices with OpenShift and Vert.x

WHATDOESVERT.XPROVIDETOBUILDMICROSERVICES?TCP,UDP,HTTP1&2serversandclients(non-blocking)DNSclientEventbus(messaging)DistributeddatastructuresLoad-balancingFail-overServicediscoveryFailuremanagement,Circuit-breakerShell,Metrics,Deploymentsupport

Page 19: Reactive Polyglot Microservices with OpenShift and Vert.x

HTTP&RESTBECAUSE I T ALWAYSBEG IN W ITH A REST

Page 20: Reactive Polyglot Microservices with OpenShift and Vert.x

VERT.XHELLOWORLDvertx.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 21: Reactive Polyglot Microservices with OpenShift and Vert.x

VERT.XHELLOWORLDInvoke

Page 22: Reactive Polyglot Microservices with OpenShift and Vert.x

EVENT LOOPS

Page 23: Reactive Polyglot Microservices with OpenShift and Vert.x

VERT.XASYNCHTTPCLIENTHttpClient client = vertx.createHttpClient( new HttpClientOptions() .setDefaultHost(host) .setDefaultPort(port));

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

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

Page 24: Reactive Polyglot Microservices with OpenShift and Vert.x

SERVICEDISCOVERYLocatetheservices,environment-agnostic

Page 25: Reactive Polyglot Microservices with OpenShift and Vert.x

SERVICEDISCOVERY-KUBERNETESNoneedforpublication-ImportKubernetesservices

HttpEndpoint.getClient(discovery, new JsonObject().put("name", "vertx-http-server"), result -> { if (result.failed()) { rc.response().end("D'oh no matching service"); return; } HttpClient client = result.result(); client.getNow("/", response -> { response.bodyHandler(buffer -> { rc.response().end("Hello " + buffer.toString()); }); }); });

Page 26: Reactive Polyglot Microservices with OpenShift and Vert.x

CHAINEDHTTPREQUESTS

Invoke

Page 27: Reactive Polyglot Microservices with OpenShift and Vert.x

MESSAGINGWITHTHEEVENTBUSTHE SP INE OF VERT . X

APPL ICAT IONS

Page 28: Reactive Polyglot Microservices with OpenShift and Vert.x

THEEVENTBUSTheeventbusisthenervoussystemofvert.x:

Allowsdifferentcomponentstocommunicateregardlesstheimplementationlanguageandtheirlocationwhethertheyrunonvert.xornot(usingbridges)

Address:MessagesaresenttoanaddressHandler:Messagesarereceivedinhandlers.

Page 29: Reactive Polyglot Microservices with OpenShift and Vert.x

POINTTOPOINT

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

Page 30: Reactive Polyglot Microservices with OpenShift and Vert.x

PUBLISH/SUBSCRIBE

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

Page 31: Reactive Polyglot Microservices with OpenShift and Vert.x

REQUEST/RESPONSE

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

Page 32: Reactive Polyglot Microservices with OpenShift and Vert.x

DISTRIBUTEDEVENTBUSAlmostanythingcansendandreceivemessages

Page 33: Reactive Polyglot Microservices with OpenShift and Vert.x

DISTRIBUTEDEVENTBUSLet'shaveajava(Vert.x)app,andanodeappsendingdatajusthere:

bonjourfromnode(248)

hellofromjava(286)

bonjourfromnode(247)

hellofromjava(285)

bonjourfromnode(246)

hellofromjava(284)

bonjourfromnode(245)

hellofromjava(283)

bonjourfromnode(244)

Page 34: Reactive Polyglot Microservices with OpenShift and Vert.x

DISTRIBUTEDEVENTBUS

Page 35: Reactive Polyglot Microservices with OpenShift and Vert.x

EVENTBUSCLIENTSANDBRIDGESBridges

SockJS:browser,node.jsTCP:languages/systemsabletoopenaTCPsocketStompAMQPCamel

Clients:

Go,C#,C,Python,Swift...

Page 36: Reactive Polyglot Microservices with OpenShift and Vert.x

RELIABILITYPATTERNS

DON 'T BE FOOL , BEPREPARED TO FA IL

Page 37: Reactive Polyglot Microservices with OpenShift and Vert.x

RELIABILITYIt'snotaboutbeingbug-freeorbulletproof,it'simpossible.

It'saboutbeingpreparedtofail,andhandlingthesefailures.

Page 38: Reactive Polyglot Microservices with OpenShift and Vert.x

MANAGINGFAILURESDistributedcommunicationmayfailvertx.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 39: Reactive Polyglot Microservices with OpenShift and Vert.x

MANAGINGFAILURES

Invoke

Page 40: Reactive Polyglot Microservices with OpenShift and Vert.x

CIRCUITBREAKER

Page 41: Reactive Polyglot Microservices with OpenShift and Vert.x

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

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

Page 42: Reactive Polyglot Microservices with OpenShift and Vert.x

CIRCUITBREAKER

Invoke

Page 43: Reactive Polyglot Microservices with OpenShift and Vert.x

SCALABILITYPATTERNS

BE PREPARED TO BEFAMOUS

Page 44: Reactive Polyglot Microservices with OpenShift and Vert.x

ELAST IC I TY PATTERNS

Page 45: Reactive Polyglot Microservices with OpenShift and Vert.x

BALANCINGTHELOADWhenseveralconsumerslistentothesameaddress,Vert.xdispatchesthesentmessagesusingaroundrobin.

So,toimprovethescalability,justspawnanewnode!

Page 46: Reactive Polyglot Microservices with OpenShift and Vert.x

BALANCINGTHELOAD

Page 47: Reactive Polyglot Microservices with OpenShift and Vert.x

BALANCINGTHELOADInvoke

Page 48: Reactive Polyglot Microservices with OpenShift and Vert.x

SCALINGHTTP

Page 49: Reactive Polyglot Microservices with OpenShift and Vert.x

THISISNOTTHEEND()

BUTTHEFIRSTSTEPONTHEREACTIVEMICROSERVICEROAD

Page 50: Reactive Polyglot Microservices with OpenShift and Vert.x
Page 51: Reactive Polyglot Microservices with OpenShift and Vert.x
Page 52: Reactive Polyglot Microservices with OpenShift and Vert.x

HOWTOSTART?https://www.openshift.org

http://vertx.iohttp://vertx.io/blog/posts/introduction-to-vertx.htmlhttp://escoffier.me/vertx-hol

SecureEnclavesForReactiveCloudApplications