Anubix is a demonstration of a custom-built API Gateway with advanced load balancing capabilities, built with Spring Boot 3.2.5 and Spring Cloud 2023.0.1. The platform showcases a robust API Gateway implementation with custom load balancing, circuit breaking, rate limiting, and monitoring capabilities.
Built on Spring WebFlux and Project Reactor, Anubix leverages reactive programming to provide non-blocking, event-driven architecture. This enables high throughput and low latency, making it ideal for handling concurrent requests and real-time data processing.
- Reactive Architecture:
- Non-blocking I/O with WebFlux
- Event-driven processing
- Backpressure handling
- Reactive streams support
- Custom Load Balancer: Implements multiple load balancing algorithms (Round Robin, Least Connections , adaptive)
- Dynamic Service Discovery: Automatic service registration and discovery via Eureka
- Advanced Routing: Intelligent request routing with path-based and service-based routing
- Resilience Patterns:
- Circuit Breaking with Resilience4j
- Rate Limiting per service
- Retry mechanisms with configurable attempts
- Request caching with Redis
- Security:
- JWT validation
- Request authentication
- Rate limiting per client
- Monitoring:
- Prometheus metrics integration
- Grafana dashboards
- Health checks
- Request tracing
The following services are included as demonstrative examples to showcase the API Gateway's capabilities:
- Auth Service: Demonstrates JWT authentication flow
- Feature Service: Example of service scaling and load balancing
- Toggle Service: Shows circuit breaking and resilience patterns
- Analytics Service: Demonstrates metrics collection and monitoring
- Java 17 or higher
- Docker and Docker Compose
- Maven
-
Clone the repository
git clone https://github.com/Petrichor0314/Anubix.git cd Anubix
-
Create environment files
rename
.env.example
file in the root directory to.env
: -
Build and Run
.\rebuild.ps1
- Multiple Algorithms:
- Round Robin
- Adaptive Algorithm
- Least Connections
- Dynamic Instance Management:
- Automatic instance discovery
- Health-based instance selection
- Instance weight management
- Circuit Breaking:
- Configurable failure thresholds
- Automatic circuit state management
- Fallback mechanisms
- Rate Limiting:
- Per-service rate limits
- Per-client rate limits
- Configurable time windows
- Retry Mechanism:
- Configurable retry attempts
- Exponential backoff
- Retry condition customization
- Redis Integration:
- Response caching
- Configurable TTL
- Cache invalidation
- Cache Strategies:
- Time-based expiration
- Event-based invalidation
- Selective caching
- Metrics Collection:
- Request counts
- Response times
- Error rates
- Circuit breaker states
- Health Checks:
- Service health monitoring
- Instance health tracking
- Health-based routing
- Access at
http://localhost:9090
- Scrapes metrics every 5 seconds
- Key metrics:
- Request rates
- Response times
- Error rates
- Circuit breaker states
- Load balancer statistics
- Access at
http://localhost:3000
- Default credentials: admin/admin
- Pre-configured dashboards:
- API Gateway Overview
- Load Balancer Metrics
- Circuit Breaker States
- Rate Limiter Statistics
- Scale services to multiple instances
- Monitor request distribution
- Test different load balancing algorithms
- Simulate service failures
- Observe circuit breaker behavior
- Test rate limiting
- Verify retry mechanisms
- Monitor response times
- Check caching effectiveness
- Verify load distribution
Located in simulate-requests/simulate_requests.py
, this script provides:
- Multi-threaded request simulation
- JWT authentication flow testing
- Random endpoint selection across services
- Mix of authenticated and unauthenticated requests
- Detailed request/response logging
- Configurable request delays and timeouts
Usage:
cd simulate-requests
python simulate_requests.py
The project includes a Postman collection for manual testing of:
- API Gateway endpoints
- Service discovery
- Load balancing behavior
- Circuit breaker states
- Rate limiting
- Cache operations
The API Gateway includes extensive logging to facilitate testing and debugging:
- Request/response details
- Load balancer decisions
- Circuit breaker state changes
- Rate limiter events
- Cache operations
- Service discovery events
- Authentication/authorization events
Anubix/
βββ api-gateway/ # Custom API Gateway implementation
β βββ src/
β β βββ main/
β β β βββ java/
β β β β βββ com/
β β β β βββ petrichor/
β β β β βββ loadbalancer/
β β β β βββ load_balancer/
β β β β βββ algorithm/ # Load balancing algorithms
β β β β βββ config/ # Gateway configuration
β β β β βββ controller/ # API endpoints
β β β β βββ factory/ # Factory patterns
β β β β βββ model/ # Data models
β β β β βββ registry/ # Service registry
β β β β βββ security/ # Security components
β β β β βββ service/ # Business logic
β β β β βββ util/ # Utility classes
β β β β βββ ApiGatewayApplication.java
β β β βββ resources/
β β β βββ application.yml # Gateway configuration
β βββ pom.xml # Gateway dependencies
β βββ Dockerfile # Gateway container definition
βββ [other services]/ # Supporting microservices (auth, feature, toggle, analytics)
βββ simulate-requests/ # Python load testing script
βββ prometheus/ # Prometheus configuration
βββ docker-compose.yml # Docker services composition
βββ pom.xml # Parent Maven configuration
βββ rebuild.ps1 # PowerShell build script
The API Gateway is the core component of this project, implementing custom load balancing, circuit breaking, and other advanced features. Other services are included as demonstrative examples to showcase the gateway's capabilities.
The API Gateway is designed to handle:
- Multiple backend service instances
- High request volumes
- Dynamic service scaling
- Configurable load balancing
-
Load Balancer Issues
- Check service registration in Eureka
- Verify load balancing algorithm configuration
- Monitor instance health
-
Circuit Breaker Issues
- Check circuit breaker configuration
- Monitor failure rates
- Verify fallback mechanisms
-
Rate Limiting Issues
- Verify rate limit configuration
- Check client identification
- Monitor rate limit metrics
# View API Gateway logs
docker-compose logs -f api-gateway
# View all logs
docker-compose logs -f
While this project demonstrates key API Gateway concepts, it's important to note that this is a learning/demonstration project and not production-ready. Here are some areas for potential improvement:
- Implement more sophisticated load balancing algorithms
- Add support for WebSocket connections
- Enhance security with OAuth2/OIDC
- Implement API versioning
- Add request/response transformation capabilities
- Implement more advanced caching strategies
- Add support for GraphQL
- Comprehensive test coverage
- Performance benchmarking
- Security auditing
- Documentation improvements
- Container optimization
- CI/CD pipeline
- Production-grade monitoring
- Backup and recovery procedures
- Horizontal scaling of the gateway itself
- Distributed rate limiting
- Global load balancing
- Multi-region deployment support
- Service mesh integration
This project is open source and available under the MIT License. Feel free to use, modify, and distribute it as you wish.
Contributions are welcome! Whether it's:
- Bug fixes
- Feature enhancements
- Documentation improvements
- Performance optimizations
- Test cases
- Example implementations
Please feel free to:
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request