Microservices with Spring Framework

Introduction

As the title suggests, we will be discussing about how we can use microservices with spring framework . But before jump into spring framework, let me give you quick idea about what microservices actually are  

The microservice architectural style is an approach of developing 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. 

To understand the microservice style, it’s useful to compare it to the monolithic style. 

  

Monolithic Architecture  

In simpler termone can define monolithic style approach as an applicationthat has been built on single unit. 

Generally, Enterprise Applications are often built in three main parts:  

  1. A client-side user interface (consisting of HTML pages and JavaScript running in a browser on the user’s machine)  
  1. A database (consisting of many tables inserted into a common, and usually relational, database management system), and  
  1. server-side application. 

 The server-side application will handle HTTP requests, execute domain logic, retrieve and update data from the database, and select as well as populate HTML views to be sent to the browser.  

This server-side application is a monolith – a single logical executable.  

Disadvantages of monolithic approach 

  • Any changes made to the system, involve building and deploying a new version of the server-side application. 
  • Hence sometimesthe monolithic approach becomes frustrating – especially as more applications are being deployed to the cloud. The change cycles are tied together – a change made to a small part of the application, requires the entire monolith to be rebuilt and deployed.  
  • Thus, this led to the discovery of microservice architectural style: building applications as suites of services. 

  

Quick Overview of MicroServices  

  

Now, lets get quick a brief on microservices before moving onto the spring framework.  

In 2005, Peter Rodgers introduced the term “Micro-Webservices” during a presentation at the Web Services Edge conference.  

  1. Robert C. Martin coined the term single responsibility principle which states gather together those things that change for the same reason, and separate those things that change for different reasons. 

Lets understand Microservices using this diagram. 

The requestcomes through the clientgoes to the UI (you can refer this UI as view or Frontend) The request then comes through API or Controller and moves on to Services further moving on to the Database and then lastly to the client. 

Microservices are an architectural style, that develops a single application as a set of small services that are 

  • Highly maintainable and testable 
  • Loosely coupled 
  • Independently deployable 
  • Organized around business capabilities 
  • Owned by a small team 

  

Each of this service runs in its own process. The services communicate with the clients, and with each other, using lightweight protocols, often over HTTP. 

 

Advantages of using microservices 

  

  • Since the services are small, they can be built by one or more small teams, from the beginning separated by service boundarieswhich makes it easier to scale up the development effort, if needed.  
  • Once developed, these services can also be deployed independently of each other. 
  • Hence, if there occurs an error in one service, then the whole application doesnt necessarily stop functioning. When the error is fixed, it can be deployed only for the respective service instead of redeploying an entire application. 
  • Another advantage that a microservices architecture brings to the table, is making it easier to choose the technology stack (programming languages, databases, etc.) which is best suited for the required functionality (service) instead of being required to take, a more standardized, one-size-fits-all approach. 

  

Now that have come this far, and are aware of the basic idea about microservices and the functionality behind it, lets start with our main topicMicroservices with spring framework.  

Microservices with Spring Framework 

The first version was written by Rod Johnson, who released the framework with the publication of his book Expert One-on-One J2EE Design and Development in October 2002. The framework was first released under the Apache license 2.0 in June 2003. The first production release, 1.0, was released in March 2004.The Spring 1.2.6 framework won a Jolt Productivity Award and a JAX innovation award in 2006. 

Source : https://en.wikipedia.org/wiki/Spring_Framework 

When Spring came out, it was a simpler, a light-weight alternative to J2EE, to make J2EE development easier. 

But Wait, what does enterprise Java refers to? 

  1. Enterprise Java refers to the Java enterprise software. It’s computer software used to satisfy the needs of an organization rather than individual users. Examples can include: accounting software, billing Management, business process management, ERP, etc. 

Now, what are the benefits of using Spring? 

Advantages of Spring 

  1. Uses Java POJOs 

  

Spring Framework uses Java POJOs (Plain Old Java Object), which make it much simpler to build enterprise-class applications as compared to the heavyweight EJBs (from the earlier versions of J2EE).   

Tip: (A POJO is a Java objectnot bound by any restriction other than those forced by the Java language specification. So, POJO does not require to extend pre-specified classes or implement pre-specified interfaces in the framework.) 

  1. Dependency of Injection 

Spring framework promotes loose coupling by making use of dependency of injection. So, instead of hard-coding the dependencies of an object, you simply specify the dependencies via configuration file (or annotations, or java code). 

One might wonder, what dependency injection is?  

Here we will discuss concept of dependency injection using some examples 

In object-oriented design, Coupling refers to the degree of direct knowledge that one element has of another. In other words, how often do changes in class A force related changes in class B. 

  

There are two types of coupling: 

Tight coupling: In general, tight coupling means, that the two classes often change together. In other words, if A knows more than it should, about the way in which B was implemented, then A and B are tightly coupled. 

