Lagom: Let’s Create Micro Services Application


In previous post we introduced you all with our new hero Micro Services but now let’s create our first application. Light Bend Inc. created Lagom framework for creating reactive micro services. Its really easy to build micro services applications using Lagom framework because of reduced boiler plate task and code.

Architecture overview of Lagom Application, our application is basically divided into 2 components

  1. application-api   (here we define entity objects and api endpoints)
  2. application-impl   (here we provide the implementation of our services)

Prerequisite

  • Knowledge of Java 6 and basics of Java 8 functional programming
  • Lagom framework for micro services, just have an overview
  • Any IDE supporting Java Development (We are using IntellijIdea)
  • Cassandra (Basic SQL queries, we are using embedded cassandra), Kafka is internally used by Lagom Framework for message posting.
  • Lombok just basic idea, using for automated creation of immutable objects (Just to make code look cleaner and concise)
  • Junit Testing Framework

Architecture Overview

Lagom application works in read and write segregated mode, read side operation which only reads from persistent storage system and doesn’t perform any write operation similarly write side operations makes changes into persistent storage (here we are using embedded cassandra provided by lagom framework)

  1. We have to create service interface first which contains entity objects and abstract methods which are service endpoints to be exposed to user and their mapping
  2. Next we have to create service implementation corresponding to abstract methods in service-api. This is the part where we have to focus as we have to define our services here and need to get our hands dirty with code

Below is a snapshot of Lagom project, this is how project structure looks like.

image

Book.java is Entity class here we are declaring out entity which is to be persisted into DB(we can use any relational database but we are using embedded cassandra), you must be curious as we have not defined getter-setter methods but look there is annotation @Builder lombok framework will create getter-setter on the fly.

BookService.java extends Service interface of Lagom framework, here we provide abstract methods for each service. ServiceCall interface is there which provides implementation for how service is implemented and invoked by Lagom Framework which receives request and response type parameter. Inside descriptor we specify mapping of Call identifiers with corresponding method to be invoked, here we are using restCall method for identifying service call.

There are three types of Call identifiers

  1. Named Call : In this we directly provide same name or any alternative name corresponding to service method
  2. Path Call : As name suggests, here we can specify uri path of the resource mapped with service method.
  3. Rest Call : In this we provide url pattern to be mapped with service method with http methods (GET, POST, PUT, DELETE etc)

Here goes the implementation of BookServiceImpl.java where we provide implementation of abstract methods in service api. Lagom framework create and provide instance of cassandra. We create persistent entity registry and cassandra session which allows us to read and persist the data into cassandra.

Testing Lagom Micro Services

For testing we are using JUnit framework and lagom-javadsl-integration-client for providing mock client for hitting the rest end points. We are creating mock data by calling @Before Junit annotation which fills mock data in cassandra, calling rest end points then verifying data and, at the end  we are calling @After  Junit annotation for deleting the mock data from cassandra.

Github url of the application Lagom Application

Feel free to comment and post queries, Let me know in a comment if you guys want any topic/topics to be covered.


KNOLDUS-advt-sticker

Advertisements
This entry was posted in Scala and tagged , . Bookmark the permalink.

3 Responses to Lagom: Let’s Create Micro Services Application

  1. Chris Hodapp says:

    Gah, this is _almost_ RESTful, if only you used URIs properly: rather than e.g. “/api/get-book/:isbn”, your paths should just be e.g. “/api/books/:isbn”. Your URIs should identify the resource you are operating on, not the operation!

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