#restserver #restclient #howmicroservicecommunicate #springbootmicroservices #resttemplate
You can fork this tutorial on my github.
rest-server(https://github.com/AminTurmudiYusup/r...)
rest-client(https://github.com/AminTurmudiYusup/r...)
How Microservices communicate with other services.
Prerequisite
1. You understand the concept of microservices
2. You already know different microservices and monolith app
3. Understand to create spring boot app from scratch
4. understand how to connect spring boot app to postgresql or mysql
5. understand how to integrate spring boot app with thymeleaf
6. Understand different synchronous and asynchronous communication
7. understand how to call api using postman
Before your application migrate from monolith to microservices, There are several factors to consider.
1. Your application want to support variety of different clients including dekstop browser, mobile browser and mobile native application.
2. Your application might also want to expose an API for 3 parties to consume.
3. Your application already scale horizontally and vertically, but the impact in performance not good
4. Your application handle many user in every day
In this tutorial i will use this scenario
1. ali bubu company have market place, the name of market place is shoping.com
2. andi is a seller in market place( shoping.com)
3. to make seller easy, shoping.com provide api for update product to every seller
4. in this scenario we have three component
rest-server(server which provide the resources=shoping.com)
rest-client(client which consume rest-server)
desktop browser(provide feature for seller, for update product using dekstop browser)
Let's jump right in
A. 1. create rest-server which connect to mysql database(spring boot app rest-server)
2. see my pom.xml
3. see my package structure
4. see my application.properties(match application properties as per your database connection)
5. create entity the name is product class
6. create repository interface
7. create controller class
8. check the result using post man, if rest-server work properly.
9. in rest-server i using port 8080
note in section save and update (rest server handle request body from client with JSON type)
B. 1. create rest-client which consume resource from server and send request to rest-server(springboot app client)
2. see my pom.xml(in rest-client)
3. see my application.properties
4. see my package structure
5. create configuration class and add create RestTemplate bean inside config package(beans =tells Spring 'here is an instance of this class, please keep hold of it and give it back to me when I ask)
6. create model class
7. create controller class. (call RestTemplate which already created, call using @Autoweird)@Autowired says 'please give me an instance of this class, for example, one that I created with an @Bean annotation earlier'.
8. create page product-list
9. create page form which used for save product in rest-client
10. create page form-update
11. run application rest-client
NOTE : in the real production environment, this services deployed in different environment(server)
Let's understand the flow
flow when show the list product
user hit this url http://localhost:8081/list/product/
rest-client send request to rest-server
when product exist
mapping product with ui(product-list.html)
flow when save product
show list product
click button add
show form to input product
mapping action from form
convert product into json
send value to rest-server
if success, show the list product already added
flow when delete
show list product
select row , you want to delete
send request to rest-server
if success redirect into list product page
flow when update
show list product
select row, you want update
mapping to edit page
submit, and send to rest-server
if success redirect to list product page
Happy learning and happy sharing !!! :) :) :)
Watch video How Microservices communicate with other services online without registration, duration hours minute second in high quality. This video was added by user Programming Tutorial 09 November 2022, don't forget to share it with your friends and acquaintances, it has been viewed on our site 78 once and liked it like people.