SpringMVC 限流的示例代碼
在使用SpringBoot做接口訪問(wèn)如何做接口的限流,這里我們可以使用google的Guava包來(lái)實(shí)現(xiàn),當(dāng)然我們也可以自己實(shí)現(xiàn)限流,Guava中的限流是久經(jīng)考驗(yàn)的我們沒(méi)必需重新再去寫一個(gè),如果想了解限流原理的同學(xué)可以自己查閱一下相關(guān)的資料,本文不作過(guò)來(lái)說(shuō)明噢。
使用說(shuō)明
在項(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á)到限流后,往頁(yè)面提示的錯(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);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Java swing組件實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
這篇文章主要介紹了基于Java swing組件實(shí)現(xiàn)簡(jiǎn)易計(jì)算器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Servlet關(guān)于RequestDispatcher的原理詳解
這篇文章主要介紹了Servlet關(guān)于RequestDispatcher的原理詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
springboot集成RestTemplate及常見(jiàn)的用法說(shuō)明
這篇文章主要介紹了springboot集成RestTemplate及常見(jiàn)的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Spring中@PathVariable注解的簡(jiǎn)單使用
這篇文章主要介紹了Spring中@PathVariable注解的簡(jiǎn)單使用,@PathVariable 是 Spring Framework 中的注解之一,用于處理 RESTful Web 服務(wù)中的 URL 路徑參數(shù),它的作用是將 URL 中的路徑變量綁定到方法的參數(shù)上,需要的朋友可以參考下2024-01-01
Java并發(fā)工具之Exchanger線程間交換數(shù)據(jù)詳解
這篇文章主要介紹了Java并發(fā)工具之Exchanger線程間交換數(shù)據(jù)詳解,Exchanger是一個(gè)用于線程間協(xié)作的工具類,Exchanger用于進(jìn)行線程間的數(shù)據(jù)交 換,它提供一個(gè)同步點(diǎn),在這個(gè)同步點(diǎn),兩個(gè)線程可以交換彼此的數(shù)據(jù),需要的朋友可以參考下2023-12-12
spring-boot-klock-starter V1.1 主體功能重大更新內(nèi)容介紹
這篇文章主要介紹了spring-boot-klock-starter V1.1 主體功能重大更新內(nèi)容描述,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01
使用Spring源碼報(bào)錯(cuò)java:找不到類 InstrumentationSavingAgent的問(wèn)題
這篇文章主要介紹了使用Spring源碼報(bào)錯(cuò)java:找不到類 InstrumentationSavingAgent的問(wèn)題,本文給大家分享解決方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

