An API gateway implementation using Spring Cloud Gateway for request routing and throttling with Bucket4j based on the token-bucket algorithm.
This project is built with Java 21 and Spring Boot 3.3.4, using Spring Cloud Gateway to route and filter API requests. It leverages Bucket4j to enforce rate limiting via the token-bucket algorithm, ensuring your API is protected from excessive requests. The filtering logic is abstracted in a base class to support different types of throttling (for example, by IP or by header).
-
Throttling Filters:
An abstract filter (AbstractThrottlingFilter
) that provides a blueprint for various limiting strategies. -
IP-Based Rate Limiting:
TheIpThrottlingFilter
class extracts the client IP (supportingX-Forwarded-For
headers) and applies rate limiting. -
Header-Based Rate Limiting:
TheHeaderThrottlingFilter
class resolves a specific header value to be used as the key for throttling. -
Bucket4j Integration:
Uses Bucket4j (v8.14.0) to implement the token bucket algorithm for rate limiting. -
Caching Support:
Integrates with Caffeine Cache to support rate limiting in distributed or high-load environments.
- Java 21
- Spring Boot 3.3.4
- Spring Cloud Gateway
- Bucket4j (for rate limiting)
- Caffeine Cache
- Maven for dependency management
api-gateway-throttling/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── dev/mehdizebhi/gateway/filter/
│ │ │ ├── AbstractThrottlingFilter.java
│ │ │ ├── HeaderThrottlingFilter.java
│ │ │ └── IpThrottlingFilter.java
│ │ └── resources/
│ │ └── application.yml (or application.properties)
└── pom.xml
-
AbstractThrottlingFilter:
Provides the common logic to try consuming a token from a rate limit bucket and handling the response if the limit is exceeded. -
HeaderThrottlingFilter & IpThrottlingFilter:
Extend the abstract filter to use different request properties (headers or IP addresses) as keys for rate limiting.
-
Clone the repository:
git clone https://your.repo.url/api-gateway-throttling.git cd api-gateway-throttling
-
Build the project using Maven:
mvn clean install
-
Run the application:
mvn spring-boot:run
Alternatively, run the packaged jar:
java -jar target/gateway-0.0.1-SNAPSHOT.jar