Understanding Hello World

§Understanding Hello World

After creating and running Hello World from the command line, you no doubt appreciate what Lagom framework did for you. There was no need to determine what infrastructure you might need and then install and configure it. The template removed the necessity to set up a project or build structure. And, as you create services of your own, Lagom detects changes and performs a hot reload! Lagom allows you to concentrate on satisfying your business needs.

The separation of concerns illustrated in Hello World and an introduction to service descriptors and the registry will help you as you start developing your own microservices:

§Service interface

The service interface belongs in the api project. For instance, the service interface for the hello service resides in the hello-api project (look for the HelloService.scala source file).

import akka.{Done, NotUsed}
import com.lightbend.lagom.scaladsl.api._
import play.api.libs.json._

trait HelloService extends Service {

  def hello(id: String): ServiceCall[NotUsed, String]

  def useGreeting(id: String): ServiceCall[GreetingMessage, Done]

  override final def descriptor = {
    import Service._
    named("hello").withCalls(
      pathCall("/api/hello/:id", hello _),
      pathCall("/api/hello/:id", useGreeting _)
    ).withAutoAcl(true)
  }
}

case class GreetingMessage(message: String)

object GreetingMessage {
  implicit val format: Format[GreetingMessage] = Json.format[GreetingMessage]
}

Note that:

  • The service interface inherits from Service and provides an implementation of Service.descriptor method.

  • The implementation of Service.descriptor returns a Descriptor. The HelloService descriptor defines the service name and the REST endpoints it offers. For each endpoint, declare an abstract method in the service interface as illustrated in the HelloService.hello method. For more information, see Service Descriptors.

§Service implementation

The related impl project, hello-impl provides implementation for the service abstract methods. For instance, the HelloServiceImpl.scala source file contains the service implementation of the HelloService.hello method for the hello service. The PersistentEntityRegistry supports data persistence using Event Sourcing and CQRS.

import com.lightbend.lagom.scaladsl.api.ServiceCall
import com.lightbend.lagom.scaladsl.persistence.PersistentEntityRegistry

class HelloServiceImpl(persistentEntityRegistry: PersistentEntityRegistry) extends HelloService {

  override def hello(id: String) = ServiceCall { _ =>
    val ref = persistentEntityRegistry.refFor[HelloEntity](id)
    ref.ask(Hello(id, None))
  }

  override def useGreeting(id: String) = ServiceCall { request =>
    val ref = persistentEntityRegistry.refFor[HelloEntity](id)
    ref.ask(UseGreetingMessage(request.message))
  }
}

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.