Develop Microservices using Spring Cloud — Part 1
Microservice architecture style was evolving throughout the last decade and it’s a demanding topic where anyone can find plenty of documents related to the concepts and implementation. According to Wikipedia, “A microservice architecture — a variant of the service-oriented architecture structural style — is an architectural pattern that arranges an application as a collection of loosely coupled, fine-grained services, communicating through lightweight protocols.”
In this article let’s discuss a use case where we can adopt microservices. Spring cloud will be used to develop the use case. Latest Spring version is used here(version 3) and it’s a bit different to use annotations and configs than other versions. The URL for the code can be found here: https://github.com/PubuduC/swagger-openapi-springcloud-integration
The use case can be taken as follows. Suppose there’s a store which will sell products in retail and wholesale. Products are organized in cartons for wholesale selling. Price structures can be taken as follows for an example.
Product A is 15 units per carton. A carton is 300.
Product B is 5 units per carton, a carton is 225.
For single unit purchasing, the price is acquired using the carton price multiplied by an increase of 30% (1.3). (i.e. to compensate for manual labor)
Purchase 3 cartons or more, you will receive a 10% discount off the carton price.
Let’s say we will want to calculate the price for a given product and amount with the above criteria.
For that we can have two services basically for the products and price calculation. One can wonder why we need two separate services as we can have both in a single service. For now as this is a simple scenario, that’s true. But when we develop this further, maybe we can isolate the product service for the product related crud operations and we can maintain separate utility service related to placing orders and calculating the price. For now as we are just calculating the price, let’s name it like price-service.
Eureka/ Discovery Server holds the information about all client-service applications. Every Micro service will register into the Eureka server and Eureka server knows all the client applications running on each port and IP address. In this example Standalone Mode is implemented. (Having only one eureka server instance. The combination of the two caches (client and server) and the heartbeats make a standalone Eureka server fairly resilient to failure. Anyway Multiple Peer Aware Eureka Servers can be implemented using Spring Cloud). The Eureka server will be registered as the server by using @EnableEurekaServer annotation and all other services will be registered as clients by using @EnableDiscoveryClient. The eureka server can be found here for this demo project. https://github.com/PubuduC/swagger-openapi-springcloud-integration/tree/main/eureka-discovery-service
API Gateway provides a flexible way of routing requests based on a number of criteria. It focuses on cross-cutting concerns such as security, resiliency, and monitoring also. In this simple scenario that will not add much value to API Gateway. In deployments we can deploy all the microservices inside private VPC without having public IP, where only the API Gateway will have public IP, which will be the only access point to the entire services.
In API Gateway, it is needed to add routes in RouteLocator bean, configuring which service to forward each request based on prefix. The project can be found here. https://github.com/PubuduC/swagger-openapi-springcloud-integration/tree/main/api-gateway-service
Product service and price service are simple microservices built using Spring Boot and Spring JPA. code can be found here. https://github.com/PubuduC/swagger-openapi-springcloud-integration/tree/main/price-service, https://github.com/PubuduC/swagger-openapi-springcloud-integration/tree/main/product-service PostgreSQL DB is connected to the product service for saving products. In the demo code I have added some good practices which we can follow when developing microservices.
- Audit the tables when creating and updating operations to your DB.
- Using ControllerAdvice for Exception handling
- Using Hibernate Validation to validate the inputs and handle bad request exceptions when thrown for invalid
- ModelMapper is used to map entity to DTOs
- Swagger added as API documentation. Will discuss this detail in the next chapter.
Thanks for reading! See you soon in next article!