自定义注解 - 实现接口频率限制

1,自定义注解

/**
 * Created by hzq on 2017/5/24.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
@Order(Ordered.HIGHEST_PRECEDENCE)
public @interface RequestLimit {

    /**
     * 调用的次数
     * @return
     */
    int count() default Integer.MAX_VALUE;

    /**
     * 时间段; 在time内调用的次数count
     * @return
     */
    int time() default 60000;

}

2,切面通知实现类。

/**
 * Created by hzq on 2017/5/24.
 */
@Aspect
@Component
public class RequestLimitContract {

    private static Logger logger = LoggerFactory.getLogger(RequestLimitContract.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Around("within(@org.springframework.stereotype.Controller *) && @annotation(limit)")
    public Object requestLimit(final ProceedingJoinPoint joinPoint, RequestLimit limit){
        Map map = Maps.newHashMap();

        try {
            Object[] args = joinPoint.getArgs();
            HttpServletRequest request = null;
            for(int i = 0; i < args.length; i++){
                if(args[i] instanceof HttpServletRequest){
                    request = (HttpServletRequest) args[i];
                    break;
                }
            }

            if(request == null){
                throw new RuntimeException("request is valid");
            }

            String ip = NginxUtils.getRealIp(request);
            String url = request.getRequestURI();
            String key = "req_limit_" + url + "_" + ip;
            BoundValueOperations ops = stringRedisTemplate.boundValueOps(key);

            String countString = ops.get();
            if(countString == null || countString.equals("")){
                logger.info("ip:" + ip + ", first request within 1s");
                ops.set("0", limit.time(), TimeUnit.MILLISECONDS);
            }

            long count;
            try {
                count = Long.valueOf(ops.get());
            } catch (Throwable t){
                count = 0;
            }

            if(count > limit.count()){
                logger.info("url:\t" + url + ",  ip:\t" + ip + " limit request");
                map.put("success", false);
                map.put("message", "the ip: " + ip + " is up to the limit " + limit.count() + " within " + limit.time()/1000 + "s");

                return map;
            }

            Long milSeconds = stringRedisTemplate.getExpire(key, TimeUnit.MILLISECONDS);
            if(milSeconds == null || milSeconds <= 0){
                ops.set("0", limit.time(), TimeUnit.MILLISECONDS);
            }

            ops.increment(1);
            if(milSeconds != null && milSeconds > 0){
                ops.expire(milSeconds, TimeUnit.MILLISECONDS);
            }

            return joinPoint.proceed();
        } catch (Throwable t){
            logger.info(t.getMessage(), t);
            throw new RuntimeException(t.getMessage());
        }
    }

}

3,使用注解。

    @RequestMapping(value = "/api/info", method = RequestMethod.POST)
    @ResponseBody
    @RequestLimit(count = 10, time = 1000)
    public Map getApiInfo(HttpServletRequest request){
        Map map = Maps.newHashMap();
        try {
            //service
            map.put("success", true);
            map.put("info", uidAndRoleList);
        } catch(Throwable t){
            logger.info(t.getMessage(), t);
            map.put("success", false);
            map.put("message", t.getMessage());
        }

        return map;
    }

4,另一种使用拦截器的方式

@Component
public class RequestLimitInterceptor implements HandlerInterceptor{

    private static Logger logger = LoggerFactory.getLogger(RequestLimitInterceptor.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Value("${request.limit.count}")
    private String limitCount;

    @Value("${request.limit.time}")
    private String limitTime;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        try {
            if(request == null){
                throw new RuntimeException("request is valid");
            }

            String ip = request.getRemoteAddr();
            String url = request.getRequestURI();
            String key = "req_limit_" + url + "_" + ip;
            long count = stringRedisTemplate.boundValueOps(key).increment(1);
            if(count == 1){
                stringRedisTemplate.boundValueOps(key).expire(Integer.valueOf(limitTime), TimeUnit.MILLISECONDS);
            }

            if(count > Integer.valueOf(limitCount)){
                logger.info("url:\t" + url + ",  ip:\t" + ip + " limit request");
                throw new RuntimeException("the ip:\t" + ip + " is up to the limit " + limitCount + " within " + Integer.valueOf(limitTime)/1000 + " s");
            }
        } catch (Throwable t){
            logger.info(t.getMessage(), t);
            throw new RuntimeException(t.getMessage());
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}

你可能感兴趣的:(自定义注解 - 实现接口频率限制)