Vert.x v3 - high performance polyglot application toolkit

90
vert.x v3 high performance polyglot application toolkit

Transcript of Vert.x v3 - high performance polyglot application toolkit

Page 1: Vert.x v3 - high performance  polyglot application toolkit

vert.x v3high performance

polyglot application toolkit

Page 2: Vert.x v3 - high performance  polyglot application toolkit

Hello!Bartek Zdanowski @bartekzdanowski

2

Page 3: Vert.x v3 - high performance  polyglot application toolkit

Hello!developer @ vertx loverfather and husband :)

3

Page 4: Vert.x v3 - high performance  polyglot application toolkit

vert.x

4

Page 5: Vert.x v3 - high performance  polyglot application toolkit

vert.x

5

Invented by Tim Fox as Node.x24 June 2015 - released v3still it’s very hot :D

Page 6: Vert.x v3 - high performance  polyglot application toolkit

vert.x buzzwords

simple embeddable toolkitthreadSafe concurrent

asynchronous eventDriven reactiveeventBus scalable

polyglot

6

Page 7: Vert.x v3 - high performance  polyglot application toolkit

vert.x buzzwords

fun

7

Page 8: Vert.x v3 - high performance  polyglot application toolkit

the problem

8

Page 9: Vert.x v3 - high performance  polyglot application toolkit

the problemnumber of mobile users raises2,1 bln in 2012 -> 7 bln in 2018Internet of things - 30 bln in 2020!IPv4 is exhaustedIPv6 is already ready for all of them

9

Page 10: Vert.x v3 - high performance  polyglot application toolkit

the problemtraditional synchronous approach■one thread handles one task■thread is waiting for job to finish■whole queue of tasks waits

10

Page 11: Vert.x v3 - high performance  polyglot application toolkit

the problemtraditional synchronous approach■synchronous code■scalability problem■lots of threads■concurrency that no-one understands ;)

11

Page 12: Vert.x v3 - high performance  polyglot application toolkit

the problem

12

Tomcat: 200 threads = 200 connectionsrest of incoming connections must wait*…

*okay, now you can do nonblocking in Tomcathttps://tomcat.apache.org/tomcat-7.0-doc/aio.html

Page 13: Vert.x v3 - high performance  polyglot application toolkit

the problem

13

thread pools

Page 14: Vert.x v3 - high performance  polyglot application toolkit

the problem

14

thread pools

Page 15: Vert.x v3 - high performance  polyglot application toolkit

the problembig thread pools are evilsynchronous code is even more evilconcurrency is very hard

synchronous == blocking

15

Page 16: Vert.x v3 - high performance  polyglot application toolkit

the solution

16

Page 17: Vert.x v3 - high performance  polyglot application toolkit

the solution

17

thread pools

Page 18: Vert.x v3 - high performance  polyglot application toolkit

the solution

18

1 task = series of loosely coupled eventsevent that informs that new job/data is waiting

Page 19: Vert.x v3 - high performance  polyglot application toolkit

the solution

19

program should release thread instead of waiting for operation (I/O) to be finisheduse non-blocking IO (NIO)

Page 20: Vert.x v3 - high performance  polyglot application toolkit

the solution

20

asynchronousevent driven

Page 21: Vert.x v3 - high performance  polyglot application toolkit

the solution

21

Page 22: Vert.x v3 - high performance  polyglot application toolkit

vert.x

22

dispatcherhandlers

thread pool

reactor

Page 23: Vert.x v3 - high performance  polyglot application toolkit

vert.x

23

handlers

thread pool

thread-1

handler 1

handler 2

Page 24: Vert.x v3 - high performance  polyglot application toolkit

vert.x

24

thread pool == double cores countactor like model

Page 25: Vert.x v3 - high performance  polyglot application toolkit

some code

25

Page 26: Vert.x v3 - high performance  polyglot application toolkit

26

public class HelloVerticle extends AbstractVerticle { @Override public void start() { LoggerFactory.getLogger(getClass()).info( "Hello vert.x"); }}

Page 27: Vert.x v3 - high performance  polyglot application toolkit

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

Page 28: Vert.x v3 - high performance  polyglot application toolkit

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!

