小编给大家分享一下SpringMVC如何实现限流,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
使用说明
在项目中引入Guava相关包
http://mvnrepository.com/artifact/com.google.guava/guava/21.0
maven项目
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
gradle项目
// https://mvnrepository.com/artifact/com.google.guava/guava
compile group: 'com.google.guava', name: 'guava', version: '21.0'
写一个SpringMVC的拦截器
SmoothBurstyInterceptor.java
import com.google.common.util.concurrent.RateLimiter;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;
public class SmoothBurstyInterceptor extends HandlerInterceptorAdapter {
public enum LimitType {
DROP,//丢弃
WAIT //等待
}
/**
* 限流器
*/
private RateLimiter limiter;
/**
* 限流方式
*/
private LimitType limitType = LimitType.DROP;
public SmoothBurstyInterceptor() {
this.limiter = RateLimiter.create(10);
}
/**
* @param tps 限流量 (每秒处理量)
* @param limitType 限流类型:等待/丢弃(达到限流量)
*/
public SmoothBurstyInterceptor(int tps, SmoothBurstyInterceptor.LimitType limitType) {
this.limiter = RateLimiter.create(tps);
this.limitType = limitType;
}
/**
* @param permitsPerSecond 每秒新增的令牌数
* @param limitType 限流类型:等待/丢弃(达到限流量)
*/
public SmoothBurstyInterceptor(double permitsPerSecond, SmoothBurstyInterceptor.LimitType limitType) {
this.limiter = RateLimiter.create(permitsPerSecond, 1000, TimeUnit.MILLISECONDS);
this.limitType = limitType;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (limitType.equals(LimitType.DROP)) {
if (limiter.tryAcquire()) {
return super.preHandle(request, response, handler);
}
} else {
limiter.acquire();
return super.preHandle(request, response, handler);
}
throw new Exception("网络异常!");//达到限流后,往页面提示的错误信息。
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
super.afterCompletion(request, response, handler, ex);
}
public RateLimiter getLimiter() {
return limiter;
}
public void setLimiter(RateLimiter limiter) {
this.limiter = limiter;
}
}
SpringMVC拦截配置
WebConfig.java
@Component
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 多个拦截器组成一个拦截器链
registry.addInterceptor(new SmoothBurstyInterceptor(100, SmoothBurstyInterceptor.LimitType.DROP)).addPathPatterns("/**");
//限流可配置为SmoothBurstyInterceptor.LimitType.DROP丢弃请求或者SmoothBurstyInterceptor.LimitType.WAIT等待,100为每秒的速率
super.addInterceptors(registry);
}
}
看完了这篇文章,相信你对“SpringMVC如何实现限流”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。