So, you want to integrate an unmanaged service in Lagom using Scala.
Let’s first understand what an unmanaged service first and then we will see How we can integrate an unmanaged service with our lagom service.
What is an unmanaged service?
An unmanaged service is basically an exernal service which exposes some data over HTTP. Any external REST API which will be used in Lagom based service is an unmanaged service.
Any external REST API which will be used in Lagom based service is an unmanaged service.
For example : Integration of GoogleMap, Openweather and any other for that matter.
Communicating with external(unmanaged) services in Lagom
- It is possible to enable communication between the Lagom services defined in your build, and an unbounded number of external services (which could either be running locally or on a different machine).
- To do this, we need to register the API as external service in the Service Locator, which allows services to discover and communicate with each other.
- The first thing you will have to do is to register each external service in the Service Locator. Assume we want to register an external service named weather that is running on http://localhost:3333, here is what we would add to the build.
lagomUnmanagedServices in ThisBuild := Map(“weather” -> “http://localhost:3333”)
where http://localhost is the hostname and 3333 is port number - The above ensures that the Service Locator knows about the weather service. Then, if you need a Lagom service to communicate with it, simply @Inject the ServiceLocator and use it to either locate the weather service’s URI, or perform some arbitrary work with it.
Now Let’s see How we can consume data from the unmanaged service.
Consume data from unmanaged service
Step 1. Create a new ‘trait’ for the external service which will have exact same route as external service has.
Step 2. Injecting external service into our service
Step 3. Define external service’s host and port in build.sbt
Step 4. Bind the external service in ServiceModule.
Data modelling for unmanaged service
Now we know that the external service has exposed some APIs and we are consuming the data from the API. So we must know the type of the response. Now we will map the response with a Data model.
How do we do the data modelling or map the response with the case class?
Alright, That was it. We are done writing our service which will consume data from an external(unmanaged) service.
If you find any challenge, Do let me know in the comments.If you enjoyed this post, I’d be very grateful if you’d help it spread.Keep smiling, Keep coding!
Reblogged this on LearningPool.
Reblogged this on Coding, Unix & Other Hackeresque Things.
Thanks, I did it and it’s ok in local but after deployment to Kubernetes, it doesn’t work.
It has an error like this
external-service was not found by service locator
Is there a way to add headers to these requests to external services?
Thanks!
@deepak this post will be more helpful when you give a link to running code of this kind.