Page 29: Vert.x v3 - high performance  polyglot application toolkit

threadsafe code

29

Page 30: Vert.x v3 - high performance  polyglot application toolkit

threadsafe

30

class MyService {public synchronized Result doSomething(Data data) {

//do some critical stuff}

}

Page 31: Vert.x v3 - high performance  polyglot application toolkit

threadsafeonly when one thread!

31

class MyService {public synchronized Result doSomething(Data data) {

//do some critical stuff}

}

Page 32: Vert.x v3 - high performance  polyglot application toolkit

threadsafeparticular verticle runs in one thread!

32

Page 33: Vert.x v3 - high performance  polyglot application toolkit

33

verticle

Page 34: Vert.x v3 - high performance  polyglot application toolkit

34

basic deployment unitactor-like model (akka similarity)always run in the same threadcan have many instances

verticle

Page 35: Vert.x v3 - high performance  polyglot application toolkit

verticlethreadsafe code■verticle instance - always the same thread

■can have separated classloaders for each verticle instance

35

Page 36: Vert.x v3 - high performance  polyglot application toolkit

verticlethreadsafe code■event bus separates threads■shared data: maps, counters, locks

36

Page 37: Vert.x v3 - high performance  polyglot application toolkit

verticletypes■standard■worker■multi-threaded worker

37

Page 38: Vert.x v3 - high performance  polyglot application toolkit

let’s code!

38

Page 39: Vert.x v3 - high performance  polyglot application toolkit

■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

Page 40: Vert.x v3 - high performance  polyglot application toolkit

■running from IDE■running from command line

●java -jar●vertx run

40

let’s code!

S1

Page 41: Vert.x v3 - high performance  polyglot application toolkit

41

eventBus

Page 42: Vert.x v3 - high performance  polyglot application toolkit

42

eventBus

Page 43: Vert.x v3 - high performance  polyglot application toolkit

43

eventBus

Page 44: Vert.x v3 - high performance  polyglot application toolkit

44

eventBus

server 1

server 2

server 3

Page 45: Vert.x v3 - high performance  polyglot application toolkit

45

eventBus

server 1

webclientserver 2

server 3

Page 46: Vert.x v3 - high performance  polyglot application toolkit

46

eventBus

Publisher - subscriberpublish() //broadcastlike JMS Topics, no replying

Page 47: Vert.x v3 - high performance  polyglot application toolkit

47

eventBus

p2psend() //point-to-pointlike JMS Queues, can reply

Page 48: Vert.x v3 - high performance  polyglot application toolkit

48

eventBus

p2psend() //point-to-pointlike JMS Queues, can reply

round robin

Page 49: Vert.x v3 - high performance  polyglot application toolkit

49

eventBus

p2psend() //point-to-pointlike JMS Queues, can reply

round robin

Page 50: Vert.x v3 - high performance  polyglot application toolkit

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

Page 51: Vert.x v3 - high performance  polyglot application toolkit

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

Page 52: Vert.x v3 - high performance  polyglot application toolkit

52

public void start() { getLogger().info("Consumer started! " + hashCode());

vertx.eventBus().consumer(CONSUMER_ADDRESS, message -> {

getLogger().info("Received message: " + message.body()); });}

Consuming a message

Page 53: Vert.x v3 - high performance  polyglot application toolkit

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

Page 54: Vert.x v3 - high performance  polyglot application toolkit

let’s code!

54

Page 55: Vert.x v3 - high performance  polyglot application toolkit

■periodic events■sending/receiving events

●publish-subscribe / broadcast○one producer○a few consumers

55

let’s code!

S2

Page 56: Vert.x v3 - high performance  polyglot application toolkit

■sending/receiving events●point2p

○one consumer ○one consumer with response○a few consumers with response

56

let’s code!

S2

Page 57: Vert.x v3 - high performance  polyglot application toolkit

web client

57

Page 58: Vert.x v3 - high performance  polyglot application toolkit

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

Page 59: Vert.x v3 - high performance  polyglot application toolkit

■pulling data from http server■parsing data with multiple verticles

59

let’s code!

S3

Page 60: Vert.x v3 - high performance  polyglot application toolkit

60

vert.x ecosystem

Page 61: Vert.x v3 - high performance  polyglot application toolkit

61

lightweight vert.x coreextensions■web■data access (mongoDB, redis, JDBC)■security (basic auth, jdbc auth, jwt, shiro)

■reactive (based on RxJava)■others

vert.x ecosystem

Page 62: Vert.x v3 - high performance  polyglot application toolkit

62

web server

Page 63: Vert.x v3 - high performance  polyglot application toolkit

63

web server■vert.x core - very raw and basic HTTP server

■web server ext - neat path and content routing

Page 64: Vert.x v3 - high performance  polyglot application toolkit

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

Page 65: Vert.x v3 - high performance  polyglot application toolkit

■serving primitive

65

let’s code!

S4

Page 66: Vert.x v3 - high performance  polyglot application toolkit

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

Page 67: Vert.x v3 - high performance  polyglot application toolkit

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

Page 68: Vert.x v3 - high performance  polyglot application toolkit

68

//SessionHandler must be preceded with SessionHandlerrouter.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

Page 69: Vert.x v3 - high performance  polyglot application toolkit

69

vertx.sharedData().getLocalMap("myMap").get("myKeyInMap")

accessing shared data

Page 70: Vert.x v3 - high performance  polyglot application toolkit

■adding EventBus via websockets■using shared data S4c

■using web session■adding REST endpoints

●GET /sessionCounter●GET /globalCounter

70

let’s code!

S4b/S4c

Page 71: Vert.x v3 - high performance  polyglot application toolkit

71

The Big App

Page 72: Vert.x v3 - high performance  polyglot application toolkit

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

Page 73: Vert.x v3 - high performance  polyglot application toolkit

73

benchmarking

Page 74: Vert.x v3 - high performance  polyglot application toolkit

74

benchmarking

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

Page 75: Vert.x v3 - high performance  polyglot application toolkit

75

vert.x vs akka

Page 76: Vert.x v3 - high performance  polyglot application toolkit

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

Page 77: Vert.x v3 - high performance  polyglot application toolkit

how vert.x scales

77

Page 78: Vert.x v3 - high performance  polyglot application toolkit

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

Page 79: Vert.x v3 - high performance  polyglot application toolkit

polyglot

79

Page 80: Vert.x v3 - high performance  polyglot application toolkit

■java■groovy■javascript■ruby■...more to come!

80

polyglot

Page 81: Vert.x v3 - high performance  polyglot application toolkit

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

Page 82: Vert.x v3 - high performance  polyglot application toolkit

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!

Page 83: Vert.x v3 - high performance  polyglot application toolkit

usecases

83

Page 84: Vert.x v3 - high performance  polyglot application toolkit

■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

Page 85: Vert.x v3 - high performance  polyglot application toolkit

YOU getting started

85

Page 86: Vert.x v3 - high performance  polyglot application toolkit

http://vertx.io/https://github.com/vert-x3https://github.com/zdanek/vertx-pres

86

YOU getting started

Page 87: Vert.x v3 - high performance  polyglot application toolkit

grab a sticker!

87

YOU getting started

Page 88: Vert.x v3 - high performance  polyglot application toolkit

have fun!

88

YOU getting started

Page 89: Vert.x v3 - high performance  polyglot application toolkit

I’m not the owner of the pictures used. Just found them on:http://www.rantchic.com/wp-content/uploads/2013/12/Apollo-13.jpghttp://www.gamesaktuell.de/screenshots/1280x1024/2009/04/terminator_2_blu_ray03.jpghttp://tripoutlook.com/wp-content/uploads/2013/03/Car-parking.jpghttp://upload.wikimedia.org/wikipedia/commons/5/52/Parallel_Parking_cars.jpghttp://www.foreverbarcelona.com/wp-content/uploads/2014/02/Taxi-Tips.pnghttp://www.125p.eu/wp-content/uploads/2009/03/fiat-125p-zmiennicy-5.jpghttp://www.novosti.rs/upload/images/2011/07/2107/bg-taksi.jpghttp://i.livescience.com/images/i/000/024/292/iFF/neurons-120208.jpg?1328727600

89

All the pics used

Page 90: Vert.x v3 - high performance  polyglot application toolkit

90

Thank you