SmartCart is a scalable, cloud-native e-commerce backend system built with modern microservices architecture. This project demonstrates enterprise-grade patterns including event-driven communication, distributed tracing, circuit breakers, and comprehensive observability.
- Microservices Architecture: Demonstrate loosely coupled, independently deployable services
- Event-Driven Communication: Leverage Apache Kafka for asynchronous messaging
- Observability: Implement distributed tracing, metrics, and logging
- Resilience: Circuit breakers, retries, and fault tolerance patterns
- Security: OAuth2/OIDC integration with Keycloak
- DevOps Ready: Docker containerization and infrastructure as code
Service | Port | Database | Purpose |
---|---|---|---|
API Gateway | 9000 | - | Request routing, authentication, rate limiting |
Product Service | 8080 | MongoDB | Product catalog management |
Order Service | 8081 | MySQL | Order processing and management |
Inventory Service | 8082 | MySQL | Stock management and availability |
Notification Service | 8083 | - | Email notifications via Kafka events |
Frontend | 4200 | - | Angular-based user interface |
Component | Port | Purpose |
---|---|---|
Keycloak | 8181 | Identity and Access Management |
Kafka Broker | 9092 | Message streaming platform |
Schema Registry | 8085 | Avro schema management |
Kafka UI | 8086 | Kafka cluster management |
Prometheus | 9090 | Metrics collection |
Grafana | 3000 | Observability dashboards |
Loki | 3100 | Log aggregation |
Tempo | 3110 | Distributed tracing |
- Java 21 - Latest LTS version with modern language features
- Spring Boot 3.2.4 - Microservices framework
- Spring Cloud Gateway - API Gateway with routing and filters
- Spring Security - OAuth2 Resource Server
- Spring Data JPA - Database abstraction layer
- Apache Kafka - Event streaming platform
- Avro - Schema evolution for Kafka messages
- Resilience4J - Circuit breaker pattern implementation
- Flyway - Database migration tool
- Angular 18 - Modern web framework
- TypeScript - Type-safe JavaScript
- Tailwind CSS - Utility-first CSS framework
- Angular OIDC Client - OAuth2/OIDC integration
- MongoDB - Document database for product catalog
- MySQL 8 - Relational database for orders and inventory
- Docker & Docker Compose - Containerization
- Prometheus - Metrics collection and monitoring
- Grafana - Visualization and alerting
- Loki - Log aggregation system
- Tempo - Distributed tracing backend
- OpenTelemetry - Observability instrumentation
- Keycloak - Identity and Access Management
- OAuth2/OIDC - Modern authentication standards
- JWT Tokens - Stateless authentication
- Centralized entry point for all client requests
- Cross-cutting concerns: authentication, rate limiting, routing
- Circuit breaker integration for downstream service protection
- Asynchronous communication via Apache Kafka
- Order placement triggers inventory checks and notifications
- Schema Registry ensures message compatibility
- Each microservice owns its data
- MongoDB for document-based product catalog
- MySQL for transactional order and inventory data
- Resilience4J implementation
- Prevents cascade failures
- Configurable fallback mechanisms
- OpenTelemetry integration
- Request correlation across services
- Performance bottleneck identification
-
Authentication Flow:
- Frontend redirects to Keycloak for login
- JWT tokens issued upon successful authentication
- Tokens validated by API Gateway
-
Authorization:
- Role-based access control (RBAC)
- Scope-based API access
- Token propagation to downstream services
# Keycloak Realm Configuration
Realm: spring-microservices-security-realm
Client ID: microservices-client
Valid Redirect URIs: http://localhost:4200/*
- Application Metrics: Request rate, response time, error rate
- Business Metrics: Orders placed, products viewed, inventory levels
- Infrastructure Metrics: CPU, memory, database connections
- Kafka Metrics: Message throughput, consumer lag, topic partitions
- End-to-end request tracing across all services
- Performance optimization insights
- Error root cause analysis
- Order Placed → Kafka Topic
order-placed
- Notification Service → Consumes order events
- Email Notification → Sent to customer
Each service includes optimized Dockerfile with:
- Multi-stage builds for smaller images
- Non-root user execution
- Health check endpoints
- Graceful shutdown handling
- Unit Tests: JUnit 5, Mockito
- Integration Tests: TestContainers for database testing
- Contract Tests: API contract validation
- End-to-End Tests: REST Assured for API testing
@Testcontainers
class OrderServiceIntegrationTest {
@Container
static MySQLContainer<?> mysql = new MySQLContainer<>("mysql:8.3.0");
@Container
static KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.5.0"));
}
name: Microservices CI/CD
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
- name: Run Tests
run: ./mvnw test
- name: Build Docker Images
run: docker-compose build
- Helm charts for service deployment
- Horizontal Pod Autoscaling (HPA)
- Service mesh integration (Istio)
- External secrets management
- Development: Local Docker Compose
- Staging: Kubernetes cluster with external databases
- Production: Cloud-native services (AWS RDS, MSK, etc.)
Access comprehensive API documentation:
- Aggregated Docs: http://localhost:9000/swagger-ui.html
- Product Service: http://localhost:8080/swagger-ui.html
- Order Service: http://localhost:8081/swagger-ui.html
- Inventory Service: http://localhost:8082/swagger-ui.html
GET /api/products # Get all products
POST /api/products # Create new product
GET /api/products/{id} # Get product by ID
POST /api/orders # Place new order
GET /api/orders/{id} # Get order details
GET /api/inventory/{skuCode}/status # Check inventory status
- IntelliJ IDEA with Spring Boot plugin
- VS Code with Java Extension Pack
- Docker Desktop for container management
- SonarQube integration for code analysis
- Checkstyle for coding standards
- SpotBugs for bug detection
- Fork the repository
- Create feature branch:
git checkout -b feature/amazing-feature
- Commit changes:
git commit -m 'Add amazing feature'
- Push to branch:
git push origin feature/amazing-feature
- Open Pull Request
- Follow Java coding conventions
- Write comprehensive tests
- Update documentation
- Use conventional commit messages
# Check port usage
netstat -ano | findstr :8080
# Kill process if needed
taskkill /PID <process_id> /F
# Verify MySQL containers are running
docker ps | grep mysql
# Check logs
docker logs inventory-service-mysql
# Verify Kafka cluster health
docker exec -it broker kafka-topics --bootstrap-server localhost:9092 --list
This project is licensed under the MIT License - see the LICENSE file for details.
- Spring Boot Team for the excellent framework
- Apache Kafka community for event streaming platform
- Grafana Labs for observability tools
- Keycloak team for identity management
- TestContainers for integration testing
- GitHub Issues: Report bugs or request features
- Author: Chanuth Silva
- Email: [Your Email]
⭐ Star this repository if you find it helpful for learning microservices architecture!
Ensure you have the following installed on your system:
- Java 21 or later
- Node.js 18+ and npm
- Docker and Docker Compose
- Git
git clone https://github.com/Chanuth-silva10/microservices-springboot-kafka-grafana.git
cd microservices-springboot-kafka-grafana
Each service has its own Docker Compose file for modular deployment:
# Start API Gateway with observability stack
cd api-gateway
docker-compose up -d
cd ..
# Start Order Service with Kafka ecosystem
cd order-service
docker-compose up -d
cd ..
# Start Inventory Service with MySQL
cd inventory-service
docker-compose up -d
cd ..
# Start Product Service with MongoDB
cd product-service
docker-compose up -d
cd ..
# Build all services
./mvnw clean package -DskipTests
# Run each service (in separate terminals)
cd api-gateway && ./mvnw spring-boot:run
cd product-service && ./mvnw spring-boot:run
cd order-service && ./mvnw spring-boot:run
cd inventory-service && ./mvnw spring-boot:run
cd notification-service && ./mvnw spring-boot:run
cd frontend
npm install
npm start
The application will be available at:
- Frontend: http://localhost:4200
- API Gateway: http://localhost:9000
- Swagger UI: http://localhost:9000/swagger-ui.html
Key configuration properties can be found in each service's application.properties
:
server.port=9000
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8181/realms/spring-microservices-security-realm
product.service.url=http://localhost:8080
order.service.url=http://localhost:8081
inventory.service.url=http://localhost:8082
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.producer.properties.schema.registry.url=http://127.0.0.1:8085
- MongoDB (Product Service):
mongodb://localhost:27017/product-service
- MySQL (Order Service):
jdbc:mysql://localhost:3307/order_service
- MySQL (Inventory Service):
jdbc:mysql://localhost:3316/inventory_service
Access the observability stack:
- Grafana Dashboards: http://localhost:3000
- Prometheus Metrics: http://localhost:9090
- Kafka UI: http://localhost:8086
- Keycloak Admin: http://localhost:8181 (admin/admin)