Example: If you want to change the skin, you would also have to change the design of your body because the two are intertwined together. 

  

/* Heres one simple java program to understand what Tight Coupling is? 

 Any changes or destruction of class Car will reflect to class Vehicle. 

 Changing method of Car class will reflect Vehicle class. 

*/ 

class Vehicle {  

Car c = new Car ();  

public void numberOfTyre()  

 

c.print ();  

 

 

class Car {  

public void print()  

 

System.out.println(“Tight coupling”);  

 

} 

  

Loose coupling: In simple words, loose coupling means, that the classes are mostly independent. If the only knowledge that class A has about class B, is what class B has exposed through its interface, then class A and class B are said to be loosely coupled. In order to overcome the problems of tight coupling between objects, spring framework uses dependency injection mechanism with the help of POJO/POJI model and through dependency injection its possible to achieve loose coupling. 

  

Still Confused? Dont worry. Lets understand this using this diagram. 

Lets say you want to change your shirt, in this case you dont need to change body like you did in tight coupling. This is loose coupling. 

  

/* Simple Java program to illustrate  

 Loose coupling concept  

/* 

public interface Topic  

 

void understand();  

 

class Topic1 implements Topic {  

public void understand()  

 

System.out.println(“Got it”);  

 

} class Topic2 implements Topic {  

public void understand()  

 

System.out.println(“understand”);  

 

} public class Subject {  

public static void main(String[] args)  

 

Topic t = new Topic1();  

t.understand();  

 

} 

  

Explanation: in above example if any changes are made to the requirement specification or interface, you dont have to change all the functionalities, right? Just change the implementation class i.e., Subject class.  

 In dependency injectioninstead of declaring Topic classs object in class Subject you can configure it into config file which can be XML, using plain Java or Annotation. This is what Dependency injection is all about. 

 

  1. Aspect Oriented Programming (AOP) 

It allows the separation of cross-cutting concerns from the business logic. 

The cross-cutting concerns are features, that span across different parts of the application and affect the entire application. 

 Examples → logging, declarative transactions, security, caching, etc. 

But why we would use it in the first place? 

AOP attempts to solve this problem by allowing us to express cross-cutting concerns in stand-alone modules called aspects. 

For example, a security module aspect” can include advice” that performs a security check (actual piece of code that will be executed), and a join point (when the code is executed) which defines when in the corresponding program the aspect code, should be executed. 

This can be done either using XML syntax, or syntax based on AspectJ, which uses Java code. 

 

  1. Spring Architecture  

Spring is organized in a modular fashion which allows you to concentrate only on the modules you need and ignore the rest. 

Modular programming is a software design technique that separates the functionality of a program into independent modules, such that each contains one specific functionality. 

Modularity vs. AOP  The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. 

It provides about 20 modules. All modules are built on the top of its core container. These modules provide everything that a developer may need, to use in the enterprise application development. 

  1. Core Container is used for creating beans and managing beans dependencies. 
  1. The Core and Bean modules provide the fundamental parts of the framework, including the IoC and Dependency Injection features. 
  1. The Bean module provides BeanFactory, which is a sophisticated implementation of the factory pattern. It decouples the configuration and specification of dependencies from the actual program logic. 

The Context module builds upon the Core and Beans modules. It is a means to access any objects defined and configured.  

  1. Core Container 

Its used for creating beans and managing beans dependencies. 

The Core and Bean modules provide the fundamental parts of the framework, including the IoC and Dependency Injection features. 

The Bean module provides BeanFactory, which is a sophisticated implementation of the factory pattern. It decouples the configuration and specification of dependencies from the actual program logic. 

The Context module builds on the Core and Beans modules. It is a means to access any objects defined and configured. 

  

Final thought  

We came across some basics of Microservices and Spring Framework and discussed their advantagesBoth of these are gaining popularity by the day. Below, are reports stated by Wikipedia on Microservices, for your reference.  

In February 2020, the Cloud Microservices Market Research Report predicted that the global microservice architecture market size, will increase at a CAGR of 21.37% from 2019 to 2026 and reach $3.1 billion by 2026. 

Source: https://en.wikipedia.org/wiki/Microservices 

 

Spring is one of the most popular frameworks for Java enterprise edition.   

It is much easier for a beginner aspiring to be a java developer, to start with the spring framework.  

I hope this blog helps you in getting started with the spring framework!  

All the best! 

ImranShaikh
Imran is a BSC.IT graduate. His current goal is to learn and implement, Microservices Architecture with Spring boot Technology. He wants to contribute in every part of microservices architecture in future, using Angular and various other technologies.
Aside from being a techfreak, Imran has a passion for traveling and exploring the world beyond.

Imran Shaikh, Intern at GlobalVox | Posted on: April 8, 2021