Microservices

The microservice architecture provides a rapid, frequent and reliable delivery of large and complex applications. It is distributed and loosely coupled, so it won't break the entire app if you make changes in one team which builds an application as a collection of small autonomous services developed for a business domain.

Microservices are also known as the microservice architecture, a variant of the service-oriented architecture (SOA) structural style and used to structure an application as a collection of services that have the following features:
  • Distributed and loosely coupled
  • Highly maintainable and testable
  • Independently deployable
  • Organized around business capabilities
  • Owned by a small team
Following are the three commonly used tools for Microservices:
  • Wiremock
  • Docker
  • Hysrix

The Microservice architecture of an application can be simplified into multiple modules that independently perform the single precise standalone task. Let's see how Microservice architecture works:

  • An application is fragmented into loosely coupled various modules, each of which performs a distinct function.
  • It is distributed across clouds and data centers.
  • Each application module is an independent service/process that can be replaced, updated, or deleted without disrupting the rest of the application.
  • Under microservice architecture, an application can grow along with its requirements.

Following is a list of some most important advantages of using Microservices:
  • Microservices provide great technology diversity. You can mix it easily with other frameworks, libraries, and databases.
  • Microservices support fault isolation as it collects small autonomous services or processes, so a process failure should not bring the whole system down.
  • It provides excellent support for the minor and parallel team.
  • It reduces the deployment time significantly.
  • Independent deployment
Following is a list of some disadvantages of using Microservices:
  • Increases troubleshooting challenges
  • Increases delay due to remote calls
  • Increased efforts for configuration and other operations
  • Difficult to maintain transaction safety
  • Tough to track data across various boundaries
  • Difficult to code between services

Monolithic architecture is like a big container in which all the software components of an application are clubbed inside a single package.

Comparison Between Monolithic SOA & Microservices 

  • Monolithic Architecture is similar to a big container wherein all the software components of an application are assembled together and tightly packaged.
  • A Service-Oriented Architecture is a collection of services which communicate with each other. The communication can involve either simple data passing or it could involve two or more services coordinating some activity.
  • Microservice Architecture is an architectural style that structures an application as a collection of small autonomous services, modeled around a business domain.

With Spring Boot, your microservices can start small and iterate fast. That’s why it has become most polular in microservices. Quickstart your project with Spring Initializr and then package as a JAR. With Spring Boot’s embedded server model, you’re ready to go in minutes.

Spring boot 

Spring has become more and more complex as new functionalities have been added. If you have to start a new spring project, then you have to add build path or add maven dependencies, configure application server, add spring configuration. So everything has to be done from scratch.

Spring Boot is the solution to this problem. Using spring boot you can avoid all the boilerplate code and configurations. So basically consider yourself as if you’re baking a cake spring is like the ingredients that are required to make the cake and spring boot is the complete cake in your hand.

Spring Boot favors convention over configuration and is help to up and running as quickly as possible.
It is used to create stand alone spring based application that you can just run because it needs very little spring configuration.

Spring boot let;

1. Easy dependency Management
when you add springboot-starter-web dependency by default it will pull all the commonly used libraries while developing Spring MVC applications such as spring-webmvc, jackson-json, validation-api and tomcat.
When you add  spring-boot-starter-data-jpa dependency. This pulls all the spring-data-jpa dependencies and also adds Hibernate libraries because the majority of the applications use Hibernate as JPA implementation.
Spring Boot manages dependencies and configuration automatically. You don't need to specify version for any of that dependencies.Spring Boot upgrades all dependencies automatically when you upgrade Spring Boot.


2. Auto Configuration
Not only the spring-boot-starter-web adds all these libraries but also configures the commonly registered beans like DispatcherServlet, ResourceHandlers, MessageSource etc beans with sensible defaults.
When you add spring-boot-starter-Thymeleaf which not only adds the Thymeleaf library dependencies but also configures ThymeleafViewResolver beans as well automatically.
you dont need to define any of the DataSource, EntityManagerFactory, TransactionManageretc beans but they are automatically gets created. How? If we have any in-memory database drivers like H2 or HSQL in our classpath then SpringBoot will automatically create an in-memory DataSource and then registers EntityManagerFactory, TransactionManager beans automatically with sensible defaults. But when using MySQL, so we need to explicitly provide MySQL connection details. You have configured those MySQL connection details in application.properties file and SpringBoot creates a DataSource using these properties.


