Vert.X: Microservices Were Never So Easy (Clement Escoffier)

Post on 07-Jan-2017

552 views 1 download

transcript

VERT.XMICROSERV ICES WERE NEVER SOEASY

CLEMENTESCOFFIER

Vert.xCoreDeveloper,RedHat

VERT.XI N 1 0 SL IDES . . .

VERT.X I S A TOOLK IT TO BU I LDDISTR IBUTED AND REACT IVEAPPL ICAT IONS ON TOP OF THE JVMUS ING AN ASYNCHRONOUS NON-BLOCK ING DEVELOPMENT MODEL .

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

TOOLKITTheseslidesareservedbyavert.xapplication:

executedonOpenshiftpackagedasafatjar

vertx-core:themainvert.xcomponentvertx-web:acomponenttobuildmodernwebapplicationsvertx-hazelcast:animplementationofthevert.xclustermanagervertx-service-discovery-bridge-kubernetes:anextensiontointeractwithKubernetes

DISTRIBUTED

DISTRIBUTED“ Youknowyouhaveadistributedsystemwhenthecrashofacomputeryou'veneverheardsofstopsyoufromgettinganyworkdone.(LeslieLamport)

REACTIVEReactivesystemsare

Responsive-theyrespondinanacceptabletimeElastic-theyscaleupanddownResilient-theyaredesignedtohandlefailuresgracefullyAsynchronous-theyinteractusingasyncmessages

http://www.reactivemanifesto.org/

REACTIVE“ "Moore'slaw"istheobservationthat,overthehistoryofcomputinghardware,thenumberoftransistorsinadenseintegratedcircuithasdoubledapproximatelyevery2years.

Unfortunately,nofreelunchanymore...

REACTIVECPUmanufacturerscannotgetasingle-coreCPUtogoanyfaster

Multi-coreProcessorsRequireparallelexecutionRequireadifferentwaytodesign,developandexecutesoftwareReactivesystemsisonesolutiontothisissue

POLYGLOTVert.xapplicationscanbedevelopedusing

JavaGroovyRuby(JRuby)JavaScript(Nashorn)Ceylon

MICROSERVICESREBRAND INGD ISTR IBUTEDAPPL ICAT IONS

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

ASUITEOFINDEPENDENTSERVICES:Eachservicerunsinitsownprocess

Sotheyaredistributedapplications

Lightweightinteractions-Loose-coupling

NotonlyHTTPMessagingStreams(async)RPC

ASUITEOFINDEPENDENTSERVICES:Independentlydeveloped,testedanddeployed

Automatedprocess(Liskov)substitutability

It'sallaboutagility

AgilityofthecompositionYoucanreplaceanymicroserviceYoucandecidewhouseswho

SORRY...NOFREELUNCHDistributedapplicationsarehard

fail(fail-stop,byzantinefault)DiscoverabilityissueReliabilityissueAvailabilityissue

Howtokeepthingsontrack

MonitoringHealthTracing...

COMPLEXITYGROWTH

VERT.X&MICROSERVICESWewont'buildregularmicroservices,butreactivemicroservices

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

ASYNCHRONOUS&NON-BLOCKING

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

HTTP&RESTBECAUSE I T ALWAYSBEG IN W ITH A REST

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(); } });

VERT.XHELLOWORLDInvoke

EVENT LOOPS

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 });});

SERVICEDISCOVERYLocatetheservices,environment-agnostic

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()); }); }); });

CHAINEDHTTPREQUESTS

Invoke

INTERACTINGWITHBLOCKINGSYSTEMS

MESSAGINGWITHTHEEVENTBUSTHE SP INE OF VERT . X

APPL ICAT IONS

THEEVENTBUSTheeventbusisthenervoussystemofvert.x:

Allowsdifferentcomponentstocommunicateregardlesstheimplementationlanguageandtheirlocationwhethertheyrunonvert.xornot(usingbridges)

Address:MessagesaresenttoanaddressHandler:Messagesarereceivedinhandlers.

POINTTOPOINT

PUBLISH/SUBSCRIBE

REQUEST/RESPONSE

DISTRIBUTEDEVENTBUSAlmostanythingcansendandreceivemessages

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

hellofromjava(8321)

hellofromjava(8320)

hellofromjava(8319)

hellofromjava(8318)

hellofromjava(8317)

hellofromjava(8316)

hellofromjava(8315)

hellofromjava(8314)

hellofromjava(8313)

DISTRIBUTEDEVENTBUS

EVENTBUSCLIENTSANDBRIDGESClients:

SockJS:browser,node.jsTCP:everysystemabletoopenaTCPsocketGo:forsystemdevelopinGo

Bridges:

StompAMQPCamel

RELIABILITYPATTERNS

DON 'T BE FOOL , BEPREPARED TO FA IL

RELIABILITYIt'snotaboutbeingbug-freeorbulletproof,it'simpossible.

It'saboutbeingpreparedtofail,andhandlingthesefailures.

MANAGINGFAILURESDistributedcommunicationmayfail

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()); }});

MANAGINGFAILURES

Invoke

MANAGINGFAILUREclient.get("/", response -> { response .exceptionHandler(t -> { // If the content cannot be read rc.response().end("Sorry... " + t.getMessage()); }) .bodyHandler(buffer -> { rc.response().end("Ola " + buffer.toString()); }); }) .setTimeout(3000) .exceptionHandler(t -> { // If the connection cannot be established rc.response().end("Sorry... " + t.getMessage()); }) .end();

MANAGINGFAILURE

Invoke

CIRCUITBREAKER

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 -> /* ... */);

CIRCUITBREAKER

Invoke

SCALABILITYPATTERNS

BE PREPARED TO BEFAMOUS

BALANCINGTHELOADWhenseveralconsumerslistentothesameaddress,Vert.xdispatchesthesentmessagesusingaroundrobin.

So,toimprovethescalability,justspawnanewnode!

BALANCINGTHELOAD

BALANCINGTHELOADInvoke

SCALINGHTTP

THISISNOTTHEEND()

BUTTHEFIRSTSTEPONTHEREACTIVEMICROSERVICEROAD

HOWTOSTART?http://vertx.iohttp://vertx.io/blog/posts/introduction-to-vertx.htmlhttp://vertx-lab.dynamis-technologies.comhttps://github.com/vert-x3/vertx-examples

RedHatNews

youtube.com/RedHat

facebook.com/redhatinc

THANKYOU!

linkedin.com/company/red-hat