Skip to content

Resolver

Bjorn Borud edited this page Dec 10, 2011 · 1 revision

The Resolver

In order to find a given service by its coordinate you need to use the resolver API. This is the API clients use to resolve the endpoints they want to talk to.

In addition to resolving plain coordinates, the resolver API offers some additional syntax for resolving specific endpoints on an instance or across all of the instances of a given service.

Let's start with the two most common forms of resolver expressions. First let's look at how you find a particular endpoint of a service:

Resolving specific instance

The simplest form is just resolving what host a coordinate is assigned to at the moment:

// Little more than a glorified DNS lookup
Endpoint endpoint = resolver.resolve("3.myservice.borud.xx");
System.out.println("host = " + endpoint.getHost());

However, you usually want to resolve a specific service that listens to some port number. You can do this by querying the coordinate for named endpoints:

// Resolve the "rpcport" endpoint on "3.myservice.borud.xx"
Endpoint endpoint = resolver.resolve("rpcport.3.myservice.borud.xx");
System.out.println("host = " + endpoint.getHost());
System.out.println("port = " + endpoint.getPort());

If the service 3.myservice.borud.xx has published an endpoint named rpcport through Cloudname the above code will return an Endpoint instance containing the host, port and some other information about the endpoint.

Resolving endpoint on several instances

Often you will want to resolve multiple endpoints. For instance you may want to look up all the endpoints named "restport" on a cluster of service instances.

// Resolve the "rpcport" endpoint on "3.myservice.borud.xx"
List<Endpoint> endpoints = resolver.resolveAll("rpcport.all.myservice.borud.xx");
for (Endpoint endpoint : endpoints) {
    System.out.println("host = " + endpoint.getCoordinate());
    System.out.println("host = " + endpoint.getHost());
    System.out.println("port = " + endpoint.getPort());
}

You may notice in the code above that we have now replaced the instance number with a string. In resolver expressions we call this the "resolution strategy". The above example we have a strategy called "all", which will result in the resolver returning a list of Endpoint instances.

You can also use this without naming an endpoint. In which case the resulting list of Endpoint instances will not contain a port number.

// Resolve all running instances of "myservice.borud.xx"
List<Endpoint> endpoints = resolver.resolveAll("all.myservice.borud.xx");
for (Endpoint endpoint : endpoints) {
    System.out.println("host = " + endpoint.getCoordinate());
    System.out.println("host = " + endpoint.getHost());
}

Clone this wiki locally