Coder Social home page Coder Social logo

cepr0 / sb-cloud-stream-demo Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 136 KB

A demonstration of using Spring Cloud Stream with RabbitMQ to transfer messages between micro-services

Java 92.71% Dockerfile 3.59% Shell 0.73% HTML 2.97%
spring-boot spring-cloud-stream rabbitmq reactive spring-webflux postgresql microservices spring-retry optimistic-locking blog

sb-cloud-stream-demo's Introduction

Spring Cloud Stream demo

A demonstration of using Spring Cloud Stream with RabbitMQ to transfer messages between micro-services.

Used stack

  • 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

Architecture

architecture

Workflow

  1. Client 'wants' to buy a product and sends a POST request with specific productId to order-service. By default the demo-client sends such requests every 200 ms.
  2. 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.
  3. 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 event OrderCompleted. If the product not found then the service sends ProductNotFound event. If the product amount is zero then service sends ProductEnded event.
  4. Order service received these events and mark the order respectively: completed or failed.
  5. Supply service has subscribed on ProductEnded event. When it received the event then it sends a POST request to product-service to restock the product with some value.
  6. 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.

Peculiarities

  1. 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.
  2. 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.

Play with the demo

You should have Java 11, Git, Maven, Docker and Docker Compose be installed.

  1. Clone this project and cd to its folder
  2. Build the project with maven:
mvn package
  1. Move to demo folder
cd demo
  1. Run the environment:
docker-compose up -d --build
  1. Open browser on http://localhost:8080 to see the statistics: demo
  2. Stop the environment:
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

sb-cloud-stream-demo's People

Contributors

cepr0 avatar

Watchers

 avatar  avatar

Forkers

heidsoft

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.