SpringMVC 限流的示例代码
作者:dounine 时间:2022-08-21 09:48:51
在使用SpringBoot做接口访问如何做接口的限流,这里我们可以使用google的Guava包来实现,当然我们也可以自己实现限流,Guava中的限流是久经考验的我们没必需重新再去写一个,如果想了解限流原理的同学可以自己查阅一下相关的资料,本文不作过来说明噢。
使用说明
在项目中引入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);
}
}
来源:http://blog.csdn.net/dounine/article/details/71439809
标签:Spring,MVC,限流
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#、vb.net及SQL判断指定年份是否为闰年的方法
2023-05-18 09:49:55
浅谈java中OO的概念和设计原则(必看)
2023-11-24 13:09:56
SpringBoot项目中分页插件PageHelper无效的问题及解决方法
2021-07-08 23:25:55
Java源码解析之可重入锁ReentrantLock
2021-12-28 22:32:40
mybatis中映射文件(mapper)中的使用规则
2021-06-28 20:09:08
![](https://img.aspxhome.com/file/2023/8/76248_0s.png)
Flutter倒计时/计时器的实现代码
2023-07-01 03:50:50
![](https://img.aspxhome.com/file/2023/5/103895_0s.jpg)
Java实现Redis的集合(set)命令操作
2022-05-08 11:07:03
![](https://img.aspxhome.com/file/2023/4/61954_0s.jpg)
使用mybatis-plus的insert方法遇到的问题及解决方法(添加时id值不存在异常)
2023-11-12 19:25:55
![](https://img.aspxhome.com/file/2023/6/59606_0s.png)
基于javaMybatis存进时间戳的问题
2023-11-29 02:55:51
Java实现简易Web服务器
2023-11-17 09:10:11
hibernate4基本配置方式详解
2023-03-11 11:07:43
![](https://img.aspxhome.com/file/2023/7/68737_0s.png)
Java中List转Map List实现的几种姿势
2022-10-23 19:19:57
Java内存模型JMM与volatile
2022-03-25 09:33:50
![](https://img.aspxhome.com/file/2023/4/62204_0s.png)
Spring实现默认标签解析流程
2021-07-29 10:07:55
关于MyBatis模糊查询的几种实现方式
2023-05-09 04:23:12
详解JAVA Spring 中的事件机制
2022-01-13 05:26:33
双重检查锁定模式Java中的陷阱案例
2023-11-13 22:11:02
![](https://img.aspxhome.com/file/2023/5/59575_0s.png)
Java如何把int类型转换成byte
2023-03-13 11:12:39
![](https://img.aspxhome.com/file/2023/6/79816_0s.png)
2020JDK1.8安装教程详解(一次就可安装成功)
2023-11-28 04:08:58
![](https://img.aspxhome.com/file/2023/3/60253_0s.png)
java8 Stream大数据量List分批处理切割方式
2023-02-10 22:34:36