3. Embedded Servlet Container Support
The most important and surprising thing is when you created a simple Java class annotated with some magical annotation @SpringApplication having a main method and by running that main we are able to run the  application and access it at http://localhost:8080/.

Where is the servlet container comes from

you have added spring-boot-starter-web which pulls the spring-boot-starter-tomcat automatically and when you run the main() method it started tomcat as an embedded container so that you don’t have to deploy your application on any externally installed tomcat server.

Spring Vs Spring Boot

Spring is a web application framework based on Java. It provides tools and libraries to create a complete cutomized web application.Wheras Spring Boot is a spring module which is used to create spring application project that can just run.

Advantages of Spring boot
  • Create stand-alone Spring applications that can be started using java -jar.
  • Embed Tomcat, Jetty or Undertow directly. You don't need to deploy WAR files.
  • It provides opinionated 'starter' POMs to simplify your Maven configuration.
  • It automatically configure Spring whenever possible.
Features of Spring Boot
  • Web Development
  • SpringApplication
  • Application events and listeners
  • Admin features
Spring Boot project using Spring Initializer

It is a web tool which is provided by Spring on official site. You can create Spring Boot project by providing project details.

@RestController is a stereotype annotation. It adds @Controller and @ResponseBody annotations to the class. We need to import org.springframework.web.bind.annotation package in our file, in order to implement it.

 @RequestMapping annotation is used to provide routing information. It tells to the Spring that any HTTP request should map to the corresponding method. We need to import org.springframework.web.annotation package in our file.

Spring Boot properties
Spring Boot provides various properties which can be specified inside our project's application.properties file. These properties have default values and you can set that inside the properties file. Properties are used to set values like: server-port number, database connection configuration etc.

Spring Boot Actuator
Spring Boot provides actuator to monitor and manage our application. Actuator is a tool which has HTTP endpoints. when application is pushed to production, you can choose to manage and monitor your application using HTTP endpoints.

thymeleaf
It is a server side Java template engine for web application. It's main goal is to bring elegant natural templates to your web application. It can be integrate with Spring Framework and ideal for HTML5 Java web applications.

Connect Spring Boot to the database using JPA
Spring Boot provides spring-boot-starter-data-jpa starter to connect Spring application with relational database efficiently. You can use it into project POM (Project Object Model) file.

Spring cloud is used to manage the microservices where as Spring boot is used to create microservices.


Spring cloud

Spring Cloud is a framework that provides facilities to use cloud services in your application. It acts as a container orchestration tool. The framework that provides an enterprise-level framework for integrating and managing container at scale is called container orchestration tool. It gives a developer-friendly environment for developing and deploying microservices.

Spring Cloud framework provides tools for developers to build a robust cloud application quickly. We can also build the microservice-based applications, for example, configuration management, service discovery, circuit breakers, intelligent routing, cluster state, micro-proxy, a control bus, one time tokens, etc. Using Spring Cloud, a developer can quickly develop services and applications that implement the design patterns. 

Why we should require Spring Cloud
We face the following issues during the development of distributed microservices using Spring Boot:

  • Performance issues: Different operational overheads affects performance badly.
  • Complexity in deployment: DevOps skills are required.
  • Redundancy: Distributed system often faces redundancy issues.
  • Load–balancing: Workload distribution across various computing resources are improved by Load Balancing.
  • Distributed system complexities: Complexities include bandwidth issues, network issues, security issues, latency issues, etc.
  • Service directory tools: These tools let the processes and services in the same cluster talk to each other.
Spring Cloud's primary goal is to keep the network's traffic flowing. Another point of view SB's main goal is to save time and increase yield.

Advantages of Spring Cloud
  • Cloud-native based development
  • Microservice-based architecture
  • Inter-service communication
  • Follows the Spring Boot model
  • It is cloud-agnostic
  • Distributed configuration
  • Distributed messaging
  • service-to-service calls
  • Circuit breakers
  • Global locks
  • Service registration
  • Service Discovery
  • Load balancing
  • Cluster state
  • Routing

Comments