A modern, scalable microservices architecture built with Spring Boot 3.x/4.x, Spring Cloud, and PostgreSQL. This system demonstrates enterprise-grade patterns including service discovery, API gateway, centralized configuration, JWT authentication, and custom exception handling through a shared common library.
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β API Gateway β β Eureka Server β β Config Server β
β :8080 β β :8761 β β :8888 β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β β
βββββββββββββββββββββββββΌββββββββββββββββββββββββ
β
βββββββββββββββββββββββββΌββββββββββββββββββββββββ
β β β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β AUTH Service β β EMPLOYEE Serviceβ β ADDRESS Service β
β :8084 β β :8081 β β :8082 β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β β
βββββββββββββββββββββββββΌββββββββββββββββββββββββ
β
βββββββββββββββββββ
β PostgreSQL β
β :5432 β
βββββββββββββββββββ
| Service | Port | Description | Technology |
|---|---|---|---|
| API Gateway | 8080 | Single entry point, routing & authentication | Spring Cloud Gateway |
| Eureka Server | 8761 | Service discovery & registration | Netflix Eureka |
| Config Server | 8888 | Centralized configuration management | Spring Cloud Config |
| Auth Service | 8084 | User authentication & JWT token generation | Spring Security + JWT |
| Employee Service | 8081 | Employee management operations | Spring Boot + JPA |
| Address Service | 8082 | Address management for employees | Spring Boot + JPA |
- Framework: Spring Boot 4.0.5, Spring Cloud 2025.1.1
- Language: Java 21
- Database: PostgreSQL
- Security: JWT Authentication, Spring Security
- Service Discovery: Netflix Eureka
- API Gateway: Spring Cloud Gateway
- Circuit Breaker: Resilience4j
- Configuration: Spring Cloud Config
- Build Tool: Maven
- Custom Library:
common-libfor centralized exception handling
This project includes a custom common-lib library that provides centralized exception handling and reusable components across all microservices.
- Centralized Exception Handling:
GlobalExceptionHandlerbase class - Custom Exceptions: Pre-built exception types for common scenarios
- Standardized Error Responses: Consistent error format across services
- Reusable Components: Common utilities and patterns
// Base exception classes provided by common-lib
- BadRequestException // HTTP 400 - Invalid request data
- MissingParameterException // HTTP 400 - Required parameters missing
- CustomException // Configurable HTTP status codesEach microservice extends the GlobalExceptionHandler from common-lib:
@RestControllerAdvice
public class GlobalStatusHandler extends GlobalExceptionHandler {
// Service-specific exception handling can be added here
}<dependency>
<groupId>com.commonlib</groupId>
<artifactId>common-lib</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>- Java 21
- Maven 3.6+
- PostgreSQL 12+
- Git
CREATE DATABASE employee;
CREATE USER root WITH PASSWORD 'test';
GRANT ALL PRIVILEGES ON DATABASE employee TO root;-
Clone the repository
git clone <repository-url> cd microservices-system
-
Start services in order:
# Terminal 1 - Eureka Server cd EUREKA-SERVER mvn spring-boot:run # Terminal 2 - Config Server cd CONFIG-SERVER mvn spring-boot:run # Terminal 3 - Auth Service cd AUTH mvn spring-boot:run # Terminal 4 - Employee Service cd EMPLOYEE mvn spring-boot:run # Terminal 5 - Address Service cd ADDRESS mvn spring-boot:run # Terminal 6 - API Gateway cd API-GATEWAY mvn spring-boot:run
POST /auth/register-user- Register new userPOST /auth/generate-token- Generate JWT token
POST /employees/save- Create new employeeGET /employees/- Get all employeesGET /employees/{id}- Get employee by IDPUT /employees/update/{id}- Update employeeDELETE /employees/delete/{id}- Delete employeeGET /employees/get-by-emp-code-and-company-name- Search by code and company
POST /addresses/save- Create address(es)GET /addresses/all-address- Get all addressesGET /addresses/{addressId}- Get address by IDGET /addresses/empId/{empId}- Get addresses by employee IDPUT /addresses/update- Update address(es)DELETE /addresses/delete/{addressId}- Delete address
- URL:
http://localhost:8761 - View all registered services and their health status
# Gateway Health
GET http://localhost:8080/actuator/health
# Circuit Breaker Status
GET http://localhost:8080/actuator/circuitbreakers
# Service Metrics
GET http://localhost:8080/actuator/metrics- JWT Authentication: Stateless token-based authentication
- API Gateway Security: Centralized authentication filter
- Circuit Breaker: Fault tolerance with Resilience4j
- Custom Exception Handling: Centralized error responses via common-lib
The system implements circuit breakers for fault tolerance:
- Employee Service: 5s timeout, 50% failure rate threshold
- Address Service: 5s timeout, 50% failure rate threshold
Fallback Endpoints:
- Employee Service:
/employeeServiceFallback - Address Service:
/addressServiceFallback
The common-lib provides standardized error responses across all services:
{
"message": "Invalid Credentials",
"status": 400,
"timestamp": "2024-01-01T12:00:00Z",
"path": "/auth/generate-token"
}GlobalExceptionHandler (from common-lib)
βββ BadRequestException
βββ MissingParameterException
βββ CustomException (configurable)
- Database: PostgreSQL on localhost:5432
- Eureka: Service discovery on localhost:8761
- Config Server: Centralized config on localhost:8888
| Service | Port |
|---|---|
| API Gateway | 8080 |
| Auth Service | 8084 |
| Employee Service | 8081 |
| Address Service | 8082 |
| Eureka Server | 8761 |
| Config Server | 8888 |
microservices-system/
βββ API-GATEWAY/ # Spring Cloud Gateway
βββ AUTH/ # Authentication Service
βββ EMPLOYEE/ # Employee Management
βββ ADDRESS/ # Address Management
βββ EUREKA-SERVER/ # Service Discovery
βββ CONFIG-SERVER/ # Configuration Management
βββ common-lib/ # Shared Exception Library
- Use environment-specific configuration files
- Configure production database connections
- Enable HTTPS and secure JWT secrets
- Implement distributed tracing (Zipkin/Sleuth)
- Add application performance monitoring
- Configure centralized logging
- Containerize services with Docker
- Use Kubernetes for orchestration
- Configure load balancing for high availability
- Rotate JWT secrets regularly
- Implement rate limiting
- Add input validation and sanitization
- Configure CORS policies
- Follow Java coding conventions
- Write unit and integration tests
- Use SonarQube for code analysis
- Maintain API documentation
- Add reusable exceptions to
common-lib - Update library version when making changes
- Test library integration across services
- Document new exception types
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request