Getting started with Microservices

 

In the earlier blog we discussed Microservices Anatomy and talked about

  • Why we need microservices
  • What is microservices
  • Details & advantages
  • Known uses of Microservices

However, it was not enough to get started and start using the Microservices in a production environment. In this article, we will be taking about the following to enable you to get started with the Microservices:

  • What to expect from Microservices?
  • Technology Comparison
  • Spring Boot Features
  • Microservice Communication
  • Testing
  • Sample application

Before we start

We need the following software on our system to implement a microservices with the help of IDE.

Getting ready

  • Download and install Java in your system. Follow the steps as per your OS.
  • Setup maven as per the steps mentioned here.
  • Install Eclipse using the steps as per the eclipse version and you OS.

What to expect from Microservices?

If you need a software/service with following features then Microservices may be a good choice for you

  • Loosely coupled
  • REST Support
  • Transaction Management
  • Log Management
  • Auditing
  • Scalable
  • Security
  • Services Statistics

Technology Comparison

To get started with Microservices implementation, we do have multiple technology options available with us. In this article, I have compared the top five technologies that I have come across and I am sure there are more options, which may help in very specific cases. You can choose the best one among these or any other new technology, which will fulfill your needs.

 

We need a software/service with minimum REST and Security features support.

In this article, I will be using Spring Boot for our sample applications and their demo.

Spring Boot

Spring Boot has the number of inbuilt features, which we need to use in any enterprise application. By making use of Sprint Boot, we get those features and related configuration off-the-shelf.

In this section, I am summarizing few important features that we often use. For more details, you can refer to the Sprint Boot project page.

Logging

Spring Boot provides us the various way to achieve logging

  • Using Aspect
  • Using logback
  • Annotation based logging ( When an exception occurs )

Monitoring

Spring boot comes with its own monitoring tool called “Spring Admin Console”

We can monitor various aspects like:

  • Microservice basic details like version, info, status
  • Application health
  • Memory
  • JVM
  • Garbage Collection
  • Metrics

We can use this as “Performance Management” tool also. Further, we can also changelog level without restarting the application, which is a great help in debugging any prod issue.

Sample UI: spring-boot-admin

Auditing

Auditing would be achieved using AspectJ. We can have the information like

  • Who accessed the URL and from where
  • How many requests have been successfully served to whom
  • How many requests failed
  • Which services – How many times – By Whom

Transaction Management

Spring gives us various ways to manage the transactions, in which the following two are very popular:

  • Using Aspects
  • Annotation Based

Microservice Communication

Microservices communication can be divided into two parts.

  • Communication between UI & microservices
  • Intercommunication between microservices

Nowadays, most of the modern browsers understand JSON data and they render the JSON without/with minimal change. Microservices can also return the JSON response along with regular response types, such as XML, HTML & etc…

If we have the design of inter microservice communication, then we can use the binary encoded format as a response. With the help of binary encoded format, we can improve the application performance. The application will encode/decode less sized data and the data will travel over a network.

Testing

Unit testing: We can test the implemented microservices using JUnit.

Integration testing: We can use JUnit also for integration testing.

Following UI tools can be used for testing

  • Postman
  • REST Client for Mozilla firefox
  • Advanced REST client for Google Chrome

Sample application

Here I am going to discuss a simple application (Patient) which is a part of Hospital Management application, which is implemented using Spring-Boot framework.

In this application, we can

  • Create a new patient record
  • View existing record
  • Update existing record
  • Enable/Disable the existing record

Create a simple maven project, it should have spring-boot-starter-parent as its parent as shown in the below screenshot. Also, provide the required dependencies for our database transactions and building the artifact.

 

Application directory structure will be as shown below.

 

Main class should be annotated with the EnableAutoConfiguration, ComponentScan & SpringBootApplication to have the required setup for Microservices.

application.properties details are provided with the details

logback.xml file configurations are shown below with the details. The log format has more data and it can be used in debugging the logs easily using ELK combination.

Create the jar file to execute the application. Run the pom.xml file as “Maven build” by providing goals “clean install” as shown below.

Start the application using com.wt.PatientApplication from eclipse

or

java -jar patient-1.0.jar from command prompt/terminal and you can confirm the application started by looking at the logs as below.

We have below APIs available in our application. Let’s test them using Advanced Rest Client

  • http://localhost:8080/

  • http://localhost:8080/patient/create

  • http://localhost:8080/patient/{id}

  • http://localhost:8080/patient/update

  • http://localhost:8080/patient/disable

  • http://localhost:8080/patient/enable

JUnit test execution screenshot

Summary

In this article, we discussed the different technologies used to build Microservices. We compared them to enable you to chose the one that will work best for you. Also, we implemented a sample application using “Spring Boot”, covered microservices communication and unit testing using JUnit & Advanced rest client.

I hope you found this article helpful. If you have a monolithic application, which you would like to be redesigned or you are looking for highly scalable solution based on Microservices architecture then we will be happy to assist!

References

Related Posts

Leave a comment

Privacy Preferences
When you visit our website, it may store information through your browser from specific services, usually in form of cookies. Here you can change your privacy preferences. Please note that blocking some types of cookies may impact your experience on our website and the services we offer.