Microservices Architecture with Spring Boot in 15mins
1. Introduction
Microservices is not a new term. It coined in 2005 by Dr Peter Rodgers then called micro web services based on SOAP. It became more popular since 2010. Micoservices allows us to break our large system into number of independent collaborating processes. Lets see below microservices architecture.
1.1 What is Microservices Architecture?
Microservices architecture allows to avoid monolith application for large system. It provide loose coupling between collaborating processes which running independently in different environments with tight cohesion. So lets discuss it by an example as below.
For example imagine an online shop with separate microservices for user-accounts, product-catalog order-processing and shopping carts. So these components are inevitably important for such a large online shopping portal. For online shopping system we could use following architectures.
1.2 Shopping system without Microservices (Monolith architecture)
In this architecture we are using Monolith architecture i.e. all collaborating components combine all in one application.
1.3 Shopping system with Microservices
In this architecture style the main application divided in a set of sub applications called microservices. One large Application divided into multiple collaborating processes as below.
Spring enables separation-of-concerns
Microservices provide the same strength as Spring provide
Implementing Service Discovery
Spring Cloud support several ways to implement service discovery but for this I am going to use Eureka created by Netflix. Spring Cloud provide several annotation to make it use easy and hiding lots of complexity.
Client-side Load Balancing
Each service typically deployed as multiple instances for fault tolerance and load sharing. But there is problem how to decide which instance to use?
Implementing Client-Side Load Balancing
We will use Netflix Ribbon, it provide several algorithm for Client-Side Load Balancing. Spring provide smart RestTemplate for service discovery and load balancing by using @LoadBalanced annotation with RestTemplate instance.
6. Developing Simple Microservices Example
For build a simple microservices system following steps required
Maven Dependencies
Step 1: Creating Discovery Service (Creating Eureka Discovery Service)
application.yml
DiscoveryMicroserviceServerApplication.java
pom.xml
For Whole Source Code for the Discover Server Application you could download from github as below link.
discovery-microservice-server
Run this Eureka Server application with right click and run as Spring Boot Application and open in browser http://localhost:1111/
Step 2: Creating Account Producer MicroService
Microservice declares itself as an available service and register to Discovery Server created in Step 1.
Lets see the service producer application structure as below.
application.yml
AccountsMicroserviceServerApplication.java
pom.xml
Other required source files related to this application you could download from github link as given below
accounts-microservice-server
Now run this account service application as Spring Boot application and after few seconds refresh browser to the home page of Eureka Discovery Server at http://localhost:1111/ in previous Step 1. Now one Service registered to the Eureka registered instances with Service Name "ACCOUNT-MICROSERVICE" as below
Step 3: Consumer Service
Lets see the consumer application structure as below.
application.yml
WebclientMicroserviceServerApplication.java
pom.xml
Other required source files related to this application you could download from github link as given below
webclient-microservice-server
Now run this consumer service application as Spring Boot application and after few seconds refresh browser to the home page of Eureka Discovery Server at http://localhost:1111/ in previous Step 1. Now one more Service registered to the Eureka registered instances with Service Name "ACCOUNTS-WEB" as below
Lets our consumer consume the service of producer registered at discovery server.
Lets open web application which is a consumer of the account microservice registered at Eureka Discovery Server.
http://localhost:8080/ as below
Now click on View Account List then fetch all accounts from account microservice.
http://localhost:8080/accountList
Now click on any account from the list of accounts to fetch the details of account for account number from account microservice.
http://localhost:8080/accountDetails?number=5115
Load Balanced RestTemplate
Create using @LoadBalanced- Spring enhances it to service lookup & load balancing
Must inject using same qualifier-
Load Balancing with Ribbon
Our smart RestTemplate automatically integrates two Netflix utilities
Eureka return the URL of all available instances
Ribbon determine the best available service too use
Just inject the load balanced RestTemplate automatic lookup by logical service-name
7. Summary
After completion of this article you should have learned:
Microservices is not a new term. It coined in 2005 by Dr Peter Rodgers then called micro web services based on SOAP. It became more popular since 2010. Micoservices allows us to break our large system into number of independent collaborating processes. Lets see below microservices architecture.
1.1 What is Microservices Architecture?
Microservices architecture allows to avoid monolith application for large system. It provide loose coupling between collaborating processes which running independently in different environments with tight cohesion. So lets discuss it by an example as below.
For example imagine an online shop with separate microservices for user-accounts, product-catalog order-processing and shopping carts. So these components are inevitably important for such a large online shopping portal. For online shopping system we could use following architectures.
1.2 Shopping system without Microservices (Monolith architecture)
In this architecture we are using Monolith architecture i.e. all collaborating components combine all in one application.
1.3 Shopping system with Microservices
In this architecture style the main application divided in a set of sub applications called microservices. One large Application divided into multiple collaborating processes as below.
Spring enables separation-of-concerns
- Loose Coupling- Effect of changes isolated
- Tight Cohesion- Code perform a single well defined task
Microservices provide the same strength as Spring provide
- Loose Coupling- Application build from collaboration services or processes, so any process change without effecting another processes.
- Tight Cohesion-An individual service or process that deals with a single view of data.
There are a number of moving parts that you have to setup and configure to build such a system. For implementing this system is not too obvious you have to knowledge about spring boot, spring cloud and Netflix. In this post I will discuss one example for this architecture before the example lets first discuss about pros and cons of microservices architecture.
2. Microservices Benefits
5.1 Using Spring for creating Microservices
5.2 Adding Spring Cloud and Discovery server
What is Spring Cloud?
Problem without discovery
Resolution with service discovery
2. Microservices Benefits
- Smaller code base is easy to maintain.
- Easy to scale as individual component.
- Technology diversity i.e. we can mix libraries, databases, frameworks etc.
- Fault isolation i.e. a process failure should not bring whole system down.
- Better support for smaller and parallel team.
- Independent deployment
- Deployment time reduce
- Difficult to achieve strong consistency across services
- ACID transactions do not span multiple processes.
- Distributed System so hard to debug and trace the issues
- Greater need for end to end testing
- Required cultural changes in across teams like Dev and Ops working together even in same team.
- Platform as a Service like Pivotal Cloud Foundry help to deployment, easily run, scale, monitor etc.
- It support for continuous deployment, rolling upgrades fo new versions of code, running multiple versions of same service at same time.
5.1 Using Spring for creating Microservices
- Setup new service by using Spring Boot
- Expose resources via a RestController
- Consume remote services using RestTemplate
5.2 Adding Spring Cloud and Discovery server
What is Spring Cloud?
- It is building blocks for Cloud and Microservices
- It provides microservices infrastructure like provide use services such as Service Discovery, Configuration server and Monitoring.
- It provides several other open source projects like Netflix OSS.
- It provides PaaS like Cloud Foundry, AWS and Heroku.
- It uses Spring Boot style starters
- Service Discovery (How do services find each other?)
- Client-side Load Balancing (How do we decide which service instance to use?)
Problem without discovery
- How do services find each other?
- What happens if we run multiple instances for a service
Resolution with service discovery
Implementing Service Discovery
Spring Cloud support several ways to implement service discovery but for this I am going to use Eureka created by Netflix. Spring Cloud provide several annotation to make it use easy and hiding lots of complexity.
Client-side Load Balancing
Each service typically deployed as multiple instances for fault tolerance and load sharing. But there is problem how to decide which instance to use?
Implementing Client-Side Load Balancing
We will use Netflix Ribbon, it provide several algorithm for Client-Side Load Balancing. Spring provide smart RestTemplate for service discovery and load balancing by using @LoadBalanced annotation with RestTemplate instance.
6. Developing Simple Microservices Example
For build a simple microservices system following steps required
- Creating Discovery Service (Creating Eureka Discovery Service)
- Creating MicroService (the Producer)
- Register itself with Discovery Service with logical service.
- Create Microservice Consumers find Service registered with Discovery Service
- Discovery client using a smart RestTemplate to find microservice.
Maven Dependencies
Step 1: Creating Discovery Service (Creating Eureka Discovery Service)
- Eureka Server using Spring Cloud
- We need to implement our own registry service as below.
application.yml
DiscoveryMicroserviceServerApplication.java
pom.xml
For Whole Source Code for the Discover Server Application you could download from github as below link.
discovery-microservice-server
Run this Eureka Server application with right click and run as Spring Boot Application and open in browser http://localhost:1111/
Step 2: Creating Account Producer MicroService
Microservice declares itself as an available service and register to Discovery Server created in Step 1.
- Using @EnableDiscoveryClient
- Registers using its application name
Lets see the service producer application structure as below.
application.yml
AccountsMicroserviceServerApplication.java
pom.xml
Other required source files related to this application you could download from github link as given below
accounts-microservice-server
Now run this account service application as Spring Boot application and after few seconds refresh browser to the home page of Eureka Discovery Server at http://localhost:1111/ in previous Step 1. Now one Service registered to the Eureka registered instances with Service Name "ACCOUNT-MICROSERVICE" as below
Step 3: Consumer Service
- Create Consumers to find the Producer Service registered with Discovery Service at Step 1.
- @EnableDiscoveryClient annotation also allows us to query Discovery server to find miroservices.
Lets see the consumer application structure as below.
application.yml
WebclientMicroserviceServerApplication.java
pom.xml
Other required source files related to this application you could download from github link as given below
webclient-microservice-server
Now run this consumer service application as Spring Boot application and after few seconds refresh browser to the home page of Eureka Discovery Server at http://localhost:1111/ in previous Step 1. Now one more Service registered to the Eureka registered instances with Service Name "ACCOUNTS-WEB" as below
Lets our consumer consume the service of producer registered at discovery server.
Lets open web application which is a consumer of the account microservice registered at Eureka Discovery Server.
http://localhost:8080/ as below
Now click on View Account List then fetch all accounts from account microservice.
http://localhost:8080/accountList
Now click on any account from the list of accounts to fetch the details of account for account number from account microservice.
http://localhost:8080/accountDetails?number=5115
Load Balanced RestTemplate
Create using @LoadBalanced- Spring enhances it to service lookup & load balancing
Must inject using same qualifier-
- If there are multiple RestTemplate you get the right one.
- It can used to access multiple microservices
Load Balancing with Ribbon
Our smart RestTemplate automatically integrates two Netflix utilities
- Eureka Service Discovery
- Ribbon Client Side Load Balancer
Eureka return the URL of all available instances
Ribbon determine the best available service too use
Just inject the load balanced RestTemplate automatic lookup by logical service-name
7. Summary
After completion of this article you should have learned:
- What is the MicroServices Architecture
- Advantages and Challenges of MicroServices
- And some information about Spring Cloud such as Eureka Discover Server by Netflix and Ribbon.
Comments
Microservices Online Training
Microservices Online Training
Microservices Training in Hyderabad
spring boot online training
spring boot full course
spring boot online training hyderabad
microservices online training
PHP multiple file upload
How to fetch data from database in PHP
text to audio converter
ttk combobox
Extract text from image Python
PHP query string
Save emoji in MySQL
Alphabet pyramid pattern in Python
Python tkinter combobox
PHP multiple file upload
How to fetch data from database in PHP
text to audio converter
ttk combobox
Extract text from image Python
PHP query string
Save emoji in MySQL
workday online integration course
workday online integration course in india
workday online integration course india
workday studio training india
Transform your legacy application with a microservices architecture that breaks your application into small components independent from each other.
to get more - https://www.nitorinfotech.com/services/microservices/
Transform your legacy application with a microservices architecture that breaks your application into small components independent from each other.
to get more - https://www.nitorinfotech.com/services/microservices/
workday online training hyderabad
workday online training in india
Oracle Fusion HCM Training
Workday Training
Okta Training
Palo Alto Training
Adobe Analytics Training
spring boot certification course training
spring boot certification course training
spring boot certification course training
Very good information.
Spring Boot Online Training