fbpx
Select Page

MicroServices

The only constant thing in Software Field is ‘Change’. A software developer should be ready with the upcoming technologies and updates. Everyone can feel the churn, as one has to be ready for a sudden change in technology. Some technologies cause a revolution. Churn is adopting new technologies, that too in less span of time. As part of the same versatility, many organizations are shifting to microservices now.

Microservices are small individual units of the project which you can build separately. Instead of looking towards a project as one single huge task (monolithic project), we can divide the project into the smaller intelligent unit. Intelligent means, which we can build separately as a small project. Basically dividing a big project into small units, developing these units separately and later combining it as a project is the concept. These small units are nothing but microservices.

This article will introduce you to microservices architecture and how to build microservices with spring boot.

Before microservices, almost all web-based application built, as one single task i.e. one single artifact was generated. And only one artifact was deployed to the server. As a result of which, If you have any updates in the project, the entire application will be down for that particular time period. Which may lead to business loss. Those who are familiar with “irctc” train ticket booking, may have observed this situation couple of years back that, “irctc” announced a “Server will be  Down for XX hours on Date: XX” kind of notification. This kind of situations troubles users, as well as there, maybe a huge business loss.

To avoid this we can switch to microservices. As irctc have switched to microservices ans, now in last year Irctc had a drastic change in look, feel and operations but we have not observed a single server down notification form the irctc. Size and complexity of monolithic applications grew, so one small update in application also leads to rebuilding, retest and redeployment. Microservice is small, distributed, loosely coupled service, which decomposes the code in small well-defined pieces, reducing the traditional problem of complexity in large monolithic code.  Decomposing and unbundling of the application so they are completely independent of one another.

For Free, Demo classes Call: 8237077325

Registration Link: Click Here!

I will give an example of my company for better understandings. I am working with this company, where students are trained under 3 separate departments as  1. Development  2. Networking also has a separate department which looks after the placement of students from all the departments. If you imagine developing a monolithic CRM (Customer Relation Management) application for a company, one single update in any of the departments may lead to business loss of another department, as the entire application will be down.[Fig 1.]

Fig 1

Instead of creating one single monolithic application to manage everything, we can think about creating 4 separate microservices managing Development, HR, Networking and Placement department. If in case the Development department wants to add information of a new course and wants to update pages, only ‘development’ microservices will be down and other microservices will be working fine.  Which will avoid the business loss for another department? [Fig 2]

Spring Boot Web based Microservice

Fig 1. Show the diagrammatic representation of the monolithic app for the example discussed above.

Fig 2 Show the one micro-service for managing details of the student which belongs to the Development Department. We can have such Two more micro-services to manage students from the HR department and, Networking department.

Fig 3. Diagrammatically represents the difference between typical monolithic application and micro-services.

the diffrence between Monilithic Application and

For Free, Demo classes Call: 8237077325

Registration Link: Click Here!

Characteristics of Microservices architecture  :

  • Application logic is divided well defined small pieces, with some responsibilities and role which coordinate to deliver a solution.
  • Every component is deployed independently and has separate well defined set of roles.
  • Yes of course, one microservices can communicate with other with some standard principles like JSON (Java Sript Object Notation), HTTP .

 

MicroServices Communication

          Communication between microservices is challenging due to cost, coupling, latency, throughput and few more things. Microservices communication can be done in a synchronous or asynchronous way. We will be discussing ways of communication between microservices in next blog.

 Why To Go with Spring Boot For developing Microservices?

Spring is a de facto lightweight framework for developing web applications. Spring is a dependency injection framework, which divides the application into classes. The linking is creating the object of classes and passing data to the constructor of mutators(setters).  It is called a lightweight framework because it does not need server container dependency like EJB modules.  EJB modules are heavyweight modules as every entity bean has a dependency on session bean, so code becomes lengthy and tightly coupled. To overcome these features we got Spring. But, while working with spring we have to do XML configurations for dependency injection. Spring boot avoids these XML configurations and reduces the complexity of application even more.   Spring Boot makes easy and fast development of stand-alone applications. Spring boot needs very little spring configurations.

Key Features :

  • Spring boot helps to create fast and easy to deploy applications.
  • Spring boot provides the annotation support and provides many nonfunctional features which are common for large classes of project.
  • Spring boot does not require XML configurations.

