A demonstration of using Spring Cloud Stream with RabbitMQ to transfer messages between micro-services.
- Java 11
- Spring Boot
- Spring Cloud Stream / Spring Cloud Stream Reactive
- Spring Cloud Stream RabbitMQ Binder
- Spring Cloud Netflix / Eureka (discovering and load balancing)
- Spring Web / Web Flux
- Spring Data JPA / Hibernate
- Spring Retry
- Spring Actuator
- FlyWay
- Bootstrap & JavaScript
- PostgreSQL
- RabbitMQ
- Docker / Docker Compose
- Client 'wants' to buy a product and sends a POST request with specific
productId
toorder-service
. By default thedemo-client
sends such requests every 200 ms. - Order service receives 'buy a product' request then creates new order, stores it
to the local DB, then sends event
OrderCreated
to the message broker. - Product service has subscribed on
OrderCreated
event. When the service received the event it tries to 'sell' the product. If the specified product exists and its amount is not zero then the service reduces its amount per unit, and sends the eventOrderCompleted
. If the product not found then the service sendsProductNotFound
event. If the product amount is zero then service sendsProductEnded
event. - Order service received these events and mark the order respectively: completed or failed.
- Supply service has subscribed on
ProductEnded
event. When it received the event then it sends a POST request toproduct-service
to restock the product with some value. - Stat service listens to all kind of the events, calculates their rates for the last 10 seconds, and sends it with SSE (Server Sent Event) to a subscriber (if there is one) every 1 second.
- To configure the routes of the events in the message broker the config application is used. It should be run only once before starting the rest of the services. With Spring Cloud Stream the routes are configured pretty simple in the Routes interface and the application properties file.
- To avoid locking in the DB of the Product service, when several threads and/or several instances of the service will try to modify the amount of the particular product, Optimistic Locking and Spring Retry are used.
You should have Java 11, Git, Maven, Docker and Docker Compose be installed.
- Clone this project and
cd
to its folder - Build the project with maven:
mvn package
- Move to
demo
folder
cd demo
- Run the environment:
docker-compose up -d --build
docker-compose down
You can increase system load and performance by scaling instances of the services and demo-client
.
For example, scaling demo-client
and product-service
:
docker-compose up -d --scale demo-client=2 --scale product-service=2