Lagom: Consuming a service part-2


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.

<plugin>
    <groupId>com.lightbend.lagom</groupId>
    <artifactId>lagom-maven-plugin</artifactId>
    <version>${lagom.version}</version>
    <configuration>
        <externalProjects>
            <externalProject>
                <artifact>
                    <groupId>com.example</groupId>
                    <artifactId>hello-impl</artifactId>
                    <version>1.2.3</version>
                </artifact>
            </externalProject>
        </externalProjects>
    </configuration>
</plugin>

 

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.

<dependency>
    <groupId>com.example</groupId>
    <artifactId>hello-api</artifactId>
    <version>1.2.3</version>
</dependency>

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.

@Override
protected void configure() 
    bindClient(HelloService.class);
    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;

    @Inject
    UnmanagedExampleServiceImpl(HelloService helloService) {
        this.helloService = helloService;
    }

    @Override
    public ServiceCall showDataFromUnmanagedService() {
        return request -> {
            String responseString;
            Data data = null;
            ObjectMapper jsonMapper = new ObjectMapper();
            try {
                LinkedHashMap response = helloService.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 (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.

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

Pasted image at 2017_11_27 04_17 PM

Advertisements

About anmolmehta

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

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s