How do we use Vert.x at Melusyn?

2cf4fc9951b5dcc35d7cfb274d02d2d5?s=47 Melusyn
December 02, 2015

How do we use Vert.x at Melusyn?

This talk was given during the first edition of the Paris Vert.x User Group in December 2015.
http://www.meetup.com/Paris-vert-x-Meetup/events/226587108/

This event was also filmed (talk in french) : https://www.youtube.com/watch?v=8iS660iVkZE

2cf4fc9951b5dcc35d7cfb274d02d2d5?s=128

Melusyn

December 02, 2015
Tweet

Transcript

  1. Paris Meetup 2 dec. 2015

  2. Vert.x Paris Meetup 2 dec. 2015 How do we use

    Vert.x at Melusyn?
  3. Vert.x Paris Meetup 2 dec. 2015 Michel Guillet @GuilletMichel Hugo

    Cordier @HugoCrd
  4. Vert.x Paris Meetup 2 dec. 2015

  5. Vert.x Paris Meetup 2 dec. 2015 Why Vert.x? • Melusyn

    develops several web applications (SPA) and Mobile applications • These different clients needs to connect to the same service (our API) Needed a REST API
  6. Vert.x Paris Meetup 2 dec. 2015 Wanted it async •

    Which left us with (in 2012) : • Node.js • Play Framework • Spray • Vert.x
  7. Vert.x Paris Meetup 2 dec. 2015 Not typed No multithreading

    NOT TYPED!
  8. Vert.x Paris Meetup 2 dec. 2015 • We did not

    need any templating engine • We wanted to manage persistence ourself Too much dependencies (Opinionated framework)
  9. Vert.x Paris Meetup 2 dec. 2015 Liked the actor model

    but team was not good enough in Scala
  10. Vert.x Paris Meetup 2 dec. 2015 • Reactor pattern •

    Built-in multithreading • Lightweight • Microservices deployment • Open to other languages (Scala, Groovy)
  11. Vert.x Paris Meetup 2 dec. 2015 General architecture

  12. Vert.x Paris Meetup 2 dec. 2015 Architecture

  13. Vert.x Paris Meetup 2 dec. 2015 Architecture

  14. Vert.x Paris Meetup 2 dec. 2015 Dependencies

  15. Vert.x Paris Meetup 2 dec. 2015 Vert.x For Async model,

    Reactor pattern, HTTP server and microservices deployment
  16. Vert.x Paris Meetup 2 dec. 2015 Jackson To (de)serialize messages

    into POJOs
  17. Vert.x Paris Meetup 2 dec. 2015 Hibernate validator To validate

    these POJOs
  18. Vert.x Paris Meetup 2 dec. 2015 public class Car {

    @NotNull private String manufacturer; @NotNull @Size(min = 2, max = 14) private String licensePlate; @Min(2) private int seatCount; // ... }
  19. Vert.x Paris Meetup 2 dec. 2015 RxJava To deal with

    asynchronous events and compose them into a pipe
  20. Vert.x Paris Meetup 2 dec. 2015 getBodyAs(req, Document.class) .flatMap(document ->

    rightsHelper.canWrite(user, document, Document.class)) .flatMap(documentAndUser -> { .. BUSINESS LOGIC ..}) .subscribe( result -> replyOk(result, req), error -> handleError(error, req) );
  21. Vert.x Paris Meetup 2 dec. 2015 HTTP API Authentication Service

    Mongo Service Cloud ! GET /route 200 OK API Hello World
  22. Vert.x Paris Meetup 2 dec. 2015 API Real World Cloud

    ! Authentication Service Mongo Service Authorization Service Media Service 2 3 1 4 5 6 HTTP API 9 7 8 10 13 AWS S3 11 12 14
  23. Vert.x Paris Meetup 2 dec. 2015 public class RouteController extends

    AbstractHttpController {
 
 […] 
 public void pushRessource(HttpServerRequest req) {
 String folderId = req.params().get("folderId");
 
 getBodyAs(req, Document.class)
 .flatMap(document -> getUser(req)
 .flatMap(user -> rightsHelper.canWrite(user, folderId))
 .flatMap(ignored -> documentDao.create(document))
 )
 .map(Document::toJson)
 .subscribe(
 result -> replyOk(result, req),
 error -> handleError(error, req)
 );
 }
 
 } API Real World
  24. Vert.x Paris Meetup 2 dec. 2015 Best practices • Using

    JSON in business logic is a code smell • Use Service proxies to abstract the event bus • Wrap external APIs into Services • Observables everywhere
  25. Vert.x Paris Meetup 2 dec. 2015 Best practices

  26. Vert.x Paris Meetup 2 dec. 2015 Production Deployment

  27. Vert.x Paris Meetup 2 dec. 2015 Deploying Vert.x for real

    vertx runmod com.company:my-module:1.0 -conf config.json & Nice for debug and dev but… in the real world ? Problem Example Log stored in /tmp Not very nice for post mortem… No restart on server reboot Not very nice for (un)expected reboot No restart on JVM crash or unexpected shutdown OOM exception? anyone? One module per JVM - Tedious to start a multi-module cluster for dev - Limit ressource usage No notice of failed startup - Bad config ? - Network port non available? - etc.
  28. Vert.x Paris Meetup 2 dec. 2015 Deploying Vert.x for real

    $ service vertxd start my-module Problem Solution Log stored in /tmp No restart on server reboot No restart on JVM crash or unexpected shutdown One module per JVM No notice of failed startup Reliable, robust and easy logback + Logmatic.io (ELK, etc.) init.d (systemd, upstart, etc.) Jsvc (Java Service Wrapper, YAJSW) Develop custom launcher Develop custom launcher + jsvc $ service vertxd start my-module
  29. Vert.x Paris Meetup 2 dec. 2015 Custom launcher • Set

    JVM properties (-Xmx, debug, etc.) • Set System properties (vertx.maven.remoteRepos, vertx.logger-delegate-factory-class-name, etc.) • Set classpath (Hazlecast, logback, etc.) • Reports startup success or failure • Services configurations
  30. Vert.x Paris Meetup 2 dec. 2015 • 3 files -

    system.properties: - vm.properties - daemon.json (c.f next slide) push to git repo ! Custom launcher # Forces Vert.x to use SLF4J implementation
 vertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory
 
 # Specify logback configuration file
 logback.configurationFile=conf/logback.xml […] -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9005
 -Xmx2048m […]
  31. Vert.x Paris Meetup 2 dec. 2015 Custom launcher daemon.json file

    {
 "vertx": {
 "clustered": true,
 "haEnabled": false,
 "clusterHost": 6541
 },
 "verticles": [{
 "identifier": "maven:io.vertx:vertx-mongo-service:3.1.0::io.vertx.mongo-service",
 "config": {
 "address": "service.mongodb",
 "db_name": "melusyn"
 },
 "deployment": {
 "instances": 4, "worker": false
 }
 },
 {
 "identifier": "maven:com.example:my-service:jar:service:1.0.0-SNAPSHOT",
 "config": {
 "param_1": "value_1",
 "param_2": 654
 }, "disabled": true
 }]
 } io.vertx.core.VertxOptions io.vertx.core.DeploymentOptions Vertical factory service identifier Do not start service
  32. Vert.x Paris Meetup 2 dec. 2015 init.d gifts • Standard

    and sysadmin friendly: Should look like everything else on the server • Automatic start/stop on reboot/shutdown • Service start/stop priority : Maybe you want your DB to start before Vert.x ? • Nice to use: service vertxd start, can you make any simpler ?
  33. Vert.x Paris Meetup 2 dec. 2015 • Implement org.apache.commons.daemon.Daemon 1.

    Init phase (allocate restricted ressources) 2. Synchronous Startup phase 3. Synchronous Stop • When run as root, it can downgrade to an unprivileged user. • You can respond to SIGUSR2 ! (hot configuration reload) Apache Jsvc «Jsvc is a set of libraries and applications for making Java applications run on UNIX more easily.»
  34. Vert.x Paris Meetup 2 dec. 2015 That’s all folks! Michel

    Guillet @GuilletMichel Hugo Cordier @HugoCrd
  35. Vert.x Paris Meetup 2 dec. 2015 Links • Jsvc •

    http://commons.apache.org/proper/commons-daemon/apidocs/ • https://commons.apache.org/proper/commons-daemon/jsvc.html • YAJSW (non tested) http://yajsw.sourceforge.net • Java Service Wrapper (GPL3 or paid licence) http://wrapper.tanukisoftware.com • init.d • http://vertx.io/blog/vert-x-3-init-d-script/ (by Clément E.) • https://wiki.debian.org/LSBInitScripts (headers) • /etc/init.d/skeleton on any debian based distribution • Steal from others ! (Nginx, Postgres, etc.)
  36. Paris Meetup Thanks!