This project is a B2B (Business-to-Business) shopping cart application built as a Spring Boot microservice designed to be deployed in Kubernetes (K8s). The application provides a robust backend for managing products, categories, users, orders, and authentication in a B2B e-commerce context. The application implements RESTful APIs for various business operations and leverages multiple technologies for persistence, caching, and security.
- Java 8+ - Core programming language
- Spring Boot 2.1.4 - Primary application framework
- Spring Security OAuth2 - Authentication and authorization
- Spring Data JPA - Data access layer abstraction
- Spring Data MongoDB - Document-based NoSQL storage
- Spring Data Redis - Caching and messaging
- PostgreSQL - Primary relational database
- MongoDB - Document database for file storage and flexible data
- Redis - Cache and message broker
- JWT (JSON Web Tokens) - Secure token-based authentication
- Swagger/OpenAPI - API documentation
- Docker - Containerization
- Kubernetes - Container orchestration
- ModelMapper - Simplifies object mapping between DTOs and entities
- Lombok - Reduces boilerplate code
- Jackson - JSON processing
- Hibernate - ORM implementation
- Log4j2 - Logging
- Sentry - Error tracking
- EasyRandom - Test data generation
The application follows a layered architecture typical of Spring Boot applications, with clear separation of concerns:
- Controller Layer - REST endpoints that handle HTTP requests/responses
- Service Layer - Business logic implementation
- Repository Layer - Data access interfaces
- Model Layer - Domain objects/entities
The application also implements a generic CRUD pattern through base classes that most controllers and services extend, providing consistent API behavior and reducing duplicated code.
The application is designed as a microservice with the following characteristics:
- Stateless Design - No session state is stored in the application
- API-First - Well-defined REST APIs for all operations
- Externalized Configuration - Environment variables for configuration
- Health Monitoring - Actuator endpoints for health checks
- Docker Integration - Containerized deployment
- Kubernetes Readiness - Liveness/readiness probes, resource limits
The main entities in the system include:
- User - Authentication and user information
- Role - User permissions
- Item - Products in the system
- Category - Product categorization
- Invoice - Order information
- ItemsInvoices - Order line items
- State - Workflow states for orders/items
- FileStorage - Files stored in MongoDB (GridFS)
- FileItem - Product images/attachments
The application implements OAuth2 with JWT tokens for authentication and authorization:
- JWT-Based Authentication - Secure, stateless authentication
- Role-Based Authorization - ADMIN and USER roles with different permissions
- Method-Level Security - Secured annotations at service/controller level
- Password Encryption - BCrypt password encoding
- CORS Configuration - Secure cross-origin resource sharing
The application is designed for Kubernetes deployment with:
- Containerization - Docker image building via Maven plugins
- Horizontal Pod Autoscaling - Scale based on CPU usage
- Resource Limits - Memory and CPU constraints
- Health Probes - Liveness and readiness checks
- ConfigMaps - Environment configuration
- Services - LoadBalancer for external access
- Multi-Component Deployment - Separate deployments for app, PostgreSQL, MongoDB, and Redis
- Backend Application (StatefulSet/Deployment) - The Spring Boot application
- PostgreSQL (StatefulSet) - Relational database with persistent volume
- MongoDB (StatefulSet) - Document database with persistent volume
- Redis (StatefulSet) - Cache and message broker
- Ingress - External access configuration with TLS
- Authentication and Authorization - OAuth2/JWT-based security
- Product Management - CRUD operations for products/items
- Category Management - Hierarchical product categorization
- Order Processing - Invoice creation and management
- File Storage - File upload/download using MongoDB GridFS
- Workflow Management - State transitions with rules
- Caching - Redis caching for performance
- Message Processing - Redis pub/sub for asynchronous operations
- API Documentation - Swagger UI
- Redis Caching - Configured for high-performance data access
- Connection Pooling - Database connection optimization
- Pagination - All list endpoints support pagination
- Compression - HTTP response compression enabled
- Asynchronous Processing - Message-based processing for non-critical operations
- Kubernetes Autoscaling - Automatically scale based on load
- Actuator Endpoints - Health, metrics, and info endpoints
- Kubernetes Probes - Liveness and readiness for health monitoring
- Sentry Integration - Error tracking and reporting
- Logging - Structured logging with Log4j2
- Unit Tests - Service and controller tests
- Integration Tests - Repository and API tests
- Mock Testing - Using Mockito and PowerMockito
- Test Data Generation - Using EasyRandom
- Test Coverage - Cobertura for coverage reporting
sequenceDiagram
participant Client
participant API Gateway
participant AuthService as Authentication Service
participant CartService as B2B Cart Service
participant PostgreSQL
participant MongoDB
participant Redis
participant NotificationService
Client->>API Gateway: Request Authentication
API Gateway->>AuthService: Forward Auth Request
AuthService->>PostgreSQL: Validate Credentials
PostgreSQL-->>AuthService: User Data
AuthService-->>Client: JWT Token
Client->>API Gateway: Browse Products (with Token)
API Gateway->>CartService: Forward Request
CartService->>Redis: Check Cache
Redis-->>CartService: Cache Miss
CartService->>PostgreSQL: Query Products
PostgreSQL-->>CartService: Product Data
CartService->>Redis: Update Cache
CartService-->>Client: Product List
Client->>API Gateway: Add Items to Cart
API Gateway->>CartService: Forward Request
CartService->>Redis: Save Cart State
Redis-->>CartService: Confirmation
CartService-->>Client: Updated Cart
Client->>API Gateway: Place Order
API Gateway->>CartService: Forward Request
CartService->>PostgreSQL: Save Order
CartService->>MongoDB: Save Order Documents
CartService->>Redis: Publish Order Event
Redis-->>NotificationService: Consume Order Event
NotificationService->>Client: Order Confirmation
CartService-->>Client: Order Confirmation
The project uses GitHub Actions for CI/CD with the following flow:
- Code Push to GitHub
- Automated Testing
- Docker Image Building
- Docker Image Publishing
- Kubernetes Deployment
- Microservices Decomposition - Consider breaking into smaller, domain-focused services
- Event Sourcing - Implement event sourcing for better auditability
- API Gateway - Add an API gateway for routing and cross-cutting concerns
- Service Mesh - Consider Istio for advanced networking features
- Distributed Tracing - Add OpenTelemetry for request tracing
- Reactive Programming - Consider Spring WebFlux for reactive APIs
- Schema Versioning - Implement formal API versioning strategy
- Chaos Testing - Implement resilience testing in Kubernetes
This B2B shopping cart application demonstrates a well-structured Spring Boot microservice designed for Kubernetes deployment. It incorporates best practices in security, scalability, and cloud-native architecture, making it suitable for production deployment in a business e-commerce context.
The application leverages multiple persistence technologies (PostgreSQL, MongoDB, Redis) for their respective strengths, implements robust security with OAuth2/JWT, and includes comprehensive API documentation with Swagger. The Kubernetes deployment configuration ensures the application can scale horizontally based on demand and provides health monitoring for robust operations.