Microservices Architecture Documentation
Microservices Architecture Documentation
Documentation
Project Overview
Purpose
System Scope
Architectural Principles
● Separation of concerns
● High availability
● Scalability
● Performance optimization
● Robust security implementation
Technology Stack
Documentation Purpose
# Microservices Documentation
## Overview
This project consists of two interconnected microservices:
1. Product Service (ProductService1)
2. User Service (Authentication Service)
## Technology Stack
- **Framework**: Spring Boot 3.4.0
- **Language**: Java 17
- **Primary Database**: MySQL (AWS RDS)
- **Cache**: Redis (AWS ElastiCache)
- **Message Broker**: Apache Kafka
- **Build Tool**: Maven
- **Database Migration**: Flyway
- **Containerization**: Docker
- **Container Orchestration**: Kubernetes
- **Cloud Platform**: AWS
## Cloud Architecture
spring.datasource.url=jdbc:mysql://database-1.cro2a6y8o2c2.us-east-1.rd
s.amazonaws.com/productservicnov24batch
```
4. **AWS ECS/EKS**
- Container orchestration
- Auto-scaling
- Load balancing
#### 1. Models
- **BaseModel**
- Abstract base class with common fields (id, title)
- Used as a parent class for other entities
- **Product**
- Extends BaseModel
- Fields:
- description (String)
- price (Double)
- quantity (int)
- category (Many-to-One relationship)
- **Category**
- Extends BaseModel
- Fields:
- description (String)
- One-to-Many relationship with Products
#### 2. Controllers
- **ProductController** (`/Products` endpoint)
- `GET /Products/{id}`: Retrieve product by ID
- `GET /Products`: Get all products
- `POST /Products`: Create new product
- `PUT /Products/{id}`: Update existing product
#### 3. Services
- **ProductService** (Interface)
- Defines core product operations
- **FakeStoreProductService**
- Implementation using FakeStore API
- External API integration using RestTemplate
- Handles product data transformation
- **SelfProductService**
- Primary implementation (@Primary)
- Handles database operations using JPA repositories
- Implements product CRUD operations
- **ExceptionHandler**
- Global exception handling using @RestControllerAdvice
- Standardized error responses
- **User Types**
- Student (DiscriminatorValue="1")
- Mentor (DiscriminatorValue="2")
- Instructor
### DTOs
- **UserDto**
- name
- email
- roleDtoList
- **RoleDto**
- value (String)
## Development Guidelines
## Future Enhancements
1. Implement OAuth2 resource server (commented dependency available)
2. Add comprehensive test coverage
3. Implement caching mechanisms
4. Add API rate limiting
5. Implement circuit breakers for service communication
## Testing
- JUnit test cases available
- MockMvc for controller testing
- Mockito for service layer testing
## Deployment
The services are configured to run on different ports:
- Product Service: 9090
- User Service: 8080 (default)
## Dependencies
Major dependencies include:
- spring-boot-starter-web
- spring-boot-starter-data-jpa
- spring-boot-starter-data-jdbc
- mysql-connector-j
- flyway-core
- flyway-mysql
- lombok (for development)
## Docker Configuration
### Dockerfile
```dockerfile
FROM openjdk:17-jdk-slim
WORKDIR /app
COPY target/*.jar app.jar
EXPOSE 9090
ENTRYPOINT ["java","-jar","app.jar"]
```
kafka:
image: confluentinc/cp-kafka:latest
environment:
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
```
## Kubernetes Deployment
#### 1. Deployment
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: product-service
spec:
replicas: 1
selector:
matchLabels:
app: product-service
template:
metadata:
labels:
app: product-service
spec:
containers:
- name: product-service
image: product-service:latest
ports:
- containerPort: 9090
env:
- name: SPRING_PROFILES_ACTIVE
value: "prod"
resources:
requests:
memory: "512Mi"
cpu: "500m"
limits:
memory: "1Gi"
cpu: "1000m"
```
#### 2. Service
```yaml
apiVersion: v1
kind: Service
metadata:
name: product-service
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 9090
selector:
app: product-service
```
#### 3. HorizontalPodAutoscaler
```yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: product-service-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: product-service
minReplicas: 3
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80
```
## Scalability Patterns
### 1. Circuit Breaker Pattern
```java
@CircuitBreaker(name = "productService", fallbackMethod =
"fallbackMethod")
public Product getProductById(Long id) {
// Service call
}
```
### 1. Database
- Multi-AZ RDS deployment
- Read replicas for read scaling
- Automated failover
### 2. Cache
- Redis cluster with replicas
- Cross-AZ deployment
- Automatic failover
### 3. Kafka
- Multi-broker setup
- Topic replication
- Partition distribution
## Performance Optimization
## CI/CD Pipeline
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build and Test
run: mvn clean package
- name: Build Docker image
run: docker build -t product-service .
- name: Deploy to EKS
run: kubectl apply -f k8s/
```
## Disaster Recovery
## Security Measures