I’ve already discussed the basic structure of the Lagom in my previous blog and also I’ve shared a full-fledged small Lagom service in that.
So, in this blog, I’m telling how you can consume a separate micro-service that is running separately. Or in other words, simply want to call/invoke another service’s descriptor’s method in your service. I am also describing in this how you can run two or more different micro-services in the local machine only using Lagom.
The first necessary step to consume another service is to attach its descriptor’s class in your project because the service descriptor includes everything Lagom needs to know about how to invoke a service. Lagom can implement service descriptor interfaces for you. So after attaching the descriptor you only need to create the implementation of the service client in the loader class. For that, you can use an inbuilt function implement
that is provided by Lagom.
abstract class UserServiceApplication(context: LagomApplicationContext)
extends LagomApplication(context)
with UserComponents {
lazy val userService = serviceClient.implement[UserServiceProvider]
override lazy val lagomServer: LagomServer = serverFor[UserService](wire[UserServiceImpl])
}
Note: In the above code UserComponents is a trait that extends other components like AhcWSComponents
.
Now the most interesting part after doing all of this is that you can simply use another service’s method by using invoke method like shown in the code below.
class UserServiceImpl(userServiceProvider: UserServiceProvider)(implicit ec: ExecutionContext)
extends UserService {
override def sayHello: ServiceCall[NotUsed, String] = ServiceCall{
_ =>
Future.successful(
"Hello from current micro-service(Consumer)"
)
}
override def printUserRemark: ServiceCall[User, String] = {
request =>
val newUser = NewUser(request.credScore, request.name)
val result = userServiceProvider.addRemark().invoke(newUser)
result.map(user => user.name)
}
}
As you can see in the above code in the printUserRemark
method, I simply invoke the addRemark
method of the userServiceProvider
this addRemark
method will add a remark about the user which I provide to it and the most important thing is that the implementation of this method is in the different micro-service that running separately from this service.
So, till now I’ve explained how you can consume a service in Lagom using Scala. You can download a full-fledged example of the above here and can run it on your local machine.
How to run two or more separate Lagom micro-service on a same local machine
Now let’s suppose we have two Lagom services the first one is LagomService1 and another one is LagomService2 and we consuming LagomService2 in the LagomService1.
So, open both projects in separate windows. I’m using Intellij IDE so the upcoming steps are based on this
Steps are:-
1. Open the terminal and run the sbt
command.

2. Now sbt server started so, go in the impl module where the implementation and loader class is present using project userService2-impl

3. Now you are in the impl module so, start here ServiceLocator by using the command lagomServiceLocatorStart

As you can see the ServiceLocator is running.
Note: If we didn’t start ServiceLocator separately and just hit the runAll
command to start the service it will only start or run only one service from which you hit the runAll
command and through exception when we going to start another service using runAll
but by running ServiceLocator separately we can run as many services as we want.
4. Now the ServiceLocator is running successfully. We know that ServiceLocator is the key by which services can be discovered and can communicate with each other. So, now, at last, we have to run our services. Now, here I’m running the LagomService2 first. You can run any service first after starting the ServiceLocator.


as you can see the service is running successfully now we have to run another service that is LagomService1 so to run it we again go in its terminal and start the sbt server then go to the impl module and then hit run
command.

Now, both the services are running on different ports and now I’m going to show you the result.

As shown in the above image. I’m accessing the URL of the service, which runs on port 57864. Now this will fetch data from another service, which runs on a different port, 51273.
Suggestion: for best understanding please download the projects from here so you can run them on your local machine.