Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
vert.x v3
high performance
polyglot application toolkit
Hello!
Bartek Zdanowski
@bartekzdanowski
2
Hello!
developer @
vertx lover
father and husband :)
3
vert.x
4
vert.x
5
Invented by Tim Fox as Node.x
24 June 2015 - released v3
still it’s very hot :D
vert.x buzzwords
simple embeddable toolkit
threadSafe concurrent
asynchronous eventDriven reactive
eventBus scalable
polyg...
vert.x buzzwords
fun
7
the problem
8
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 ex...
the problem
traditional synchronous approach
■one thread handles one task
■thread is waiting for job to finish
■whole queu...
the problem
traditional synchronous approach
■synchronous code
■scalability problem
■lots of threads
■concurrency that no-...
the problem
12
Tomcat: 200 threads = 200 connections
rest of incoming connections must wait*…
*okay, now you can do nonblo...
the problem
13
thread pools
the problem
14
thread pools
the problem
big thread pools are evil
synchronous code is even more evil
concurrency is very hard
synchronous == blocking
...
the solution
16
the solution
17
thread pools
the solution
18
1 task = series of loosely coupled events
event that informs that new job/data is
waiting
the solution
19
program should release thread instead of
waiting for operation (I/O) to be finished
use non-blocking IO (N...
the solution
20
asynchronous
event driven
the solution
21
vert.x
22
dispatcherhandlers
thread pool
reactor
vert.x
23
handlers
thread pool
thread-1
handler 1
handler 2
vert.x
24
thread pool == double cores count
actor like model
some code
25
26
public class HelloVerticle extends AbstractVerticle {
@Override
public void start() {
LoggerFactory.getLogger(getClass(...
27
public static final String CONSUMER_ADDRESS =
"consumer.address";
public void start() {
vertx.eventBus().consumer(CONSU...
28
public static final String CONSUMER_ADDRESS =
"consumer.address";
public void start() {
vertx.eventBus().consumer(CONSU...
threadsafe code
29
threadsafe
30
class MyService {
public synchronized Result doSomething(Data data) {
//do some critical stuff
}
}
threadsafe
only when one thread!
31
class MyService {
public synchronized Result doSomething(Data data) {
//do some critic...
threadsafe
particular verticle runs in one thread!
32
33
verticle
34
basic deployment unit
actor-like model (akka similarity)
always run in the same thread
can have many instances
verticle
verticle
threadsafe code
■verticle instance - always the same thread
■can have separated classloaders for each
verticle in...
verticle
threadsafe code
■event bus separates threads
■shared data: maps, counters, locks
36
verticle
types
■standard
■worker
■multi-threaded worker
37
let’s code!
38
■hello world vert.x - first verticle
■deploying other verticles
●one instance
●many instances
●other after successful depl...
■running from IDE
■running from command line
●java -jar
●vertx run
40
let’s code!
S1
41
eventBus
42
eventBus
43
eventBus
44
eventBus
server 1
server 2
server 3
45
eventBus
server 1
webclientserver 2
server 3
46
eventBus
Publisher - subscriber
publish() //broadcast
like JMS Topics, no replying
47
eventBus
p2p
send() //point-to-point
like JMS Queues, can reply
48
eventBus
p2p
send() //point-to-point
like JMS Queues, can reply
round robin
49
eventBus
p2p
send() //point-to-point
like JMS Queues, can reply
round robin
50
public void start() {
getLogger().info("Broadcaster started");
vertx.setPeriodic(PERIOD_MS, timerID -> {
getLogger().in...
51
public void start() {
getLogger().info("Broadcaster started");
vertx.setPeriodic(PERIOD_MS, timerID -> {
getLogger().in...
52
public void start() {
getLogger().info("Consumer started! " + hashCode());
vertx.eventBus().consumer(CONSUMER_ADDRESS, ...
public void start() {
getLogger().info("Consumer started! " + hashCode());
vertx.eventBus().consumer(CONSUMER_ADDRESS, mes...
let’s code!
54
■periodic events
■sending/receiving events
●publish-subscribe / broadcast
○ one producer
○a few consumers
55
let’s code!
S2
■sending/receiving events
●point2p
○one consumer
○one consumer with response
○ a few consumers with response
56
let’s code...
web client
57
58
HttpClientOptions opts = new HttpClientOptions()
.setDefaultHost("some.host.com");
HttpClient client = vertx.createHttp...
■pulling data from http server
■parsing data with multiple verticles
59
let’s code!
S3
60
vert.x ecosystem
61
lightweight vert.x core
extensions
■web
■data access (mongoDB, redis, JDBC)
■security (basic auth, jdbc auth, jwt, shir...
62
web server
63
web server
■vert.x core - very raw and basic HTTP server
■web server ext - neat path and content
routing
64
vertx.createHttpServer().requestHandler( req -> {
LOG.info(String.format("Got request [%s]", req.path()));
switch (req....
■serving primitive
65
let’s code!
S4
66
Router router = new RouterImpl(vertx);
router.get("/users/:uid").handler( ctx -> {
String id = ctx.request().getParam("...
67
router.post("/some/post/path")
router.put()
router.get("/user/data").consumes("application/json")
router.get("/user/dat...
68
//SessionHandler must be preceded with SessionHandler
router.route().handler(CookieHandler.create());
router.route().ha...
69
vertx.sharedData().getLocalMap("myMap").get("myKeyInMap")
accessing shared data
■adding EventBus via websockets
■using shared data S4c
■using web session
■adding REST endpoints
●GET /sessionCounter
●GET...
71
The Big App
let’s create Jenkins Monitor
■fetching build statuses parallely
■storing build statuses in shared map
■serving builds list...
73
benchmarking
74
benchmarking
https://www.techempower.com/benchmarks/#section=data-r8&hw=i7&test=plaintext
75
vert.x vs akka
verticles:
■are actor-like
■can be addressed
■can subscribe to many addresses
■can do processing jobs
■can send back conte...
how vert.x scales
77
vertically
■verticle can have multiple instances
■each instance can have own processor core
horizontally
■clustering - lot...
polyglot
79
■java
■groovy
■javascript
■ruby
■...more to come!
80
polyglot
81
puts "Ruby for the win!"
eb = $vertx.event_bus()
# Send a message every second
$vertx.set_periodic(1000) { |v|
puts "RU...
82
var eb = vertx.eventBus();
eb.consumer("producer.address", function (message) {
console.log("JAVASCRIPT: Received a mes...
usecases
83
■effective use of servers’ resources
■parallel computing
■great as a backend for lots of (mobile)
clients
■nice integratio...
YOU getting started
85
http://vertx.io/
https://github.com/vert-x3
https://github.com/zdanek/vertx-pres
86
YOU getting started
grab a sticker!
87
YOU getting started
have fun!
88
YOU getting started
I’m not the owner of the pictures used. Just found them on:
http://www.rantchic.com/wp-content/uploads/2013/12/Apollo-13.j...
90
Thank you
Upcoming SlideShare
Loading in …5
×

Vert.x v3 - high performance polyglot application toolkit

2,049 views

Published on

Presentation prepared by Bartek Zdanowski.

Vert.x v3 - high performance polyglot application toolkit

  1. 1. vert.x v3 high performance polyglot application toolkit
  2. 2. Hello! Bartek Zdanowski @bartekzdanowski 2
  3. 3. Hello! developer @ vertx lover father and husband :) 3
  4. 4. vert.x 4
  5. 5. vert.x 5 Invented by Tim Fox as Node.x 24 June 2015 - released v3 still it’s very hot :D
  6. 6. vert.x buzzwords simple embeddable toolkit threadSafe concurrent asynchronous eventDriven reactive eventBus scalable polyglot 6
  7. 7. vert.x buzzwords fun 7
  8. 8. the problem 8
  9. 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. 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. 11. the problem traditional synchronous approach ■synchronous code ■scalability problem ■lots of threads ■concurrency that no-one understands ;) 11
  12. 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. 13. the problem 13 thread pools
  14. 14. the problem 14 thread pools
  15. 15. the problem big thread pools are evil synchronous code is even more evil concurrency is very hard synchronous == blocking 15
  16. 16. the solution 16
  17. 17. the solution 17 thread pools
  18. 18. the solution 18 1 task = series of loosely coupled events event that informs that new job/data is waiting
  19. 19. the solution 19 program should release thread instead of waiting for operation (I/O) to be finished use non-blocking IO (NIO)
  20. 20. the solution 20 asynchronous event driven
  21. 21. the solution 21
  22. 22. vert.x 22 dispatcherhandlers thread pool reactor
  23. 23. vert.x 23 handlers thread pool thread-1 handler 1 handler 2
  24. 24. vert.x 24 thread pool == double cores count actor like model
  25. 25. some code 25
  26. 26. 26 public class HelloVerticle extends AbstractVerticle { @Override public void start() { LoggerFactory.getLogger(getClass()).info( "Hello vert.x"); } }
  27. 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. 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. 29. threadsafe code 29
  30. 30. threadsafe 30 class MyService { public synchronized Result doSomething(Data data) { //do some critical stuff } }
  31. 31. threadsafe only when one thread! 31 class MyService { public synchronized Result doSomething(Data data) { //do some critical stuff } }
  32. 32. threadsafe particular verticle runs in one thread! 32
  33. 33. 33 verticle
  34. 34. 34 basic deployment unit actor-like model (akka similarity) always run in the same thread can have many instances verticle
  35. 35. verticle threadsafe code ■verticle instance - always the same thread ■can have separated classloaders for each verticle instance 35
  36. 36. verticle threadsafe code ■event bus separates threads ■shared data: maps, counters, locks 36
  37. 37. verticle types ■standard ■worker ■multi-threaded worker 37
  38. 38. let’s code! 38
  39. 39. ■hello world vert.x - first verticle ■deploying other verticles ●one instance ●many instances ●other after successful deployment ●deployment options ●passing config 39 let’s code! S1
  40. 40. ■running from IDE ■running from command line ●java -jar ●vertx run 40 let’s code! S1
  41. 41. 41 eventBus
  42. 42. 42 eventBus
  43. 43. 43 eventBus
  44. 44. 44 eventBus server 1 server 2 server 3
  45. 45. 45 eventBus server 1 webclientserver 2 server 3
  46. 46. 46 eventBus Publisher - subscriber publish() //broadcast like JMS Topics, no replying
  47. 47. 47 eventBus p2p send() //point-to-point like JMS Queues, can reply
  48. 48. 48 eventBus p2p send() //point-to-point like JMS Queues, can reply round robin
  49. 49. 49 eventBus p2p send() //point-to-point like JMS Queues, can reply round robin
  50. 50. 50 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
  51. 51. 51 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
  52. 52. 52 public void start() { getLogger().info("Consumer started! " + hashCode()); vertx.eventBus().consumer(CONSUMER_ADDRESS, message -> { getLogger().info("Received message: " + message.body()); }); } Consuming a message
  53. 53. 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"); }); } 53 Consuming a message + replying
  54. 54. let’s code! 54
  55. 55. ■periodic events ■sending/receiving events ●publish-subscribe / broadcast ○ one producer ○a few consumers 55 let’s code! S2
  56. 56. ■sending/receiving events ●point2p ○one consumer ○one consumer with response ○ a few consumers with response 56 let’s code! S2
  57. 57. web client 57
  58. 58. 58 HttpClientOptions opts = new HttpClientOptions() .setDefaultHost("some.host.com"); HttpClient client = vertx.createHttpClient(opts); client.get("/request/uri").handler(resp -> { resp.bodyHandler(bodyBuffer -> { System.out.println("here's the body"); System.out.println(bodyBuffer.toString()); }); }).end(); http client
  59. 59. ■pulling data from http server ■parsing data with multiple verticles 59 let’s code! S3
  60. 60. 60 vert.x ecosystem
  61. 61. 61 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
  62. 62. 62 web server
  63. 63. 63 web server ■vert.x core - very raw and basic HTTP server ■web server ext - neat path and content routing
  64. 64. 64 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
  65. 65. ■serving primitive 65 let’s code! S4
  66. 66. 66 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
  67. 67. 67 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
  68. 68. 68 //SessionHandler must be preceded with SessionHandler router.route().handler(CookieHandler.create()); router.route().handler( SessionHandler.create(LocalSessionStore.create(vertx))); router.get("/sessionCounter").handler(ctx -> { ctx.session().get("counter"); } web server ext - accessing session
  69. 69. 69 vertx.sharedData().getLocalMap("myMap").get("myKeyInMap") accessing shared data
  70. 70. ■adding EventBus via websockets ■using shared data S4c ■using web session ■adding REST endpoints ●GET /sessionCounter ●GET /globalCounter 70 let’s code! S4b/S4c
  71. 71. 71 The Big App
  72. 72. let’s create Jenkins Monitor ■fetching build statuses parallely ■storing build statuses in shared map ■serving builds list GET /jobs ■serving build status GET /job/id 72 let’s code! S5
  73. 73. 73 benchmarking
  74. 74. 74 benchmarking https://www.techempower.com/benchmarks/#section=data-r8&hw=i7&test=plaintext
  75. 75. 75 vert.x vs akka
  76. 76. 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 76 vert.x vs akka
  77. 77. how vert.x scales 77
  78. 78. vertically ■verticle can have multiple instances ■each instance can have own processor core horizontally ■clustering - lot’s of nodes ■eventbus reaches all nodes 78 how vert.x scales
  79. 79. polyglot 79
  80. 80. ■java ■groovy ■javascript ■ruby ■...more to come! 80 polyglot
  81. 81. 81 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
  82. 82. 82 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!
  83. 83. usecases 83
  84. 84. ■effective use of servers’ resources ■parallel computing ■great as a backend for lots of (mobile) clients ■nice integration platform ■could be awesome in microservices 84 usecases
  85. 85. YOU getting started 85
  86. 86. http://vertx.io/ https://github.com/vert-x3 https://github.com/zdanek/vertx-pres 86 YOU getting started
  87. 87. grab a sticker! 87 YOU getting started
  88. 88. have fun! 88 YOU getting started
  89. 89. 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 89 All the pics used
  90. 90. 90 Thank you

×