自定义注解,aop+redis,实现controller接口频率
2017-05-26 本文已影响105人
沐兮_d64c
1,环境配置
- 引入aop的jar包
compile 'org.springframework:spring-aop:3.2.4.RELEASE'版本号同项目中的一致即可
2)使用xml配置
applicationContext.xml,项目配置文件中开启aop支持。 <aop:aspectj-autoproxy/>
dispatcherServletContext.xml,spring的配置文件中开启aop支持。 <aop:aspectj-autoproxy/>
使用java config配置
WebConfig类以及AppConfig类中,使用注解 @EnableAspectJAutoProxy
- 配置扫描的包
xml方式
<context:component-scan base-package="com.hzq"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/> </context:component-scan>
java config方式
@ComponentScan(basePackages = "com.hzq" includeFilters = {@ComponentScan.Filter(Controller.class), @ComponentScan.Filter(ControllerAdvice.class)})
2,自定义注解
/**
* 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;
}
3,切面通知实现类。使用@Around的方式。可以在调用方法前后,执行一些操作。
AOP概念相关的可参考:http://www.cnblogs.com/shipengzhi/articles/2716004.html
通知(Advice) :定义了切面是什么以及何时使用。描述了切面要完成的工作和何时需要执行这个工作。eg环绕通知:@Around
切入点(Pointcut) :例如某个类或方法的名称。eg:within(@org.springframework.stereotype.Controller *) && @annotation(limit)"//所有的controller中,含有@RequestLimit注解的方法作为切点。
连接点(Joinpoint) :例如方法被调用时、异常被抛出时等等。
目标对象(Target Object) :即被通知的对象。eg:joinPoint.getTarget()
切面(Aspect) :通知+切入点=切面。
/**
* 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<String, Object> 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<String, String> 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());
}
}
}
4,使用注解。
@RequestMapping(value = "/api/info", method = RequestMethod.POST)
@ResponseBody
@RequestLimit(count = 10, time = 1000)
public Map<String, Object> getApiInfo(HttpServletRequest request){
Map<String, Object> 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;
}
5,使用拦截器的方式
@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 {
}
}