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

vertx.pdf

 vertx.pdf

microservices with vert.x

More Decks by Claudio Eduardo de Oliveira

Other Decks in Programming

Transcript

  1. 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: [email protected] Linkedin: https://br.linkedin.com/in/claudioedoliveira Twitter: @claudioed
  2. Agenda • Microservices ◦ Definition ◦ Patterns • Reactive Manifesto

    ◦ Reactive Systems ◦ Reactive Programming • Vert.x
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. Highlights • Non-Blocking (vert.x core) • Polyglot • Event Bus

    • General purpose • Unopinionated • Really fun to code vert.x
  9. 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>
  10. Verticle vert.x • Small Vert Unit • Regular Verticle •

    Worker Verticle • Multi Threaded Worker • Automatic node discovery
  11. 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
  12. Regular Verticle • Golden Rule - Don’t block me! •

    Event Loop (more than one) • Multi Reactor Pattern • High throughput (i.e http) vert.x
  13. 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
  14. 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); } }
  15. Worker Verticle • Vert.x worker thread pool • Execute blocking

    code • They are never executed by more than one thread • Background tasks vert.x
  16. 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()); } }
  17. 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
  18. Event Bus • Nervous system of Vert.x • Publish /

    Subscribe • Point to Point • Request Response • Can be distributed vert.x
  19. 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); } }
  20. 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/
  21. Service Discovery Service provider Publish / Unpublish service record Update

    the status of a service vert.x http://vertx.io/docs/vertx-service-discovery/java/
  22. 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/
  23. 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);
  24. 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"));
  25. 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())); });
  26. 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