A sophisticated, production-ready load balancer service built with Spring Boot that provides advanced request routing, load balancing strategies, and real-time monitoring with enterprise-grade reliability features.
- ๐ Quick Start
- โจ Key Features
- ๐๏ธ Architecture
- โ๏ธ Configuration
- ๐ Security
- ๐ Dashboard
- ๐ณ Deployment
- ๐งช Testing
- ๐ Performance
- ๐ง Troubleshooting
- Java 21+
- Gradle 8.14+
- Docker (optional)
git clone <repository>
cd router-service
./gradlew build./gradlew bootRun# Open login page
open http://localhost:8080/login.html
# Login with default credentials
# Username: admin
# Password: admin123
# Or access dashboard directly (will redirect to login)
open http://localhost:8080/dashboard.html# Generate test traffic
curl -H "Host: api.example.com" http://localhost:8080/v1/users
curl -H "Host: api.example.com" http://localhost:8080/v1/orders- Username:
admin - Password:
admin123
- Advanced Routing: Regex-based host and path pattern matching
- Load Balancing: Round Robin, Random, Least Connections, Weighted Round Robin
- Health Checks: Automatic backend health monitoring
- Request Proxying: Full HTTP request/response forwarding
- Real-time Metrics: WebSocket-based metrics streaming
- Configuration Management: JSON-based routing configuration
- Circuit Breaker Pattern: Automatic failure detection and recovery
- Rate Limiting: Token bucket algorithm for request throttling
- Retry Logic: Configurable retry policies with exponential backoff
- Graceful Degradation: Fallback mechanisms for service failures
- Professional Dashboard: Modern, responsive web interface
- Real-time Metrics: Request counts, latency percentiles, error rates
- Health Status: Backend availability monitoring
- Circuit Breaker Status: State monitoring and manual reset
- WebSocket Streaming: Live metrics via WebSocket connection
- Prometheus Integration: Metrics export for monitoring systems
- Authentication: Basic authentication for dashboard access
- Role-Based Access: ADMIN role required for management features
- Session Management: Persistent login with secure logout
- Security Headers: HSTS, frame options, content type protection
- Input Validation: Comprehensive request validation
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ HTTP Client โโโโโถโ Router Service โโโโโถโ Backend 1 โ
โโโโโโโโโโโโโโโโโโโ โ โ โโโโโโโโโโโโโโโโโโโ
โ โโโโโโโโโโโโโโโ โ
โ โRate Limiter โ โ โโโโโโโโโโโโโโโโโโโ
โ โโโโโโโโโโโโโโโ โโโโโถโ Backend 2 โ
โ โโโโโโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโ
โ โCircuit โ โ
โ โBreaker โ โ โโโโโโโโโโโโโโโโโโโ
โ โโโโโโโโโโโโโโโ โโโโโถโ Backend 3 โ
โ โโโโโโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโ
โ โHealth Check โ โ
โ โโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโ
Create a config/routes.json file:
[
{
"hostPattern": "api\\.example\\.com",
"pathPattern": "/v1/users(/.*)?",
"backends": [
"http://10.0.0.5:8080",
"http://10.0.0.6:8080"
],
"loadBalancingStrategy": "ROUND_ROBIN",
"timeoutSeconds": 30,
"healthCheckEnabled": true,
"healthCheckPath": "/health",
"circuitBreakerEnabled": true,
"circuitBreakerFailureThreshold": 5,
"circuitBreakerResetTimeoutSeconds": 60,
"rateLimitingEnabled": true,
"rateLimitMaxTokens": 100,
"rateLimitTokensPerSecond": 10.0,
"retryEnabled": true,
"maxRetries": 3,
"retryDelayMs": 1000
}
]# Application Configuration
spring.application.name=router-service
server.port=8080
# Router Configuration
router.config.path=config/routes.json
router.health-check.interval=30000
router.health-check.timeout=5000
# Circuit Breaker Configuration
router.circuit-breaker.default-failure-threshold=5
router.circuit-breaker.default-reset-timeout=60000
# Rate Limiting Configuration
router.rate-limit.default-max-tokens=100
router.rate-limit.default-tokens-per-second=10.0
# Logging Configuration
logging.level.com.example.router=INFO
logging.file.name=logs/router-service.log
logging.file.max-size=100MB
logging.file.max-history=30
# Actuator Configuration
management.endpoints.web.exposure.include=health,info,metrics,prometheus
management.metrics.export.prometheus.enabled=trueThe router service implements multi-layered security with:
- Basic Authentication: Username/password with BCrypt encoding
- Role-Based Access: ADMIN role required for management features
- Session Management: Persistent login with secure logout
- Protected Endpoints: Dashboard, management API, WebSocket, actuator
# Default credentials
spring.security.user.name=admin
spring.security.user.password=admin123
# Dashboard security
dashboard.security.enabled=true
dashboard.security.allowed-origins=*
# Session management
server.servlet.session.timeout=3600s
server.servlet.session.cookie.http-only=true| Resource | Authentication | Authorization | Description |
|---|---|---|---|
/dashboard.html |
โ Required | ADMIN Role | Main dashboard interface |
/api/management/** |
โ Required | ADMIN Role | Management API endpoints |
/ws/metrics |
โ Required | ADMIN Role | WebSocket metrics stream |
/actuator/** |
โ Required | ADMIN Role | Application monitoring |
/login.html |
โ Public | None | Login page |
/actuator/health |
โ Public | None | Health check endpoint |
/v1/**, /api/** |
โ Public | None | Router endpoints |
X-Frame-Options: DENY # Clickjacking protection
X-Content-Type-Options: nosniff # MIME type sniffing protection
Strict-Transport-Security: max-age=31536000 # HSTS for HTTPS enforcement- Login Page:
http://localhost:8080/login.html๐ - Dashboard:
http://localhost:8080/dashboard.html๐จ - Default Credentials:
admin/admin123
- Latency Trends Chart: Time-series latency data with multiple rule tracking
- Request Volume Chart: Bar chart showing request counts over time
- Interactive Legends: Click to show/hide data series
- Responsive Design: Works on desktop, tablet, and mobile
- Total Requests: Real-time request count across all rules
- Average Latency: Response time metrics with P95/P99 percentiles
- Error Rate: Percentage of failed requests
- System Status: Backend health, circuit breakers, rate limiters
- Auto-refresh: Configurable automatic updates (5-second intervals)
- Manual Refresh: Instant data refresh button
- Circuit Breaker Reset: Bulk and individual reset functionality
- Connection Status: WebSocket connection indicator
- Routing Rules Table: All configured rules with status indicators
- Circuit Breakers Table: Backend failure states with reset actions
- Status Badges: Color-coded indicators (success/warning/danger)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๐ Router Service Dashboard โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Service: Online | Backends: 3/5 Healthy | CB: Normal โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ [Refresh] [Reset CB] [Auto Refresh] โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ Performance Metrics โ ๐ฅ System Status โ
โ Total Requests: 1,234 โ Healthy Backends: 3 โ
โ Avg Latency: 45.2ms โ Open Circuit Breakers: 0 โ
โ P95 Latency: 120.5ms โ Active Rate Limiters: 5 โ
โ Error Rate: 0.1% โ Total Rules: 6 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ Latency Trends โ ๐ Request Volume โ
โ [Interactive Chart] โ [Bar Chart] โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ ๐ Routing Rules โ ๐ก๏ธ Circuit Breakers โ
โ [Data Table] โ [Data Table with Actions] โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# Build Docker image
docker build -t router-service .
# Run with Docker
docker run -p 8080:8080 -v $(pwd)/config:/app/config router-service# Start full stack with monitoring
docker-compose up -d
# Access services
# Router Service: http://localhost:8080
# Prometheus: http://localhost:9090
# Grafana: http://localhost:3000 (admin/admin)# Required for production
export ADMIN_USERNAME=your_admin_username
export ADMIN_PASSWORD=your_secure_password
export DASHBOARD_SECURITY_ENABLED=true
# Optional security enhancements
export SERVER_SSL_ENABLED=true
export SERVER_SSL_KEY_STORE=/path/to/keystore.p12
export SERVER_SSL_KEY_STORE_PASSWORD=your_keystore_password# Production JVM options
JAVA_OPTS="-Xms4g -Xmx8g \
-XX:+UseG1GC \
-XX:MaxGCPauseMillis=200 \
-XX:+UseStringDeduplication \
-XX:+UseContainerSupport \
-XX:MaxRAMPercentage=75.0"./gradlew test- Load balancing strategies
- Circuit breaker functionality
- Rate limiting algorithms
- String parsing and pattern matching
- Health check functionality
- Request routing and proxying
- Management API endpoints
- Retry logic and error handling
# Start test backends
docker-compose up backend1 backend2 backend3
# Run integration tests
./gradlew integrationTestFor handling 1 million requests, use the high-performance configuration:
{
"rateLimitMaxTokens": 50000,
"rateLimitTokensPerSecond": 5000.0,
"timeoutSeconds": 10,
"retryDelayMs": 100,
"maxRetries": 2
}# Production JVM options
JAVA_OPTS="-Xms4g -Xmx8g -XX:+UseG1GC -XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0"# High-performance server settings
server.tomcat.threads.max=1000
server.tomcat.threads.min-spare=100
server.tomcat.max-connections=10000
server.tomcat.accept-count=500# Install wrk (load testing tool)
brew install wrk # macOS
# or
sudo apt-get install wrk # Ubuntu
# Run load test
wrk -t12 -c400 -d30s http://localhost:8080/v1/users# Check if service is running
curl http://localhost:8080/api/management/health
# Verify dashboard file exists
ls -la src/main/resources/static/dashboard.html
# Check browser console for WebSocket errors# Test WebSocket endpoint
curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" \
-H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: test" \
http://localhost:8080/ws/metrics# Check circuit breaker status
curl http://localhost:8080/api/management/circuit-breakers
# Reset circuit breaker
curl -X POST http://localhost:8080/api/management/circuit-breakers/{backend}/reset# Check rate limiter status
curl http://localhost:8080/api/management/rate-limiters# Monitor application logs
tail -f logs/router-service.log
# Search for errors
grep ERROR logs/router-service.log
# Monitor circuit breaker events
grep "Circuit breaker" logs/router-service.log- Check backend health:
/api/management/health - Review circuit breaker states:
/api/management/circuit-breakers - Monitor rate limiting:
/api/management/rate-limiters - Check JVM memory usage
- Review network connectivity
- Check for memory leaks in latency buffers
- Review JVM heap settings
- Monitor garbage collection
- Check for large request bodies
| Endpoint | Method | Description | Authentication |
|---|---|---|---|
/api/management/rules |
GET | List all routing rules | Required |
/api/management/health |
GET | Backend health status | Required |
/api/management/metrics |
GET | Current metrics | Required |
/api/management/status |
GET | Overall system status | Required |
/api/management/circuit-breakers |
GET | Circuit breaker states | Required |
/api/management/circuit-breakers/{backend}/reset |
POST | Reset circuit breaker | Required |
| Endpoint | Method | Description | Authentication |
|---|---|---|---|
/v1/** |
ALL | Route requests to backends | Not Required |
/api/** |
ALL | Route requests to backends | Not Required |
/admin/** |
ALL | Route requests to backends | Not Required |
| Endpoint | Method | Description | Authentication |
|---|---|---|---|
/actuator/health |
GET | Application health | Not Required |
/actuator/info |
GET | Application info | Not Required |
- JWT Token Authentication: Replace basic auth with JWT
- OAuth2 Integration: SSO with external providers
- Multi-Factor Authentication: 2FA support
- API Key Management: Secure API key handling
- Weighted Load Balancing: Support for backend weights
- SSL/TLS Termination: HTTPS support
- Request/Response Transformation: Header and body modification
- Service Discovery: Integration with Consul, Eureka
- Distributed Tracing: OpenTelemetry integration
- Advanced Metrics: Custom business metrics
- Configuration Hot Reload: Dynamic configuration updates
- Multi-Region Support: Geographic load balancing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Update documentation
- Submit a pull request
This project is licensed under the MIT License.
For support and questions:
- Check the troubleshooting section
- Review the configuration examples
- Open an issue on GitHub
- Check the logs:
tail -f logs/router-service.log
Router Service - Production-ready load balancer with advanced routing, monitoring, and security features. ๐