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

How do we use Vert.x at Melusyn?

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

Melusyn

December 02, 2015
Tweet

Other Decks in Programming

Transcript

  1. 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
  2. Vert.x Paris Meetup 2 dec. 2015 Wanted it async •

    Which left us with (in 2012) : • Node.js • Play Framework • Spray • Vert.x
  3. 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)
  4. Vert.x Paris Meetup 2 dec. 2015 Liked the actor model

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

    Built-in multithreading • Lightweight • Microservices deployment • Open to other languages (Scala, Groovy)
  6. Vert.x Paris Meetup 2 dec. 2015 Vert.x For Async model,

    Reactor pattern, HTTP server and microservices deployment
  7. 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; // ... }
  8. Vert.x Paris Meetup 2 dec. 2015 RxJava To deal with

    asynchronous events and compose them into a pipe
  9. 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) );
  10. Vert.x Paris Meetup 2 dec. 2015 HTTP API Authentication Service

    Mongo Service Cloud ! GET /route 200 OK API Hello World
  11. 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
  12. 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
  13. 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
  14. 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.
  15. 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
  16. 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
  17. 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 […]
  18. 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
  19. 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 ?
  20. 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.»
  21. Vert.x Paris Meetup 2 dec. 2015 That’s all folks! Michel

    Guillet @GuilletMichel Hugo Cordier @HugoCrd
  22. 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.)