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

Vert.x 3 - Voxxed Days Belgrade

Vert.x 3 - Voxxed Days Belgrade

It's the latest and more improved presentation of vert.x 3

47969d3a306ab00ecd55dc0137d37463?s=128

Bartek Zdanowski

October 02, 2015
Tweet

Transcript

  1. vert.x v3 high performance polyglot application toolkit

  2. Zdravo! Bartek Zdanowski @bartekzdanowski 2

  3. Hello! developer @ vertx lover father and husband :) 3

  4. vert.x 4

  5. vert.x 5 Invented by Tim Fox as Node.x 24 June

    2015 - released v3 still it’s very hot :D
  6. vert.x buzzwords simple embeddable toolkit threadSafe concurrent asynchronous eventDriven reactive

    eventBus scalable polyglot 6
  7. vert.x buzzwords fun 7

  8. the problem 8

  9. the problem number of mobile users raises 2,1 bln in

    2012 -> 7 bln in 2018 Internet of things - 30 bln in 2020! IPv4 is exhausted IPv6 is already ready for all of them 9
  10. the problem traditional synchronous approach ▪ one thread handles one

    task ▪ thread is waiting for job to finish ▪ whole queue of tasks waits 10
  11. the problem traditional synchronous approach ▪ synchronous code ▪ scalability

    problem ▪ lots of threads ▪ concurrency that no-one understands ;) 11
  12. the problem 12 Tomcat: 200 threads = 200 connections rest

    of incoming connections must wait*… *okay, now you can do nonblocking in Tomcat https://tomcat.apache.org/tomcat-7.0-doc/aio.html
  13. the problem 13 thread pools

  14. the problem 14 thread pools

  15. the problem big thread pools are evil synchronous code is

    even more evil concurrency is very hard synchronous == blocking 15
  16. the solution 16

  17. the solution 17 thread pools

  18. the solution 18 1 task = series of loosely coupled

    events event that informs that new job/data is waiting
  19. the solution 19 program should release thread instead of waiting

    for operation (I/O) to be finished use non-blocking IO (NIO)
  20. the solution 20 asynchronous event driven

  21. the solution 21

  22. vert.x 22 dispatcher handlers thread pool reactor

  23. vert.x 23 handlers thread pool thread-1 handler 1 handler 2

  24. vert.x 24 thread pool == double cores count actor like

    model
  25. some code 25

  26. 26 public class HelloVerticle extends AbstractVerticle { @Override public void

    start() { LoggerFactory.getLogger(getClass()).info( "Hello vert.x"); } }
  27. 27 public static final String CONSUMER_ADDRESS = "consumer.address"; public void

    start() { vertx.eventBus().consumer(CONSUMER_ADDRESS, new Handler<Message<String>>() { public void handle(Message<String> message) { logger.info("Got message:" + message.body()); } }); } //Java < 8
  28. 28 public static final String CONSUMER_ADDRESS = "consumer.address"; public void

    start() { vertx.eventBus().consumer(CONSUMER_ADDRESS, message -> { logger.info("Got message: " + message.body()); }); } //Java 8 Lambdas FTW!
  29. threadsafe code 29

  30. threadsafe 30 class MyService { public synchronized Result doSomething(Data data)

    { //do some critical stuff } }
  31. threadsafe only when one thread! 31 class MyService { public

    synchronized Result doSomething(Data data) { //do some critical stuff } }
  32. threadsafe particular verticle runs in one thread! 32

  33. 33 verticle

  34. 34 basic deployment unit actor-like model (akka similarity) always run

    in the same thread classloader isolated can have many instances verticle
  35. verticle threadsafe code ▪ verticle instance - always the same

    thread ▪ separated classloaders for each verticle instance 35
  36. verticle threadsafe code ▪ event bus separates threads ▪ shared

    data: maps, counters, locks 36
  37. 37 eventBus

  38. 38 eventBus

  39. 39 eventBus

  40. 40 eventBus server 1 server 2 server 3

  41. 41 eventBus server 1 webclient server 2 server 3

  42. 42 eventBus Publisher - subscriber publish() //broadcast like JMS Topics,

    no replying
  43. 43 eventBus p2p send() //point-to-point like JMS Queues, can reply

  44. 44 eventBus p2p send() //point-to-point like JMS Queues, can reply

    round robin
  45. 45 eventBus p2p send() //point-to-point like JMS Queues, can reply

    round robin
  46. 46 public void start() { getLogger().info("Broadcaster started"); vertx.setPeriodic(PERIOD_MS, timerID ->

    { getLogger().info("Broadcasting message " + counter); vertx.eventBus().publish(Consumer.CONSUMER_ADDRESS, "Message " + counter); counter++; }); } Broadcasting a message
  47. 47 public void start() { getLogger().info("Broadcaster started"); vertx.setPeriodic(PERIOD_MS, timerID ->

    { getLogger().info("Broadcasting message " + counter); vertx.eventBus().send(Consumer.CONSUMER_ADDRESS, "Message " + counter); counter++; }); } Sending a message p2p
  48. 48 public void start() { getLogger().info("Consumer started! " + hashCode());

    vertx.eventBus().consumer(CONSUMER_ADDRESS, message -> { getLogger().info("Received message: " + message.body()); }); } Consuming a message
  49. public void start() { getLogger().info("Consumer started! " + hashCode()); vertx.eventBus().consumer(CONSUMER_ADDRESS,

    message -> { getLogger().info("Received message: " + message.body()); message.reply("I got your message"); }); } 49 Consuming a message + replying
  50. 50 vert.x ecosystem

  51. 51 lightweight vert.x core extensions ▪ web ▪ data access

    (mongoDB, redis, JDBC) ▪ security (basic auth, jdbc auth, jwt, shiro) ▪ reactive (based on RxJava) ▪ others vert.x ecosystem
  52. 52 web server

  53. 53 web server ▪ vert.x core - very raw and

    basic HTTP server ▪ web server ext - neat path and content routing
  54. 54 vertx.createHttpServer().requestHandler( req -> { LOG.info(String.format("Got request [%s]", req.path())); switch

    (req.path()) { case "/" : req.response().end("Ok. Here's root"); break; case "/other": req.response().end("Other things..."); break; default: req.response().setStatusCode(404).end("Unknown resource!"); } }).listen(port) vert.x core - very raw and basic HTTP server
  55. 55 Router router = new RouterImpl(vertx); router.get("/users/:uid").handler( ctx -> {

    String id = ctx.request().getParam("uid"); JsonObject user = new JsonObject().put("id", id).put("name", "bartek"); ctx.response().end(user.encode()); }); HttpServer server = vertx.createHttpServer(); server.requestHandler(router::accept).listen(8080); web server ext - neat path and content routing
  56. 56 router.post("/some/post/path") router.put() router.get("/user/data").consumes("application/json") router.get("/user/data").consumes("text/html") router.get("/info").produces("text/html") router.get("/info").produces("text/plain") web server ext

    - content routing
  57. 57 benchmarking

  58. 58 benchmarking https://www.techempower.com/benchmarks/#section=data-r8&hw=i7&test=plaintext

  59. 59 vert.x vs akka

  60. verticles: ▪ are actor-like ▪ can be addressed ▪ can

    subscribe to many addresses ▪ can do processing jobs ▪ can send back content ▪ can have multiple instances of same class 60 vert.x vs akka
  61. how vert.x scales 61

  62. vertically ▪ verticle can have multiple instances ▪ each instance

    can have own processor core horizontally ▪ clustering - lot’s of nodes ▪ eventbus reaches all nodes 62 how vert.x scales
  63. polyglot 63

  64. ▪ java ▪ groovy ▪ javascript ▪ ruby ▪ ...more

    to come! 64 polyglot
  65. 65 puts "Ruby for the win!" eb = $vertx.event_bus() #

    Send a message every second $vertx.set_periodic(1000) { |v| puts "RUBY: sending Ruby message" eb.publish("producer.address", "Ruby shines!") } Ruby shines
  66. 66 var eb = vertx.eventBus(); eb.consumer("producer.address", function (message) { console.log("JAVASCRIPT:

    Received a message: " + message.body()); }); console.log("Receiver ready!"); For JavaScript dare devils!
  67. usecases 67

  68. ▪ effective use of servers’ resources ▪ parallel computing ▪

    great as a backend for lots of (mobile) clients ▪ nice integration platform ▪ could be awesome in microservices 68 usecases
  69. YOU getting started 69

  70. http://vertx.io/ https://github.com/vert-x3 https://github.com/zdanek/vertx-pres 70 YOU getting started

  71. grab a sticker! 71 YOU getting started

  72. have fun! 72 YOU getting started

  73. I’m not the owner of the pictures used. Just found

    them on: http://www.rantchic.com/wp-content/uploads/2013/12/Apollo-13.jpg http://www.gamesaktuell.de/screenshots/1280x1024/2009/04/terminator_2_blu_ray03.jpg http://tripoutlook.com/wp-content/uploads/2013/03/Car-parking.jpg http://upload.wikimedia.org/wikipedia/commons/5/52/Parallel_Parking_cars.jpg http://www.foreverbarcelona.com/wp-content/uploads/2014/02/Taxi-Tips.png http://www.125p.eu/wp-content/uploads/2009/03/fiat-125p-zmiennicy-5.jpg http://www.novosti.rs/upload/images/2011/07/2107/bg-taksi.jpg http://i.livescience.com/images/i/000/024/292/iFF/neurons-120208.jpg?1328727600 73 All the pics used
  74. 74 Hvala!