spring-boot-microservices

Sequence Diagram

Choreography-based Saga

sequenceDiagram
    participant Customer
    participant OrderService
    participant InventoryService
    participant PaymentService
    participant NotificationService

    Customer->>OrderService: Create Order
    OrderService->>OrderService: Emit OrderCreated Event
    OrderService->>Customer: Order Created

    OrderService-->>InventoryService: OrderCreated Event
    InventoryService->>InventoryService: Reserve Inventory
    InventoryService->>InventoryService: Emit InventoryReserved Event
    InventoryService-->>PaymentService: InventoryReserved Event

    PaymentService->>PaymentService: Process Payment
    PaymentService->>PaymentService: Emit PaymentProcessed Event
    PaymentService-->>NotificationService: PaymentProcessed Event

    NotificationService->>NotificationService: Send Confirmation Email
    NotificationService->>Customer: Order Confirmed

    Note right of OrderService: If any step fails

    InventoryService-->>OrderService: InventoryReserveFailed Event
    PaymentService-->>OrderService: PaymentFailed Event
    OrderService->>OrderService: Emit OrderFailed Event
    OrderService->>Customer: Order Failed
    OrderService-->>NotificationService: OrderFailed Event
    NotificationService->>NotificationService: Send Cancellation Email
    NotificationService->>Customer: Order Cancelled

    OrderService-->>InventoryService: OrderFailed Event
    InventoryService->>InventoryService: Compensate Inventory

    OrderService-->>PaymentService: OrderFailed Event
    PaymentService->>PaymentService: Compensate Payment

Loading

Orchestration-based Saga

sequenceDiagram
    participant Customer
    participant Orchestrator
    participant OrderService
    participant InventoryService
    participant PaymentService
    participant NotificationService

    Customer->>OrderService: Create Order
    OrderService->>Orchestrator: Order Created
    Orchestrator->>InventoryService: Reserve Inventory
    InventoryService->>Orchestrator: Inventory Reserved
    Orchestrator->>PaymentService: Process Payment
    PaymentService->>Orchestrator: Payment Processed
    Orchestrator->>OrderService: Confirm Order
    OrderService->>Customer: Order Confirmed
    Orchestrator->>NotificationService: Send Confirmation Email
    NotificationService->>Customer: Order Confirmation Sent

    Note right of Orchestrator: If any step fails

    Orchestrator->>PaymentService: Compensate Payment
    PaymentService->>Orchestrator: Payment Compensated
    Orchestrator->>InventoryService: Compensate Inventory
    InventoryService->>Orchestrator: Inventory Compensated
    Orchestrator->>OrderService: Cancel Order
    OrderService->>Customer: Order Cancelled
    Orchestrator->>NotificationService: Send Cancellation Email
    NotificationService->>Customer: Order Cancellation Sent


Loading

List what has been used

  • Spring Boot, web framework, makes it easy to create stand-alone, production-grade Spring based Applications
  • Apache Kafka, a distributed and fault-tolerant stream processing system used for event-driven communication between microservices.
  • Spring Cloud Netflix Eureka, a service discovery mechanism that allows microservices to find and communicate with each other without hard-coding the hostname and port.
  • Spring Cloud Gateway, an api gateway that provide a simple, yet effective way to route to APIs and provide cross-cutting concerns to them such as: security, monitoring/metrics, and resiliency.
  • Spring Data Redis, provides an implementation of Spring Framework’s Cache Abstraction.
  • Spring Integration, provides inbound and outbound channel adapters to support the Message Queueing Telemetry Transport (MQTT) protocol.
  • Resilience4j, a library that helps prevent cascading failures and provides mechanisms for graceful degradation and self-healing when external services experience issues.
  • Zipkin, a distributed tracing system that provides end-to-end visibility into how requests flow through the system, helping troubleshoot issues in distributed architectures.
  • Spring Cloud Sleuth, autoconfiguration for distributed tracing
  • Micrometer Tracing with Brave, library for distributed tracing (update to Spring Boot 3.x)
  • Docker and docker-compose, for containerization
  • Spring Data JPA provides repository support for the Jakarta Persistence API
  • Flywaydb for migrations
  • Keycloak for providing authentication, user management, fine-grained authorization
  • PostgreSQL

Prerequisite

  • Java 17
  • Maven
  • Docker
  • GNU Make
  • WSL (if using Windows)

Setup

.
├── spring-boot-microservices
├── discovery-server
├── api-gateway
├── user-service
├── product-service
├── order-service
├── payment-service
├── inventory-service
└── notification-service

Running the app

Adding to host file
127.0.0.1   keycloak
Run command in spring-boot-microservices directory
  • Docker environment
# Docker compose up
make up

# Docker compose down
make down
  • Non Docker / standalone environment
# Docker compose up
make local-up

# Start discovery-server, api-gateway, user-service

# Start microservice   

# Docker compose down
make local-down

Keycloak

Admin console
Non Docker / standalone environment: http://localhost:8090/

Docker environment: http://keycloak:8090/

username/password

admin/admin

img_5.png

Get access token
Non Docker / standalone environment: POST http://localhost:8090/realms/spring-boot-microservices-realm/protocol/openid-connect/token

Docker environment: POST http://keycloak:8090/realms/spring-boot-microservices-realm/protocol/openid-connect/token
#Basic Auth
Username="microservices-auth-client"
Password="123456789"
#form data
'grant_type="password"'
'scope="openid offline_access"'
'username="admin"'
'password="P@ssw0rd"'
  • Account
username/password

admin/P@ssw0rd
customer/P@ssw0rd
guest/P@ssw0rd
Swagger
http://localhost:8181/swagger-ui.html

img.png img_1.png

Zipkin
http://localhost:9411/

img_3.png

ActiveMQ
http://localhost:8161/

username/password

admin/admin

img_4.png

Apache Kafka
  • Using Offset Explorer - a GUI application for managing and using Apache Kafka clusters
Cluster name: spring-boot-microservices
Zookeeper Host: localhost
Zookeeper Port: 2181

Bootstrap servers: localhost:9092

img_2.png