We may get to a point whereby we have multiple Vert.x applications running and we want one Verticle to communicate with another – this is easy enough if the IP address and port are fixed but not so easy in more scalable/real-world scenarios where we cannot guarantee these are fixed.
In such situations we can use service discovery to locate other services.
Before we get started with the code, we need to add the following to the pom.xml
<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-service-discovery</artifactId> <version>${vertx.version}</version> </dependency>
I’m using vertx.version 3.5.0 in my examples.
Publishing/Registering our Verticle with ServiceDiscovery
To register our Verticle with ServiceDisovery we create a Record object which tells the ServiceDiscovery how to access a Verticle, this includes it’s host/IP, port and service root along with a name for other code to use to locate the service. For example
Record record = HttpEndpoint.createRecord( "hello-service", "localhost", 8080, "/hello");
So this basically says, create a Record named “hello-service” (the key or name of the service) and it’s IP/host is localhost, obviously this is just for my testing. Next we supply the exposed port and finally the root of the service.
We then publish this record to the ServiceDiscovery object like this
discovery.publish(record, ar -> { if (ar.succeeded()) { // publication succeeded publishedRecord = ar.result(); } else { // publication failed } });
Upon success we store the Record (in this case we only do this if the call succeeded) so that we can unpublish the service if it’s shutdown.
Let’s look at the full code for a simplified HelloVerticle
public class HelloVerticle extends AbstractVerticle { private ServiceDiscovery discovery; private Record publishedRecord; @Override public void start() { discovery = new DiscoveryImpl(vertx, new ServiceDiscoveryOptions()); Router router = Router.router(vertx); router.get("/hello").handler(ctx -> { ctx.response() .putHeader("content-type", "text/plain") .end("hello"); }); Record record = HttpEndpoint.createRecord( "hello-service", "localhost", 8080, "/hello"); discovery.publish(record, ar -> { if (ar.succeeded()) { // publication success publishedRecord = ar.result(); } else { // publication failure } }); vertx .createHttpServer() .requestHandler(router::accept) .listen(8080, ar -> { // handle success/failure }); } @Override public void stop() { if(discovery != null) { discovery.unpublish(publishedRecord.getRegistration(), ar -> { if (ar.succeeded()) { // Success } else { // cannot unpublish the service, // may have already been removed, // or the record is not published } }); discovery.close(); } } }
Locating a service via ServiceDiscovery
Let’s take a look at some “consumer” code which will use service discovery to locate our “HelloVerticle”. As expected we need to create access to the ServiceDiscovery object and then we try to locate the Record for a previously added Record.
In the example, below, we search for the “name”, “hello-service”, this is wrapped into a JsonObject and the result (if successful will contain a Record which matches the search criteria. Using HttpClient we can now simply get the reference to this service and interact with it without ever knowing it’s IP address or port.
ServiceDiscovery discovery = ServiceDiscovery.create(v); discovery.getRecord( new JsonObject().put("name", "hello-service"), found -> { if(found.succeeded()) { Record match = found.result(); ServiceReference reference = discovery.getReference(match); HttpClient client = reference.get(); client.getNow("/hello", response -> response.bodyHandler( body -> System.out.println(body.toString()))); } });