Let’s Consume a Micro-service

Table of contents
Reading Time: 2 minutes

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/


Written by 

Anmol Mehta is a Software Consultant having experience of more than 1.5 years. A keen programmer who has experience in Scala and Java. He is recognized as a dedicated and determined team player who enjoys working on new technologies. He is a professional and a technology enthusiast. He believes in the approach that “teamwork makes the dream work”. He is a quick and always-on learner who ensures better quality in every increment of his work. He believes in abiding standard coding practices. He always looks after that the team is working in sync with each other both at the technical and managerial level.

3 thoughts on “Let’s Consume a Micro-service3 min read

  1. Hi Anmol, do have the working repo, can you please share the repo for this . I am still struggling to make an external rest call from Lagom.

Comments are closed.