Session 4.1: Spring Boot - Microservices creation and communications
1. Clone the following repository
$ git clone https://github.com/M-Gharib/ESI-W4.git
Note: if you want to create Spring Boot projects from scratch, you need to install the following dependency for both of the services:
- Spring Web
- Spring Data JPA SQL
- PostgresSQL Driver SQL
- Lombok
- Validation I/O
- Spring Reactive Web
Note: if you can add dependencies for an already created Spring Boot project by opening the command pallet in VSCode (Windows: Ctrl + Shift + P, Mac: Cmd + Shift + P). In the textbox, choose Spring initializer: Add starters. Then, start entering the name of the starter/ dependency you want to add.
The project you cloned is composed of two different Spring Boot projects that represent two different microservices:
- product-service: fetch, add, update, and delete products.
- inventory-service: only contain the quantity of a specific product that can be identified based on a code of a product.
Each microservice has its own database and uses a different port.
The databases will be created automatically when you run their related application, and two rows of data will be inserted automatically into their respective tables to enable you to complete this session. Check the code related to the creation of the databases and the code related to the insertion of the data.
1. Check the configurations of each of these services in the application.properties
that can be found within the resources folder for each of the services.
src └── main └── java └── resources
Your teacher will describe most of the code to you, starting from the inventory-service
, which do not do anything we do not already know.
2. Check the ProductDto.java
and ProductQuantityDto.java
classes in the code of the product-service
under the dto
directory, what is the difference between them?
3. Check the differences between the following request handlers in ProductController.java
@GetMapping("/products/{id}") public Optional<ProductDto> getProduct(@PathVariable String id){ return productService.getProduct(id); } @GetMapping("/productquantity/{id}") public Optional<ProductQuantityDto> getProductWithQuantity(@PathVariable String id){ return productService.getProductWithQuantity(id); }
WebClient
WebClient is an interface representing the main entry point for performing web requests. It is a part of the Spring Web Reactive module and will be replacing the classic RestTemplate. Unlike RestTemplate (synchronous), WebClient is a reactive (asynchronous), non-blocking solution that works over the HTTP/1.1 protocol.
In this session, we are using WebClient
to make a request from the product-service
to inventory-service
to fetch a quantity of a product based on its code, as shown in the figure
Your teacher will describe how we are using WebClient
to do that. The new code is annotated to facilitate your understanding.
Note: You can test how the projects work by using related requests in RestClientFile.rest
. Do not forget to modify the requests to fit the data in your databases.