When using SpringBoot as an interface to access how to do interface current limiting, we can use Google's Guava package to implement it. Of course, we can also implement current limiting by ourselves. The current limiting in Guava is a long-standing test. We don't need to write another one. If you want to understand the principle of current limiting, you can check the relevant information by yourself. This article will not explain it.
Instructions for use
Introducing Guava related packages to the project
http://mvnrepository.com/artifact/com.google.guava/guava/21.0
maven project
<!-- https://mvnrepository.com/artifact/com.google.guava/guava --><dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>21.0</version></dependency>
gradle project
// https://mvnrepository.com/artifact/com.google.guava/guavacompile group: 'com.google.guava', name: 'guava', version: '21.0'
Write a SpringMVC interceptor
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,//Discard WAIT //Waiting} /** * Current limiter*/ private RateLimiter limit; /** * Current limit method*/ private LimitType limitType = LimitType.DROP; public SmoothBurstyInterceptor() { this.limiter = RateLimiter.create(10); } /** * @param tps Flow limit (processing per second) * @param limitType Current limit type: Wait/Discard (to reach the flow limit) */ public SmoothBurstyInterceptor(int tps, SmoothBurstyInterceptor.LimitType limitType) { this.limitter = RateLimiter.create(tps); this.limitType = limitType; } /** * @param permitsPerSecond Number of new tokens added per second* @param limitType Current limit type: Wait/Drop (to reach the limit) */ public SmoothBurstyInterceptor(double permitsPerSecond, SmoothBurstyInterceptor.LimitType limitType) { this.limitter = 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("Network exception!");//The error message prompted to the page after the current limit is reached. } @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 interception configuration
WebConfig.java
@Componentpublic class WebConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { // Multiple interceptors form an interceptor chain registry.addInterceptor(new SmoothBurstyInterceptor(100, SmoothBurstyInterceptor.LimitType.DROP)).addPathPatterns("/**"); // Current limit can be configured as SmoothBurstyInterceptor.LimitType.DROP drop request or SmoothBurstyInterceptor.LimitType.WAIT wait, 100 is the rate per second super.addInterceptors(registry); }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.