Upgrade to Pro — share decks privately, control downloads, hide ads and more …

vertx.pdf

 vertx.pdf

microservices with vert.x

Transcript

  1. Building Reactive Microservices with Vert.x Claudio Eduardo de Oliveira

  2. About me Claudio Eduardo de Oliveira • Developer @ Daitan

    Group • Bacharel em Ciência da Computação • Cursando MBA em Arquitetura de Soluções em Tecnologia (DeVry/Metrocamp) • Entusiasta Docker / Spring / Vert.x Contatos: Email: claudioed.oliveira@gmail.com Linkedin: https://br.linkedin.com/in/claudioedoliveira Twitter: @claudioed
  3. Agenda • Microservices ◦ Definition ◦ Patterns • Reactive Manifesto

    ◦ Reactive Systems ◦ Reactive Programming • Vert.x
  4. Definition The term "Microservice Architecture" has sprung up over the

    last few years to describe a particular way of designing software applications as suites of independently deployable services. While there is no precise definition of this architectural style, there are certain common characteristics around organization around business capability, automated deployment, intelligence in the endpoints, and decentralized control of languages and data. microservices https://martinfowler.com/articles/microservices.html
  5. Example microservices https://cdn.wp.nginx.com/wp-content/uploads/2016/04/Richardson-microservices-part1-2_microservices-architecture.png

  6. Microservices Patterns • Circuit Breakers • Service Discovery microservices

  7. Circuit Breaker microservices “The basic idea behind the circuit breaker

    is very simple. You wrap a protected function call in a circuit breaker object, which monitors for failures” https://martinfowler.com/bliki/CircuitBreaker.html
  8. Service Discovery microservices Service discovery is the automatic detection of

    devices and services offered by these devices on a computer network https://en.wikipedia.org/wiki/Service_discovery
  9. reactive Reactive Manifesto http://www.reactivemanifesto.org/

  10. Reactive Systems as defined by the Reactive Manifesto—is a set

    of architectural design principles for building modern systems that are well prepared to meet the increasing demands that applications face today. https://www.lightbend.com/reactive-programming-versus-reactive-systems reactive
  11. Reactive Programming In computing, reactive programming is an asynchronous programming

    paradigm concerned with data streams and the propagation of change https://en.wikipedia.org/wiki/Reactive_programming reactive
  12. We will talk about Reactive Systems…. reactive

  13. Some JVM players reactive

  14. None
  15. Definition Eclipse Vert.x is a toolkit for building reactive applications

    on the JVM vert.x http://vertx.io/
  16. Highlights • Non-Blocking (vert.x core) • Polyglot • Event Bus

    • General purpose • Unopinionated • Really fun to code vert.x
  17. Dependencies - Maven vert.x <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-dependencies</artifactId> <version>${vertx.version}</version> <type>pom</type> <scope>import</scope>

    </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</artifactId> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web</artifactId> </dependency>
  18. Core Concepts Verticle vert.x Event Bus

  19. Verticle vert.x • Small Vert Unit • Regular Verticle •

    Worker Verticle • Multi Threaded Worker • Automatic node discovery
  20. Reactor Pattern The reactor pattern is one implementation technique of

    event-driven architecture. In simple terms, it uses a single threaded event loop blocking on resource-emitting events and dispatches them to corresponding handlers and callbacks vert.x https://dzone.com/articles/understanding-reactor-pattern-thread-based-and-eve
  21. Regular Verticle - Event Loop vert.x

  22. Regular Verticle • Golden Rule - Don’t block me! •

    Event Loop (more than one) • Multi Reactor Pattern • High throughput (i.e http) vert.x
  23. public class EventLoopVerticle extends AbstractVerticle { public void start() {

    final Router router = Router.router(vertx); router.get("/test").handler(req -> { req.response() .putHeader("content-type","text/plain") .end(NormalProcess.process()); }); vertx.createHttpServer().requestHandler(router::accept).listen(8080); } } Regular Verticle - Example vert.x
  24. Don't do this !!!! vert.x public class EventLoopBlockerVerticle extends AbstractVerticle

    { public void start() { final Router router = Router.router(vertx); router.get("/test").handler(req -> { LongRunningProcess.longProcess(); final String date = LocalDateTime.now().toString(); req.response() .putHeader("content-type","text/plain") .end(String.format("TDC 2017 %s", date)); }); vertx.createHttpServer().requestHandler(router::accept).listen(8081); } }
  25. Worker Verticle • Vert.x worker thread pool • Execute blocking

    code • They are never executed by more than one thread • Background tasks vert.x
  26. Worker Verticle vert.x public class EventLoopBusVerticle extends AbstractVerticle { public

    void start() { this.vertx.deployVerticle(new BusVerticle(), new DeploymentOptions().setWorker(true)); this.vertx.deployVerticle(new RequestResourceVerticle()); } }
  27. Multi Threaded Verticle • Based on worker verticle • Can

    be executed concurrently by different threads • Hard to code because you need to maintain states between verticles • Specific needs vert.x
  28. Event Bus • Nervous system of Vert.x • Publish /

    Subscribe • Point to Point • Request Response • Can be distributed vert.x
  29. Event Bus vert.x public class RequestResourceVerticle extends AbstractVerticle { private

    static final Logger LOGGER = LoggerFactory.getLogger(BusVerticle.class); public void start() { final EventBus eventBus = vertx.eventBus(); final Router router = Router.router(vertx); router.get("/test").handler(req -> { eventBus.send("data-stream", new JsonObject(), responseBus -> { if (responseBus.succeeded()) { req.response() .putHeader("content-type", "text/plain") .end(responseBus.result().body().toString()); } }); }); vertx.createHttpServer().requestHandler(router::accept).listen(8082); } }
  30. Service Discovery This component provides an infrastructure to publish and

    discover various resources, such as service proxies, HTTP endpoints, data sources…​ vert.x http://vertx.io/docs/vertx-service-discovery/java/
  31. Service Discovery Service provider Publish / Unpublish service record Update

    the status of a service vert.x http://vertx.io/docs/vertx-service-discovery/java/
  32. Service Discovery Service consumer Lookup service Bind to a selected

    service Release the service Listen for arrival and departure vert.x http://vertx.io/docs/vertx-service-discovery/java/
  33. Service Discovery - Backend vert.x final ServiceDiscoveryOptions serviceDiscoveryOptions = new

    ServiceDiscoveryOptions() .setBackendConfiguration( new JsonObject() .put("host", "127.0.0.1") .put("port", "6379") ); ServiceDiscovery sd = ServiceDiscovery.create(vertx,serviceDiscoveryOptions);
  34. Service Discovery - Publish vert.x vertx.createHttpServer().requestHandler(router::accept) .rxListen(8083) .flatMap(httpServer -> discovery

    .rxPublish(HttpEndpoint.createRecord("product", "localhost", 8083, "/"))) .subscribe(rec -> LOGGER.info("Product Service is published"));
  35. Service Discovery - Consumer vert.x final ServiceDiscoveryOptions serviceDiscoveryOptions = new

    ServiceDiscoveryOptions() .setBackendConfiguration( new JsonObject() .put("host", "127.0.0.1") .put("port", "6379") ); ServiceDiscovery discovery = ServiceDiscovery.create(vertx,serviceDiscoveryOptions); …. HttpEndpoint.rxGetWebClient(discovery, rec -> rec.getName().endsWith("product")) .flatMap(client -> client.get("/product/" + id).as(BodyCodec.string()).rxSend()) .subscribe(response -> req.response() .putHeader("content-type", "application/json") .end(response.body())); });
  36. Service Discovery • Consul • Kubernetes • Redis • Docker

    vert.x
  37. Circuit Breaker vert.x https://www.oreilly.com/ideas/microservices-antipatterns-and-pitfalls

  38. breaker.executeWithFallback( future -> { vertx.createHttpClient().getNow(8080, "localhost", "/", response -> {

    if (response.statusCode() != 200) { future.fail("HTTP error"); } else { response .exceptionHandler(future::fail) .bodyHandler(buffer -> { future.complete(buffer.toString()); }); } }); }, v -> { // Executed when the circuit is opened return "Hello"; }) .setHandler(ar -> { // Do something with the result }); Circuit Breaker vert.x
  39. vert.x Demo

  40. None
  41. References http://vertx.io/docs/guide-for-java-devs/ https://www.oreilly.com/ideas/reactive-programming-vs-reactive-systems https://dzone.com/articles/understanding-reactor-pattern-thread-based-and-eve https://developers.redhat.com/promotions/building-reactive-microservices-in-java/ https://github.com/claudioed/travel-helper