Multidimensional gateway limiting
reference to the use of spring-cloud-zuul-ratelimit
- The target URL request to limit the current (for example: a URL only allowed to call many times per minute)
- Access to IP clients are limiting (for example: an IP only allowed to request how many times per minute)
- To limit the flow of specific user or group of users (for example: non-VIP only allow users to call 100 times per minute, an API, etc.)
- Multi-dimensional mixed limiting. At this point, we need to implement some of the orchestration mechanism limiting rules. AND, OR, and other non-relationship.
Introduction
spring-cloud-zuul-ratelimit is to provide integrated and distributed zuul limiting expansion strategy, just a few lines of configuration yaml configuration, the application can support the current limit
<dependency>
<groupId>com.marcosbarbero.cloud</groupId>
<artifactId>spring-cloud-zuul-ratelimit</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
Supported limiting size
-
Granularity service (the default configuration, the current limiting control service module)
-
Users size (For details, see the end of the text summary)
-
ORIGIN particle size (as user request origin Size Control)
-
Interface size (requested size as the address of the interface control)
-
More granularity free combination, and can support a variety of situations.
-
If not enough, custom RateLimitKeyGenerator achieve.
//默认实现
public String key(final HttpServletRequest request, final Route route, final RateLimitProperties.Policy policy) {
final List<Type> types = policy.getType();
final StringJoiner joiner = new StringJoiner(":");
joiner.add(properties.getKeyPrefix());
if (route != null) {
joiner.add(route.getId());
}
if (!types.isEmpty()) {
if (types.contains(Type.URL) && route != null) {
joiner.add(route.getPath());
}
if (types.contains(Type.ORIGIN)) {
joiner.add(getRemoteAddr(request));
}
// 这个结合文末总结。
if (types.contains(Type.USER)) {
joiner.add(request.getUserPrincipal() != null ? request.getUserPrincipal().getName() : ANONYMOUS_USER);
}
}
return joiner.toString();
}
Supported storage
- InMemoryRateLimiter - used as a data storage ConcurrentHashMap
- ConsulRateLimiter - used as a data storage Consul
- RedisRateLimiter - used as a data storage Redis
- SpringDataRateLimiter - using a database as a data store
Limiting configuration
- Limit the number of access per unit time to allow
- The total time to allow access to the quota within the unit time (time comprehensive statistics per request)
- refresh-interval setting unit time
- type : url ,httpmethod, user,origin
zuul:
ratelimit:
enabled: true
repository: REDIS
policy-list:
user:
- limit: 10
refresh-interval: 60
type:
- origin
auth:
- limit: 2
refresh-interval: 2
type:
- origin
file:
- limit: 2
#允许多少个访问
refresh-interval: 60
#多少秒内
type:
- url
log:
- quota: 1
#总请求实际
refresh-interval: 60
#多少秒内
type:
- origin
Show results