In my previous post I looked at creating my first Verticle/Vert.x application. This had a single Verticle acting as a service and running up a server against port 8080. If we therefore assume that by default a Verticle represents a single service then to create multiple service we simply create multi-Verticles.
Let’s create three Verticle’s. The first will act as the “main” verticle (or run as an application) and it will register/deploy the Verticle’s that we’re using to host our two services, the HelloVerticle and WorldVerticle.
Each service will itself create/host an HttpServer. Traditionally this would tend to mean we’d have two ports exposed, one for each service, but Vert.x allows us to create multiple HttpServer’s on the same port and uses something like “round robin” to try to locate a valid route.
Check my previous post for the required pom.xml.
Here’s MainVerticle.java
package com.putridparrot; import io.vertx.core.AbstractVerticle; import io.vertx.ext.web.Router; public class MainVerticle extends AbstractVerticle { @Override public void start() { Router router = Router.router(vertx); vertx.deployVerticle(new HelloVerticle(router)); vertx.deployVerticle(new WorldVerticle(router)); } }
Notice that this Verticle’s job is to both register the other Verticle’s and acts as the main entry point to our application.
You do not need to pass the Router around in this way unless we intend to have multiple routes on the same port. So in this case we’re creating the router that will be used by both services on the same port.
Let’s look at the HelloVerticle.java service (both HelloVerticle and WorldVerticle, in this simple example, are basically the same, but I’ll reproduce all code here anyway).
package com.putridparrot; import io.vertx.core.AbstractVerticle; import io.vertx.ext.web.Router; public class HelloVerticle extends AbstractVerticle { private Router router; public HelloVerticle(Router router) { this.router = router; } @Override public void start() { router.route("/hello").handler(ctx -> { ctx.response() .putHeader("content-type", "text/plain") .end("Hello " + ctx.queryParam("name")); }); vertx.createHttpServer() .requestHandler(router::accept) .listen(8080); System.out.println("HTTP Hello server started on port 8080"); } }
There’s nothing particularly special or different here, compared to my previous posts code. So let’s look at the WorldVerticle.java
package com.putridparrot; import io.vertx.core.AbstractVerticle; import io.vertx.ext.web.Router; public class WorldVerticle extends AbstractVerticle { private Router router; public WorldVerticle(Router router) { this.router = router; } @Override public void start() { router.route("/world").handler(ctx -> { ctx.response() .putHeader("content-type", "text/plain") .end("World " + ctx.queryParam("name")); }); vertx.createHttpServer() .requestHandler(router::accept) .listen(8080); System.out.println("HTTP World server started on port 8080"); } }
What’s interesting is that both HelloVerticle and WorldVerticle create the HTTP server on the same port. As stated earlier Vert.x simply handles this for us without a port conflict and because we’re using a single router across these two Verticle’s Vert.x can correctly located the service for each of the following URL’s
http://localhost:8080/hello?name=Hello http://localhost:8080/world?name=World