Let’s Consume a Micro-service

Knoldus Inc.
Knoldus - Technical Insights
2 min readJun 21, 2017

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

<plugin>-->
<!--<groupId>com.lightbend.lagom</groupId>-->
<!--<artifactId>lagom-maven-plugin</artifactId>-->
<!--<configuration>-->
<!--<lagomService>true</lagomService>-->
<!--<unmanagedServices>-->
<!--<serviceU>http://echo.jsontest.com:80</serviceU>-->
<!--</unmanagedServices>-->
<!--</configuration>-->
<!--</plugin>

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();
@Override
default Descriptor descriptor() {
return named("serviceU").withCalls(
Service.restCall(GET, "/key/value/one/two",
this::getResultFromUnManagedService)
).withAutoAcl(true);
}
}
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 {
@Override
protected void configure () {
bindService(UnmanagedExampleService.class, UnmanagedExampleServiceImpl.class);
bindClient(ServiceUService.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;@Inject
UnmanagedExampleServiceImpl(ServiceUService serviceUService) {
this.serviceUService = serviceUService;
}
@Override
public ServiceCall showDataFromUnmanagedService() {
return request -> {
String responseString;
Data data = null;
ObjectMapper jsonMapper = new ObjectMapper();
try {
LinkedHashMap response = serviceUService.getResultFromUnManagedService().invoke().toCompletableFuture
().get();

responseString = jsonMapper.writeValueAsString(response);
data = jsonMapper.readValue(responseString, Data.class);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
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/
KNOLDUS-advt-sticker

--

--

Knoldus Inc.
Knoldus - Technical Insights

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com