Best Tricks of Spring :

  • Automatic Confutation – Like spring applications spring boot also provides auto-configuration with reduced complexity.
  • Dependencies adding at start –> At start we need to tell spring boot that what kind of application we need to create and spring boot will load the dependencies at starting of the project.
  • Command Line execution –> you can execute the project in the traditional way, so as to make you feel that spring boot is very simple and easy.
  • The Actuator –> Tells you about the spring boot application you are running. You can READ or WRITE. To work with Actuator you need to create a Spring MVC application. We can find out the application status with ‘/health’ endpoint, can display arbitrary info with ‘/info endpoint’

Developing A Microservices With Spring Boot

            We can develop a spring boot application with eclipse IDE.  We need to add spring boot plugins. You will get plugins for spring boot in Help–>Eclipse MarketPlace –>spring boot. Download Spring Tool-4 for Spring Boot.

Consider we are developing a microservices to manage students at Seven Mentor. So first we will have to create a class which will manage students data. Also, we need to create a controller which will be performing further operations on data which we are sending. So, we are creating a service where we will be sending data i.e. student information through POST. Here we will use postman to execute our service.

System Requirments For The Example

 

Name Version
1 Spring Boot Sprin Boot 2.1.6 RELEASE
2 Spring Spring Framework 5.1.8 RELEASE
3 Java Java 8
4 Build Tool Maven 3.3 + / Gradl 4.4+
5 Servlet Container Tomcat 9.0 / Jetty 9.4

 

For Free, Demo classes Call: 8237077325

Registration Link: Click Here!

// The Bean class to manage Students details, Where the student is from the Development Department.

package com.example.demo;

public class StudentAtSevenMentorDevelopmentDepartment {

private int studId;

private String studFirstName;

private String studLastNAme;

private int phoneNo;

private String course;

private int fees;

private String modeOfPayment;

public String getModeOfPayment() {

return modeOfPayment;

}

public void setModeOfPayment(String modeOfPayment) {

this.modeOfPayment = modeOfPayment;

}

public int getStudId() {

return studId;

}

public void setStudId(int studId) {

this.studId = studId;

}

public String getStudFirstName() {

return studFirstName;

}

public void setStudFirstName(String studFirstName) {

this.studFirstName = studFirstName;

}

public String getStudLastNAme() {

return studLastNAme;

}

public void setStudLastNAme(String studLastNAme) {

this.studLastNAme = studLastNAme;

}

public int getPhoneNo() {

return phoneNo;

}

public void setPhoneNo(int phoneNo) {

this.phoneNo = phoneNo;

}

public String getCourse() {

return course;

}

public void setCourse(String course) {

this.course = course;

}

public int getFees() {

return fees;

}

public void setFees(int fees) {

this.fees = fees;

}

 

}

Also we neet to create a controller class as follows  :

package com.example.demo;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class StudentSpringController {

@PostMapping(“/student”)

public String studentInformation(@RequestBody StudentAtSevenMentor sm) {

 

//For DAO logic …wait for few more blogs….!! Wish you happy coding ..!!

 

return “Student Information Saved Successfully… ::”+sm.getStudFirstName() +” “+sm.getStudLastNAme()+”  Contact No : “+sm.getPhoneNo()

+” Course Entrolled : “+sm.getCourse()+”  Fees Paid  : “+sm.getFees() + ” Mode Of Payment  : “+sm.getModeOfPayment() ;

}

 

}

For Free, Demo classes Call: 8237077325

Registration Link: Click Here!

Pom.xml will look like :

<?xml version=“1.0” encoding=“UTF-8”?>

<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.1.6.RELEASE</version>

<relativePath/> <!– lookup parent from repository –>

</parent>

<groupId>com.example</groupId>

<artifactId>Demo_Proj_05</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>Demo_Proj_05</name>

<description>Demo project for Spring Boot</description>

<properties>

<java.version>1.8</java.version>

</properties>

 

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

 

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

</dependencies>

 

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

Want To Learn More about Spring boot Like —

  • core configuration –Spring application with profiles logging
  • web application –MVC
  • Working with – SQL / Postgresql
  • Testing – Boot application
  • Extensions –> Autoconfiguration and @Conditions

Wait for few more blogs …will get back to you next month again with new happenings in software industry …!!

Hope you got the basic idea behind spring boot and microservices. But you may face many issues while executing this example. Feel free to ask your queries. Happy to help you.

Author-
Sonali Mahajan

For Free, Demo classes Call: 8237077325

Registration Link: Click Here!

Call the Trainer and Book your free demo Class for JAVA now!!!

call icon

© Copyright 2019 | Sevenmentor Pvt Ltd.

 






Pin It on Pinterest