Let’s Consume a Micro-service

Micro-services architecture is being widely adopted and guess what, Lagom is the most efficient way to achieve it.  While creating micro-services, we usually need to interact with other micro-services and consume their data. So the idea behind this blog is to guide you through the steps needed to integrate an unmanaged service.

Apparently, Integrating an external API in Lagom is challenging at first but it is actually very straightforward. Following are the steps:

Step 1: Register the API as unmanaged service in your managed service’s impl pom.xml


This will ensure that the service locater can find the external API to be used.

Step 2: Create an interface for the unmanaged service that could be used to communicate with that service.

public interface ServiceUService extends Service {
    ServiceCall getResultFromUnManagedService();

    default Descriptor descriptor() {
        return named("serviceU").withCalls(
                Service.restCall(GET, "/key/value/one/two",

This ensures that when our managed service calls “getResultFromUnManagedService” method then the service locater redirects the call to the unmanaged service’s API with the same route as we provide in this interface.

Now the question arises is, how would Lagom know that it has to provide an implementation for the unmanaged service’s api?

And the answer is, “Binding the service client ”

Step 3: Provide a binding for the client i.e, the unmanaged service.

public class UnmanagedExampleModule extends AbstractModule implements ServiceGuiceSupport {
    protected void configure () {
        bindService(UnmanagedExampleService.class, UnmanagedExampleServiceImpl.class);

Step 4: Now it’s time to use the service. And to do so, we will have to inject the client into our component using @Inject annotation.

public class UnmanagedExampleServiceImpl implements UnmanagedExampleService {

    private final ServiceUService serviceUService;

    UnmanagedExampleServiceImpl(ServiceUService serviceUService) {
        this.serviceUService = serviceUService;

    public ServiceCall showDataFromUnmanagedService() {
        return request -> {
            String responseString;
            Data data = null;
            ObjectMapper jsonMapper = new ObjectMapper();
            try {
                LinkedHashMap response = serviceUService.getResultFromUnManagedService().invoke().toCompletableFuture
                responseString = jsonMapper.writeValueAsString(response);
                data = jsonMapper.readValue(responseString, Data.class);
            } catch (Exception ex) {
            return CompletableFuture.completedFuture(data);

So this was an example to show the flow that has to be followed when an unmanaged service has to be integrated in a Lagom project. Let’s have a quick recap: First, register the external API in pom.xml. Then, create the interface to communicate with the external API. Then, bind the interface using bindClient. And in the end, inject the client in your component to call it’s methods and consume the response.
Hope this attempt would be helpful to you. For more doubts and examples regarding Lagom, feel free to go through our blogs, because we at Knoldus believe in gaining knowledge and growing our skills together.

References: https://www.lagomframework.com/documentation/


About anmolmehta

Technology Enthusiast
This entry was posted in Java, Microservices and tagged , , , , , . Bookmark the permalink.

2 Responses to Let’s Consume a Micro-service

  1. anmolmehta says:

    Reblogged this on Site Title.

  2. Pingback: Lagom: Consuming a service part-2 | Knoldus

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s