A standalone rate limiter service for Java Spring Boot applications that supports multiple rate limiting algorithms and can be used as a separate service to control API request rates.
- Multiple rate limiting algorithms supported:
- Token Bucket
- Leaking Bucket
- Fixed Window Counter
- Redis-based storage.
- Dynamic rule loading from DOC files.
- Scheduled rule reloading (every 1 hour).
- REST API for rate limit checking and rule management.
- Standalone service architecture.
@Service
public class ApiGatewayService {
@Autowired
private RestTemplate restTemplate;
@Value("${rate.limiter.url}")
private String rateLimiterUrl;
public boolean isRequestAllowed(String apiPath, String method, String clientId) {
String resourceId = apiPath + ":" + method;
RateLimitRequest request = new RateLimitRequest(resourceId, clientId);
try {
ResponseEntity<RateLimitResponse> response = restTemplate.postForEntity(
rateLimiterUrl + "/check", request, RateLimitResponse.class);
return response.getBody().isAllowed();
} catch (HttpClientErrorException.TooManyRequests ex) {
return false;
}
}
}API: /api/users
METHOD: GET
ALGORITHM: TOKEN_BUCKET
LIMIT: 100
PERIOD: 60
TIME_UNIT: SECONDS
API: /api/orders
METHOD: POST
ALGORITHM: LEAKING_BUCKET
LIMIT: 30
PERIOD: 60
TIME_UNIT: SECONDS- Check Rate Limit
POST /rate-limiter/api/v1/rate-limit/check //Request Body: { "resourceId": "/api/users:GET", "clientId": "user123" } //Response: { "allowed": true, "limit": 100, "remaining": 99, "resetTime": 1646324568 } - Get All Rules
GET /rate-limiter/api/v1/rate-limit/rules
- Reload Rules
POST /rate-limiter/api/v1/rate-limit/rules/reload


