MICROSERVICES ARCHITECTURE WITH REST TEMPLATE
Microservices :
Microservices allow large systems to be built up from a number of collaborating components. Microservices allows doing loose coupling between application processes instead of loose coupling between application components as Spring does.
Microservices Tooling Support
● Using Spring for creating Microservices
○ Setup new service by using Spring Boot
○ Expose resources via a RestController
○ Consume remote services using RestTemplate
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, a 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?
Service discovery is how applications and (micro)services locate each other on a network.Spring Cloud support several ways to implement service discovery but for this, I am going to use Eureka created by Netflix. Spring Cloud provides several annotation to make it use easy and hiding lots of complexity.
Annotation Required:
- @EnableEurekaServer — configure a registry that will allow other applications to communicate.
To 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.
Step 1: Create Eureka Server: (Service Discovery)
❖ Create Spring Boot Starter project and add eureka server dependency.
❖ Application properties:
■ Add a port number and disable registration.
❖ Add annotation in the main function.
■ @EnableEurekaServer above @SpringBootApplication
❖ Run as localhost:8761
Step 2 : Create Microservices:
Create Spring Boot Starter project and add these dependencies
● Actuator: features to help you monitor and manage your application
● Eureka Discovery: for service registration
● JPA: to save/retrieve data
● H2: an in-memory database
● Rest Repositories: to expose JPA repositories as REST endpoints
● Web: Spring MVC and embedded Tomcat
● DevTools: to auto-reload the application when files change
● Lombok: to reduce boilerplate code.
GitHub link: https://github.com/praseela/Spring-Boot-Microservice
This POC is mainly focus on communicating one service to another service through the Gateway.
Used Technology:
· Java 1.8
· Spring Boot for API
· Spring Boot JPA.
· Spring Cloud.
· Eureka Server.
· Swagger UI.
· MySQL for Database
Application Description:
In this POC, I have created one Gateway and two services. These two services are communicating through the Gateway. The EcartGateway contain Product and Customer Service. Each service contains ( GET, POST, PUT, DELETE ) endpoints to trigger that function and stored and retrieved it from database. The Customer service will get the list of Product from the Product service with the help of gateway while its running stage.
Eureka Server:
Customer Service:
Customer Service get product from Product Service:
Product Service: