SpringMVC 限流的示例代碼
在使用SpringBoot做接口訪問如何做接口的限流,這里我們可以使用google的Guava包來實(shí)現(xiàn),當(dāng)然我們也可以自己實(shí)現(xiàn)限流,Guava中的限流是久經(jīng)考驗(yàn)的我們沒必需重新再去寫一個(gè),如果想了解限流原理的同學(xué)可以自己查閱一下相關(guān)的資料,本文不作過來說明噢。
使用說明
在項(xiàng)目中引入Guava相關(guān)包
http://mvnrepository.com/artifact/com.google.guava/guava/21.0
maven項(xiàng)目
<!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>21.0</version> </dependency>
gradle項(xiàng)目
// https://mvnrepository.com/artifact/com.google.guava/guava compile group: 'com.google.guava', name: 'guava', version: '21.0'
寫一個(gè)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 限流類型:等待/丟棄(達(dá)到限流量)
*/
public SmoothBurstyInterceptor(int tps, SmoothBurstyInterceptor.LimitType limitType) {
this.limiter = RateLimiter.create(tps);
this.limitType = limitType;
}
/**
* @param permitsPerSecond 每秒新增的令牌數(shù)
* @param limitType 限流類型:等待/丟棄(達(dá)到限流量)
*/
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("網(wǎng)絡(luò)異常!");//達(dá)到限流后,往頁面提示的錯(cuò)誤信息。
}
@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) {
// 多個(gè)攔截器組成一個(gè)攔截器鏈
registry.addInterceptor(new SmoothBurstyInterceptor(100, SmoothBurstyInterceptor.LimitType.DROP)).addPathPatterns("/**");
//限流可配置為SmoothBurstyInterceptor.LimitType.DROP丟棄請(qǐng)求或者SmoothBurstyInterceptor.LimitType.WAIT等待,100為每秒的速率
super.addInterceptors(registry);
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JFINAL+Ajax傳參 array 數(shù)組方法 獲取request中數(shù)組操作
這篇文章主要介紹了JFINAL+Ajax傳參 array 數(shù)組方法 獲取request中數(shù)組操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
java web開發(fā)中大量數(shù)據(jù)導(dǎo)出Excel超時(shí)(504)問題解決
開發(fā)測試時(shí)候?qū)霐?shù)據(jù)遇到大數(shù)據(jù)導(dǎo)入的問題,整理了下,需要的朋友可以參考下2017-04-04
springboot 自定義權(quán)限標(biāo)簽(tld),在freemarker引用操作
這篇文章主要介紹了springboot 自定義權(quán)限標(biāo)簽(tld),在freemarker引用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Springboot發(fā)送post請(qǐng)求的幾種方式總結(jié)
這篇文章主要為大家詳細(xì)介紹了Springboot發(fā)送post請(qǐng)求的幾種方式,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定的幫助,感興趣的小伙伴可以了解一下2024-01-01
Nacos設(shè)置為windows自啟動(dòng)服務(wù)的步驟詳解
這篇文章給大家介紹了Nacos設(shè)置為windows自啟動(dòng)服務(wù)的操作步驟,文中通過代碼示例和圖文結(jié)合講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-12-12
Java線程安全和鎖Synchronized知識(shí)點(diǎn)詳解
在本篇文章里小編給大家分享的是關(guān)于Java線程安全和鎖Synchronized相關(guān)知識(shí)點(diǎn),有需要的朋友們可以參考下。2019-08-08
ArrayList和LinkedList的區(qū)別、擴(kuò)容機(jī)制以及底層的實(shí)現(xiàn)方式
這篇文章主要介紹了ArrayList和LinkedList的區(qū)別、擴(kuò)容機(jī)制以及底層的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
idea2019版與maven3.6.2版本不兼容的解決方法
這篇文章主要介紹了idea2019版與maven3.6.2版本不兼容的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
JDK1.7以上javaFTP上傳刪除文件的實(shí)現(xiàn)方法
下面小編就為大家分享一篇JDK1.7以上javaFTP上傳刪除文件的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-11-11

