So here’s the situation: We are using Lagom framework to develop our micro-services and we need to consume data from other services. What should we do?

Well, it’s not going to be a problem for us. Lagom provides very easy way to consume a service. The very first way to consume a service is to use the unmanagedServices parameter provided in Lagom plugin. To understand the use of unmanagedServices, please refer to the following link.

Now wait a second, In the above blog, we have created an interface for the unmanaged service that could be used to communicate with the service from which we are trying to consume data.
But if we are consuming a service that is already implemented in Lagom way, wouldn’t that be a boilerplate for us? Of Course it is. So what should we do now?
Here’s the solution from Lagom:
Prerequisite: The service from which you want to consume the data should be published via a build.
Now let’s consume the service: The following 4 steps will help you if you need to consume from another Lagom micro-service.

Step 1: The lagom-maven-plugin offers a configuration item called externalProjects that can be configured on the root project to import external projects into a Maven build.



Step 2: The dependency of the api module of the service we need to consume is to be added in the impl of the service we are implementing.


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.

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 HelloService helloService;

    UnmanagedExampleServiceImpl(HelloService helloService) {
        this.helloService = helloService;

    public ServiceCall showDataFromUnmanagedService() {
        return request -> {
            String responseString;
            Data data = null;
            ObjectMapper jsonMapper = new ObjectMapper();
            try {
                LinkedHashMap response = helloService.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 (already implemented using Lagom framework) has to be integrated in a Lagom project. Let’s have a quick recap: First, register the external project in pom.xml. Then, add the dependency of the api module of the service to be consumed. 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.


Pasted image at 2017_11_27 04_17 PM

One comment

Leave a Reply

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

You are commenting using your 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