单机版中我们了解到 AtomicInteger、RateLimiter、Semaphore 这几种解决方案,但它们也仅仅是单机的解决手段,在集群环境下就透心凉了,后面又讲述了 Nginx 的限流手段,可它又属于网关层面的策略之一,并不能解决所有问题。例如供短信接口,你无法保证消费方是否会做好限流控制,所以自己在应用层实现限流还是很有必要的。
本章目标
利用 自定义注解、Spring Aop、Redis Cache 实现分布式限流
自定义注解
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Limit {
/**
* 资源的名字
*
* @return String
*/
String name() default "";
/**
* 资源的key
*
* @return String
*/
String key() default "";
/**
* Key的prefix
*
* @return String
*/
String prefix() default "";
/**
* 给定的时间段
* 单位秒
*
* @return int
*/
int period();
/**
* 最多的访问限制次数
*
* @return int
*/
int count();
/**
* 类型
*
* @return LimitType
*/
LimitType limitType() default LimitType.CUSTOMER;
}
4. RedisConfig
@Configuration
public class RedisConfig {
@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("127.0.0.1", 6379);
return new JedisConnectionFactory(redisStandaloneConfiguration);
}
@Bean
public RedisTemplate<String, Serializable> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Serializable> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return template;
}
}
切面LimitInterceptor
Redis是线程安全的,我们利用它的特性可以实现分布式锁、分布式限流等组件,之前分布式服务防重复提交的那篇文章就使用官方API setIfAbsent实现了一个分布式锁,来实现分布式服务防重复提交。对于分布式限流而言,分布式限流不能像防重复提交的分布式锁的逻辑,完全依赖于redis expireTime,因为放重复提交在指定时间内只允许一次有效访问,但是分布式服务限流允许多次有效访问。官方未提供相应的API,但是却提供了支持Lua脚本的功能,我们可以通过编写Lua脚本实现自己的API,同时他是满足原子性的。
@Aspect
@Configuration
@Slf4j
@RequiredArgsConstructor
public class LimitInterceptor {
private final RedisTemplate<String, Serializable> limitRedisTemplate;
@Around("execution(public * *(..)) && @annotation(com.zhuoli.service.springboot.distributed.current.limit.annotation.Limit)")
public Object interceptor(ProceedingJoinPoint pjp) {
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
Limit limitAnnotation = method.getAnnotation(Limit.class);
LimitType limitType = limitAnnotation.limitType();
String name = limitAnnotation.name();
String key;
int limitPeriod = limitAnnotation.period();
int limitCount = limitAnnotation.count();
switch (limitType) {
case IP:
key = getIpAddress();
break;
case CUSTOMER:
key = limitAnnotation.key();
break;
default:
key = method.getName().toUpperCase();
}
ImmutableList<String> keys = ImmutableList.of(Joiner.on("").join(limitAnnotation.prefix(), key));
try {
String luaScript = buildLuaScript();
RedisScript<Number> redisScript = new DefaultRedisScript<>(luaScript, Number.class);
Number count = limitRedisTemplate.execute(redisScript, keys, limitCount, limitPeriod);
log.info("Access try count is {} for name={} and key = {}", count, name, key);
if (count != null && count.intValue() <= limitCount) {
return pjp.proceed();
} else {
throw new RuntimeException("Request too frequently, please try later");
}
} catch (Throwable e) {
if (e instanceof RuntimeException) {
throw new RuntimeException(e.getLocalizedMessage());
}
throw new RuntimeException("server exception");
}
}
/**
* 限流 脚本
*
* @return lua脚本
*/
public String buildLuaScript() {
StringBuilder lua = new StringBuilder();
lua.append("local c");
lua.append("\nc = redis.call('get',KEYS[1])");
// 调用超过最大值,则直接返回
lua.append("\nif c and tonumber(c) > tonumber(ARGV[1]) then");
lua.append("\nreturn c;");
lua.append("\nend");
// 执行计算器自加
lua.append("\nc = redis.call('incr',KEYS[1])");
lua.append("\nif tonumber(c) == 1 then");
// 从第一次调用开始限流,设置对应键值的过期
lua.append("\nredis.call('expire',KEYS[1],ARGV[2])");
lua.append("\nend");
lua.append("\nreturn c;");
return lua.toString();
}
private static final String UNKNOWN = "unknown";
public String getIpAddress() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
特殊讲一下这段lua脚本:
local c
c = redis.call('get',KEYS[1])
if c and tonumber(c) > tonumber(ARGV[1]) then
return c;
end
c = redis.call('incr',KEYS[1])
if tonumber(c) == 1 then
redis.call('expire',KEYS[1],ARGV[2])
end
return c;
逻辑很简单,先获取key已执行服务次数,如果已大于允许最大值直接返回。否则redis key对应的value加1,并且如果是第一次设置key,对key设置超时时间。
注解使用
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {
private UserControllerService userControllerService;
@Limit(key = "test", period = 100, count = 10)
@RequestMapping(value = "/get_user", method = RequestMethod.POST)
public ResponseEntity getUserById(@RequestParam Long id){
return ResponseEntity.status(HttpStatus.OK).body(userControllerService.getUserById(id));
}
}
设置100S内只允许访问10次
参考:
https://blog.csdn.net/weixin_41835612/article/details/83774962
http://blog.battcn.com/2018/08/08/springboot/v2-cache-redislimter/