Lagom is a new open source framework for architecting microservices. Lagom helps you build microservices as systems — Reactive systems, to be precise — so that your microservices are elastic, resilient and scalable from within.
4. MicroServices
Benefits
● Separate development and deployment. One service
can be developed, built and tested without affecting other
services or the entire application.
● Scaling and resilience. In a microservices architecture, you
can scale only those services that need it, rather than the whole
application.
● Independent tech stacks. Because each component is
separate from all other components, services can use their own
technology stacks without having to alter the way the other
services or the entire application are built.
7. Lagom - The Reactive
Microservices Framework
Lagom is a new open source framework for architecting microservices. Lagom helps
you build microservices as systems — Reactive systems, to be precise — so that your
microservices are elastic, resilient and scalable from within.
Lagom is built on top of Play, and its goal is more specific than Play. In particular, it
adds an API for describing/implementing services and a CQRS-based persistence API
on top of the APIs Play already has.
8. Lagom - The Reactive
Microservices Framework
Design Philosophy
Lagom’s design rests on the following principles:
●
Message-Driven and Asynchronous: Built upon Akka Stream for asynchronous streaming
and the JDK8 CompletionStage API.
● Distributed persistence: Lagom favours distributed persistence patterns using Event
Sourcing with Command Query Responsibility Segregation (CQRS).
● Developer productivity: Starting all microservices with a single command, code hot
reloading and expressive service interface declarations are some examples of Lagom’s high
emphasis on developer productivity.
9. Lagom - The Reactive
Microservices Framework
Build Blocks
The Lagom framework acts as an abstraction layer upon several Lightbend frameworks
and consists of the following core technologies and frameworks:
● Scala
● Java
● Play Framework
● Akka and Akka Persistence
● SBT
● Cassandra
● Guice
● ConductR
10. Lagom - The Reactive
Microservices Framework
GETTING STARTED WITH LAGOM
Prerequisites for using Lagom with Maven include:
● JDK 8
● Maven 3.3 or higher
Creating a new Lagom application is as simple as using the following command:
mvn archetype:generate -DarchetypeGroupId=com.lightbend.lagom -DarchetypeArtifactId=maven-
archetype-lagom-java -DarchetypeVersion=1.3.4
Maven prompts you for:
groupId — typically something like com.example.
artifactId — becomes the top-level folder name, for example, my-first-project.
version — the version for your project, press Enter to accept the default.
package — defaults to the same value as the groupId
11. Lagom - The Reactive
Microservices Framework
Now execute command mvn lagom:runAll
This command starts a Cassandra server, service locator and service gateway. Each of our
microservices is started in parallel while also registering them in the service locator. Note that the
ports are assigned to each microservice by an algorithm and are consistent even on different
machines. The possibility to assign a specific port is available though.
Similar to Play Framework, Lagom also supports code hot reloading allowing you to make changes
in the code and immediately seeing these changes live without having to restart anything. A
feature we’re very fond of. In general, a restart is only required when adding a new microservice
API and implementation module in the project.
12. Lagom - The Reactive
Microservices Framework
ANATOMY OF A LAGOM PROJECT
helloworld-api → Microservice API submodule
└ src/main/java → Java source code interfaces with model objects
helloworld-impl → Microservice implementation submodule
└ logs → Logs of the microservice
└ src/main/java → Java source code implementation of the API submodule
└ src/main/resources → Contains the microservice application config
└ src/test/java → Java source code unit tests
logs → Logs of the Lagom system
13. Lagom - The Reactive
Microservices Framework
Example of a MicroService
In order to write a new microservice you create a new API and implementation project. In the API project you define
the interface of your microservice:
public interface HelloService extends Service {
ServiceCall<String, NotUsed, String> hello();
ServiceCall<String, GreetingMessage, String> useGreeting();
@Override
default Descriptor descriptor() {
return named("helloservice").with(
restCall(Method.GET, "/api/hello/:id", hello()),
restCall(Method.POST, "/api/hello/:id", useGreeting())
).withAutoAcl(true);
}
}
14. Lagom - The Reactive
Microservices Framework
In the implementation submodule we implement our API’s interface.
public class HelloServiceImpl implements HelloService {
@Override
public ServiceCall<String, NotUsed, String> hello() {
return (id, request) -> {
CompletableFuture.completedFuture("Hello, " + id);
};
}
@Override
public ServiceCall<String, GreetingMessage, String> useGreeting() {
return (id, request) -> {
CompletableFuture.completedFuture(request.message + id);
};
}
}
15. Lagom - The Reactive
Microservices Framework
public class HelloServiceModule extends AbstractModule implements
ServiceGuiceSupport {
@Override
protected void configure() {
bindServices(serviceBinding(HelloService.class, HelloServiceImpl.class));
}
}
16. CQRS AND ES
Event sourcing and CQRS (Command Query Responsibility Segregation) are fundamental
concepts behind Lagom’s support for services that store information.
Why Event Sourcing?
If a service stores information, a core principle is that each service should own its data and it
is only the service itself that should have direct access to the database. Other services must
use the Service API to interact with the data. There must be no sharing of databases across
different services since that would result in a too tight coupling between the services.
To achieve this, Lagom’s persistence module advocates the use of event sourcing and
CQRS (Command Query Responsibility Segregation).
17. Testing in Lagom
@BeforeClass
public static void setUp() {
server = ServiceTest.startServer(ServiceTest.defaultSetup());
}
@AfterClass
public static void tearDown() {
if (server != null) {
server.stop();
server = null;
}
}
18. Testing in Lagom
@Test
public void shouldRespondHello() throws Exception {
// given
HelloService service = server.client(HelloService.class);
// when
String hello = service.hello().invoke("Yannick",
NotUsed.getInstance()).toCompletableFuture().get(5, SECONDS);
// then
assertEquals("Hello, Yannick", hello);
}