Microservices Architecture with Spring Boot
- 26 oct. 2017
- 4 min de lecture
In this example we will see an online shop with separate microservice for each function user-account, product-catalog, order-processing and shopping carts.
Let see at first a shopping system without Microservices (Monolith architecture).
In this architecture we are using Monolith architecture i.e. all collaborating components combine all in one application.

The next architecture shows a Microservices architecture style, the main application is divided in a set sub applications called microservices.

To implement such system you have to knowledge about Spring Boot, Spring Cloud and Netflix.
Microservices Infrastructure
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.
Microservices implementation
1- Using Spring for creating Microservices
Setup new service by using Spring Boot
Expose resources via a RestController
Consume remote services using RestTemplate
2- Adding Spring Cloud and Discovery server
2-1 Spring Cloud
A building blocks for Cloud and Microservices
Provides microservices infrastructure like provide use services such as Service Discovery, Configuration server and Monitoring.
Provides several other open source projects like Netflix OSS.
Provides PaaS like Cloud Foundry, AWS and Heroku.
Uses Spring Boot style starters
There are many use-cases supported by Spring Cloud like Cloud Integration, Dynamic
Reconfiguration, Service Discovery, Security,Client side Load Balancing etc. But in this post
we concentrate on following microservices support
Service Discovery (How do services find each other?)
Client-side Load Balancing (How do we decide which service instance to use?)
2-2 Service Discovery
Problem How do services find each other? What happens if we run multiple instances for a service

Resolution

Implementation
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.
2-3 Client-side Load Balancing
Problem Each service typically deployed as multiple instances for fault
Tolerance and load
Resolution

Implementation
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.
Microservices example development
1.Creating Discovery Service (Creating Eureka Discovery Service)
2.Creating MicroService (the Producer)
1.Register itself with Discovery Service with logical service.
3.Create Microservice Consumers find Service registered with Discovery Service
1.Discovery client using a smart to find microservice.
Step 1: Creating Discovery Service (Creating Eureka Discovery Service)
Eureka Server using Spring Cloud
We need to implement our own registry service as below.

For Whole Source Code for the Discover Server Application you could download from github as below link.
Run this Eureka Server application by maven command mvn spring–boot :run 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.

Other required source files related to this application you could download from github link as given below
Now run this account service application by maven command mvn spring–boot :run 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 “ACCOUNTS-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.

Other required source files related to this application you could download from github link as given below.
Now run this consumer service application by maven command mvn spring–boot :run 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.

Now click on any account from the list of accounts to fetch the details of account for account number from account microservice.

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 that return the URL of all available instances
- Ribbon Client Side Load Balancer that determine the best available service too use
Just inject the load balanced RestTemplate automatic lookup by logical service-name
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.

Commentaires