Skip to content

LiveSafe/lagom-service-locator-consul

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lagom Service Locator for Consul

Forked from jboner/lagom-service-locator-consul, this project implements the Lagom ServiceLocator interface for Consul and provides a Consul-based service registry for registering and unregistering service from within the services.

Register service locator in Lagom

To use it, simply mix in ConsulServiceLocatorComponents to your Lagom application. This will override serviceLocator with an instance of ConsulServiceLocator.

Routing to service instances

The ConsulServiceLocator has support for three simple routing policies:

  • first: picks the first service instance in a sorted list—sorted by IP-address and port
  • random: picks a random service instance
  • round-robin: performs a round-robin routing between the currently available service instances

Configuration

An application.conf file needs to be created in src/main/resources with the following contents:

lagom {
  discovery {
    consul {
      agent-hostname = "127.0.0.1"   # hostname or IP-address for the Consul agent
      agent-port     = 8500          # port for the Consul agent
      uri-scheme     = "http"        # for example: http or https
      routing-policy = "round-robin" # valid routing policies: first, random, round-robin
    }
  }
}

Register services in Consul

The second step is to register each of your services in Consul. This can be done using the Consul Java API library or through Consul's HTTP API. Here is some example code of how to use it in a service:

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.agent.model.NewService;

/**
 * This shows a very simplified method of registering an instance with the service discovery. Each individual
 * instance in your distributed set of applications would create an instance of something similar to ExampleServer,
 * start it when the application comes up and close it when the application shuts down.
 */
public class ExampleService {
    final ConsulClient client;
    final NewService service;

    public ExampleService(
            String serviceName,
            String serviceId,
            String serviceAddress,
            int servicePort,
            String consulHostname) throws Exception {
        service = new NewService();
        service.setId(serviceId);
        service.setName(serviceName);
        service.setPort(servicePort);
        service.setAddress(serviceAddress);

        client = new ConsulClient(consulHostname);
        client.agentServiceRegister(service);
    }

    public void stop() {
        client.agentServiceDeregister(service.getId());
    }

    public static void main(String[] args) throws Exception {
        String consulHostname= "localhost";
        String serviceName = "testService";
        String serviceId = "uniqueId";
        String serviceAddress = "localhost";
        int servicePort = 9000;

        ExampleService service = new ExampleService(
                serviceName, serviceId, serviceAddress, servicePort, consulHostname);

        service.stop();
    }
}

How to run the tests

You need a Consul agent running on your local machine (on its default port) in order to run the tests.

If you are on Mac then you can install Consul through Homebrew using brew install consul or using docker docker run -p8500:8500 consul. Once it is installed you can start up an agent in dev mode by invoking consul agent -dev -data-dir ~/tmp which will make an agent available on 127.0.0.1:8500.

Once Consul is running you can run the tests by invoking sbt test.

About

Lagom Service Locator using Consul

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Scala 100.0%