Sunday, 20 November 2016

Create new microservices within a minute!!

What are microservices?

Microservice architecture, is a distinctive method of developing software systems that has grown in popularity in recent years.  For many developers it has become a preferred way of creating enterprise applications.  Thanks to its scalability, this architectural method is considered particularly ideal when you have to enable support for a range of platforms and devices—spanning web, mobile, Internet of Things, and wearables—or simply when you’re not sure what kind of devices you’ll need to support in an increasingly cloudy future. In short, the microservice architectural style is an approach to develop a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services, which may be written in different programming languages and use different data storage technologies.

Although, this blog lists a very small introduction related microservices, you can find many articles around Microservices over the internet. This blog aims to provide an insight of how to write/implement Microservices using open source technologies.

Tools used:

Java 8
Spring boot
Hibernate
Swagger
Spring AOP for diagnostic and performance logging

Spring boot: Spring Boot aims to make it easy to create Spring-powered, production-grade applications and services with minimum fuss. Spring boot  provides features like:
  • simplifies Spring dependencies, no more version collisions
  • can be run straight from a command line without an application container
  • build more with less code - no need for XML, not even web.xml, auto-configuration
  • useful tools for running in production, database initialization,  environment specific config files, collecting metrics

Swagger: The goal of Swagger is to define a standard, language-agnostic interface to REST APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. When properly defined via Swagger, a consumer can understand and interact with the remote service with a minimal amount of implementation logic. Swagger UI is a great tool for describing and visualizing RESTful web services. It generates a small webpage, which documents your API and allows you to make test queries using JavaScript. Click here to see a small demo.

Please refer the Blog for steps to integrate Swagger with Spring boot application.

Spring AOP: Used spring AOP features to implement loggings around each method call to trace propogation of the thread. This will help in identifying the problem in case of any failures during functional testing and in analysis on production. Also, implemented Performance logging which measures the time each REST API has taken to respond. This feature will not only help in plotting the response times for each REST API while benchmarking the application from performance perspective but also helps in analysing the performance bottlenecks.

The source code for a user microservice is available in Github


Features available in this microservice implementation:

  • Spring boot application with embedded container
    • No need to install anything, just Java JDK 8
    • Build with the apache maven
    • Embedded apache tomcat container
    • Spring beans validations
  • Rest driven 
    • Documented with Swagger
    • Swagger provides different APIs by default related to Health check of the service
    • Swagger lists down all the APIs present in the service with easy to test feature on a single web page
    • JSON by default but can expand with xml
  • Database
    • Integrated with Postgresql
    • Can be easily replaced with any other database
  • Logging
    • Enabled apache tomcat access logs
    • Integrated log4j implementation
    • Performance logging using Spring AOP
    • Diagnostic logging on each method using Spring AOP
    • Sanky diagrams can also be plotted using diagnostic loggings which will help in plotting the user journey's
  • Object conversion
    • Used Orika mapper for conversion of Entity object to/from DTO object
    • Plug and play implementation for new object converter
  • Create new Microservice
    • Provided a shell script and configuration file to replicate a new micro service
    • It takes a few seconds to create a new microservice just by changing the config
    • Has basic CRUD operations related to entities such as Create, Update, Get and Delete
    • Designed microservice in such a way that we can easily add new APIs to this microservice
  • DevOps
    • Swagger provides the default health check APIs for the service. These APIs can be used to integrate with service monitoring tool such as OMD/Nagios.
    • Log files are generated in a standard format which can be pulled by Splunk or ELK for analysis and service monitoring.

Steps to create a new Microservice and run the spring boot application

  1. Checkout the source code from Github
  2. Change configurations in microservices-spring-boot/microservice-config.properties according to your requirements and do not forget to edit database configurations.



  3. Make sure you have the write permissions on target.directory.path, log.directory and above source code checkout directory.
  4. Run the command "sh microservice-create.sh" on windows or "./microservice-create.sh" on linux installation to create a new micro service.
  5. Design entity and domain classes according to your requirements and business logic.
  6. Verify the new microservice is created according to your config in your mentioned directory.
  7. This will create a .properties file at the root of the project directory which will help to keep environment related changing properties out of deployables (such as database connection, SMTP connection, other services URLs properties)
  8. Console ouptut should look somewhat like




  9. Check if maven is installed on your computer using mvn -v. If not installed please follow the instructions to install apache maven on your computer.
  10.  cd to newly created directory e.g. D:/Test/user-assignement-service and execute mvn install
  11. Execute the spring boot application using java -jar D:/Test/user-assignment-service/target/userAssignmentService.jar --spring.config.location=file:/D:/Test/user-assignment-service/user.assignment.service.properties

  12. Verify the new microservice using swagger with URL: http://localhost:10030/swagger-ui.html 

  13. Along with the defined APIs, swagger also provides predefined APIs to check health status, environment details, etc.


  14. Verify whether log files are getting created in the configured log.directory along with tomcat-access logs.
  15. Important Note: Please design the entity and DTO class according to your requirements. In the example I have only taken 2 fields into consideration for implementation. e.g. UserAssignment and UserAssignmentDTO has only 2 fields namely userId & userName which can be replaced according to your table structure, entity design and domain design. As mentioned above, please note there are 2 TODOs which needs to be implemented in the entity and DTO class "//TODO: Design this class according to your requirements."
  16. Add the required APIs as needed to this microsevice for your implementation.

No comments:

Post a Comment