To implement custom gateway filters in Spring
Cloud Gateway (SCG) for Debouncing, Throttling, Rate Limiting,
Exponential Backoff, or other custom logic, you can define custom
filters in your Spring Cloud Gateway project and configure them using the application.yml
file.
1. Spring Cloud Gateway Overview
Spring Cloud Gateway provides a flexible way to create
custom filters that can apply logic such as debouncing, throttling, and rate
limiting at the gateway level. This can help protect downstream services and
ensure efficient use of resources.
Steps to Create Custom Filters:
- Add
Spring Cloud Gateway to your project using Maven or Gradle.
- Create
a custom gateway filter factory for implementing the custom logic.
- Configure
the gateway routes and filters in application.yml.
2. Adding Spring Cloud Gateway Dependency
Add the following dependencies to your pom.xml:
xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId> </dependency> |
3. Custom Gateway Filters
You can create custom filters to implement debouncing,
throttling, rate limiting, and exponential backoff.
3.1. Debouncing Custom Gateway Filter
Debouncing limits how often a function is invoked in quick
succession by adding a delay.
Custom Debouncing Filter Factory:
java
@Component public class
DebounceGatewayFilterFactory extends
AbstractGatewayFilterFactory<DebounceGatewayFilterFactory.Config> { public DebounceGatewayFilterFactory() { super(Config.class); } @Override public GatewayFilter apply(Config config)
{ return (exchange, chain) -> { long delay = config.getDelay(); return
Mono.delay(Duration.ofMillis(delay)).then(chain.filter(exchange)); }; } public static class Config { private long delay; public long getDelay() { return delay; } public void setDelay(long delay) { this.delay = delay; } } } |
YAML Configuration for Debouncing:
yaml
spring: cloud: gateway: routes: - id: debounce_route uri: http://example.org filters: - name: Debounce args: delay: 500 |
3.2. Throttling Custom Gateway Filter
Throttling limits the rate at which requests are processed
over a specified period.
Custom Throttling Filter Factory:
java
@Component public class ThrottleGatewayFilterFactory extends AbstractGatewayFilterFactory<ThrottleGatewayFilterFactory.Config> { private final Map<String, Long> requestCounts = new ConcurrentHashMap<>(); public ThrottleGatewayFilterFactory() { super(Config.class); } @Override public GatewayFilter apply(Config config)
{ return (exchange, chain) -> { String clientIP =
exchange.getRequest().getRemoteAddress().getAddress().getHostAddress(); long currentTime = System.currentTimeMillis(); requestCounts.merge(clientIP, 1L, Long::sum); if (requestCounts.get(clientIP)
> config.getMaxRequests()) {
exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS); return
exchange.getResponse().setComplete(); } return chain.filter(exchange); }; } public static class Config { private long maxRequests; public long getMaxRequests() { return maxRequests; } public void setMaxRequests(long
maxRequests) { this.maxRequests = maxRequests; } } } |
YAML Configuration for Throttling:
yaml
spring: cloud: gateway: routes: - id: throttle_route uri: http://example.org filters: - name: Throttle args: maxRequests: 10 |
3.3. Rate Limiting Custom Gateway Filter
Rate limiting is used to restrict the number of requests a
client can make within a specific time window.
Spring Cloud Gateway has built-in support for rate limiting
via the RedisRateLimiter.
YAML Configuration for Rate Limiting:
yaml
spring: cloud: gateway: routes: - id: rate_limited_route uri: http://example.org filters: - name: RequestRateLimiter args: redis-rate-limiter: replenishRate: 10 burstCapacity: 20 key-resolver:
"#{@remoteAddrKeyResolver}" |
Key Resolver:
You need to define a key resolver to specify how the gateway
will identify clients:
java
@Bean public
KeyResolver remoteAddrKeyResolver() { return exchange ->
Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress()); } |
3.4. Exponential Backoff Retry Custom Gateway Filter
Exponential backoff is a retry strategy that increases the
wait time between retries after each failure.
Spring Cloud Gateway has a built-in retry filter that you
can configure for Exponential Backoff.
YAML Configuration for Exponential Backoff:
yaml
spring: cloud: gateway: routes: - id: exponential_backoff_route uri: http://example.org filters: - name: Retry args: retries: 3 statuses: 500 backoff: firstBackoff: 100ms maxBackoff: 2s factor: 2 jitter: true |
4. Final YAML Configuration Example
Here is a complete application.yml that demonstrates how to
configure different custom filters:
yaml
spring: cloud: gateway: routes: - id: debounce_route uri: http://example.org filters: - name: Debounce args: delay: 500 - id: throttle_route uri: http://example.org filters: - name: Throttle args: maxRequests: 10 - id: rate_limited_route uri: http://example.org filters: - name: RequestRateLimiter args: redis-rate-limiter: replenishRate: 10 burstCapacity: 20 key-resolver:
"#{@remoteAddrKeyResolver}" - id: exponential_backoff_route uri: http://example.org filters: - name: Retry args: retries: 3 statuses: 500 backoff: firstBackoff: 100ms maxBackoff: 2s factor: 2 jitter: true # Redis for
rate limiting spring: redis: host: localhost port: 6379 |
5. Key Points
- Debounce
Filter: Introduces a delay to prevent successive requests.
- Throttle
Filter: Limits the number of requests over a period of time.
- Rate
Limiting: Restricts the number of requests allowed using RedisRateLimiter.
- Exponential
Backoff: Retries failed requests with increasing delay between
retries.
You can configure these custom filters based on your
microservices' needs using the application.yml file. Each filter helps in
managing traffic, avoiding overwhelming services, and ensuring reliable
communication between services.
Flowchart of Custom gateway filters in Spring Cloud Gateway |
For More Related information, visit
Ø
Mastering
Debounce, Throttle, Rate Limit & Backoff in Java
Ø Deep
understand of ThrottlingFilter with RewritePath filter in cloud gateway
Ø Setting up Custom Filters using Debouncing, Throttling, Rate Limiting, and Exponential Backoff
Ø Custom
Filters in Microservices
Ø Mastering
Debounce, Throttle, Rate Limit & Backoff in Java
Ø Microservices:
Custom Filters for Debouncing, Throttling, Rate Limits & Backoff
Ø Spring
Cloud Gateway uses a RewritePath filter
For More DSA Related information, visit
Ø
Bench
mark of compiler using Ackerman function
Ø
To
check if the rows of a matrix are circularly identical in Java
Ø
Frequency
Weaving Logic & Spiral Printing of a Rectangle
Ø
Zig Zag
Matrix print multiple way
Ø
Gready
Algorithm’s or knapsack algorithms
Ø
understanding
recursive method for binary tree
Ø
Dynamic
Programming: Max Square Sub-matrix of 1s in a Matrix
Ø
Previous and
Next Date Palindrome
Ø
Karatsuba's
Algorithm for multiplying two large numbers
Ø
Multiplication
In different Way
Ø
Time
Analysis for Congested Routes Using Shortest Path Algorithms
Ø
Sorting
Of a list multiple attribute wise two technique
Ø
Seat
Arrangement in Sorting Order Like 1A-1E, 3C-3G etc
For More Java Related information, visit
Ø
Streams
Lambdas Collectors and Functional Interfaces in Java 8
Ø
Java
8 support static method or default method or both in the interface
Ø
Serialization
understanding
Ø
Garbage
Collection Under Standing
Ø
How
work Garbage Collection in Java
Ø
Under
Standing Of Mutable and Immutable Class
For Other information, visit
Ø
How
to get the neighbor of binary tree
Ø OWASP
(Open Web Application Security Project)
Ø Mastering
Debounce, Throttle, Rate Limit & Backoff in Java
Ø How
to draw sequence diagram and other diagrams using plantuml
Ø
Molecular
weight of chemistry in Java code
Ø
String
to xml or html Beautifier
Ø
Key
Components of Apache Kafka for Scalable Messaging
Ø
Build
a Video Stream Microservice with Kafka & REST API in Java
Ø
Kafka
general questions and answers
Ø
How
to convert XML to Object and Object to XML
Ø
To
securely obtain employee information utilizing TLS 1.3 or TLS 1.2
Ø
Convert
Floating-Point Values from SQL Server to Oracle in Java
Ø
Mastering
Design Patterns Practical Implementation Tips
Ø
0 Comments