Skip to content

uuhnaut69/saga-pattern-microservices

Repository files navigation

Saga Pattern Microservices

CircleCI Maven Central Docker Image Version (tag latest semver)

Simple order flow to demo some concepts:

  • Microservices using Spring Boot, Spring Cloud, Spring Cloud Stream
  • Database per service using Postgresql
  • Saga Pattern (Saga Orchestration): Distributed transaction span multiple services.
  • Outbox Pattern: Avoid dual-writes, no 2PC required using Kafka,Kafka Connect, Debezium , Outbox Event Router

Banner

Prerequisites

  • Java 21
  • Docker
  • Docker-compose

Get Started

Setup environment

AMD

export PLATFORM=linux/amd64 && docker-compose up -d

ARM

export PLATFORM=linux/arm64 && docker-compose up -d

Build projects

./mvnw clean package -DskipTests=true

Start Services

Run services api-gateway, order-service, customer-service, inventory-service

./mvnw -f api-gateway/pom.xml spring-boot:run
./mvnw -f order-service/pom.xml spring-boot:run
./mvnw -f customer-service/pom.xml spring-boot:run
./mvnw -f inventory-service/pom.xml spring-boot:run
Service's name Endpoint
Api Gateway localhost:8080
Order service localhost:9090
Customer service localhost:9091
Inventory service localhost:9093

Start outbox connectors

Create outbox connectors

sh register-connectors.sh

Clean Up

Delete all connectors

sh delete-connectors.sh

Useful commands

Create new customer

curl --location 'localhost:8080/customer-service/customers' \
--header 'Content-Type: application/json' \
--data '{
    "username": "usertest",
    "fullName": "Test user 1",
    "balance": 100
}'

Create new product

curl --location 'localhost:8080/inventory-service/products' \
--header 'Content-Type: application/json' \
--data '{
    "name": "Test product 1",
    "stocks": 10
}'

Create new order

curl --location 'localhost:8080/order-service/orders' \
--header 'Content-Type: application/json' \
--data '{
    "customerId": "f0a000ad-ea44-4fa1-853b-207f9d9abe2c",
    "productId": "d212c9d6-da3b-4fe2-b016-a852c8a8d32f",
    "quantity": 1,
    "price": 2
}'

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT