System Architecture
The service follows a layered architecture with clear separation of concerns:Domain-Driven Design
Aggregates
The Order aggregate is the core domain entity that encapsulates all business logic related to orders:Order.java
The Order aggregate maintains consistency by encapsulating all business rules. All modifications to an order must go through its public methods, which enforce invariants.
Value Objects
- OrderItem - Represents a line item with product ID, count, and price
- Address - Represents delivery address with province, city, and detail
- OrderStatus - Enum with CREATED and PAID states
Factories
The OrderFactory creates new Order instances and generates unique order IDs:Repositories
OrderRepository provides persistence abstraction using Spring Data:save(Order order)- Persists order to MySQLfindById(String id)- Retrieves order by IDfindAll(Pageable pageable)- Paginated order listing
Event-Driven Architecture
The service publishes domain events to RabbitMQ whenever important state changes occur:Order Created
Published when a new order is created
Order Paid
Published when payment is processed
Product Changed
Published when product quantities change
Address Changed
Published when delivery address changes
CQRS Pattern
The system implements Command Query Responsibility Segregation:- Commands modify order state (CreateOrderCommand, PayOrderCommand, etc.)
- Queries use optimized read models (OrderRepresentation, OrderSummaryRepresentation)
- Events synchronize read models asynchronously via RabbitMQ
Technology Stack
Spring Boot 2.1.4
Application framework with dependency injection
MySQL
Primary data store for order persistence
RabbitMQ
Message broker for event publishing
Zipkin
Distributed tracing for observability
Gradle
Build automation and dependency management
Lombok
Reduces boilerplate with annotations
Component Structure
The project is organized into two modules:ecommerce-order-service-api
The main application module containing:- Controllers - REST API endpoints (
OrderController,AboutController) - Application Services - Orchestration layer (
OrderApplicationService) - Domain Model - Core business logic (
Order,OrderItem,OrderStatus) - Repositories - Data access (
OrderRepository) - Configuration - RabbitMQ, database, Spring configuration
ecommerce-order-service-sdk
Published SDK for integration containing:- Commands - Request DTOs for API endpoints
- Events - Domain events published to RabbitMQ
- Representations - Response DTOs for API queries
Design Principles
- Encapsulation - Business logic is encapsulated in the domain model
- Invariants - The Order aggregate enforces all business rules
- Immutability - Events and representations are immutable
- Separation of Concerns - Clear boundaries between layers
- Testability - Multi-level testing strategy (unit, component, API)