Vert.x v3 - high performance polyglot application toolkit

Post on 07-Jan-2017

1.408 views 0 download

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

vert.x v3high performance

polyglot application toolkit

Hello!Bartek Zdanowski @bartekzdanowski

2

Hello!developer @ vertx loverfather and husband :)

3

vert.x

4

vert.x

5

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

vert.x buzzwords

simple embeddable toolkitthreadSafe concurrent

asynchronous eventDriven reactiveeventBus scalable

polyglot

6

vert.x buzzwords

fun

7

the problem

8

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

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

10

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

11

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

the problem

13

thread pools

the problem

14

thread pools

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

synchronous == blocking

15

the solution

16

the solution

17

thread pools

the solution

18

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

the solution

19

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

the solution

20

asynchronousevent 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 countactor like model

some code

25

26

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

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

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!

threadsafe code

29

threadsafe

30

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

//do some critical stuff}

}

threadsafeonly when one thread!

31

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

//do some critical stuff}

}

threadsafeparticular verticle runs in one thread!

32

33

verticle

34

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

verticle

verticlethreadsafe code■verticle instance - always the same thread

■can have separated classloaders for each verticle instance

35

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

36

verticletypes■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 deployment●deployment options●passing config

39

let’s code!

S1

■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 - subscriberpublish() //broadcastlike JMS Topics, no replying

47

eventBus

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

48

eventBus

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

round robin

49

eventBus

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

round robin

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

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

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

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

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

Consuming a message

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

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!

S2

web client

57

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

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

59

let’s code!

S3

60

vert.x ecosystem

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

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.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

■serving primitive

65

let’s code!

S4

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

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

//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

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 /globalCounter

70

let’s code!

S4b/S4c

71

The Big App

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

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 content■can have multiple instances of same class

76

vert.x vs akka

how vert.x scales

77

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

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 "RUBY: sending Ruby message" eb.publish("producer.address", "Ruby shines!")}

Ruby shines

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!

usecases

83

■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

YOU getting started

85

http://vertx.io/https://github.com/vert-x3https://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.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

90

Thank you