栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC 限流的示例代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC 限流的示例代码

在使用SpringBoot做接口访问如何做接口的限流,这里我们可以使用google的Guava包来实现,当然我们也可以自己实现限流,Guava中的限流是久经考验的我们没必需重新再去写一个,如果想了解限流原理的同学可以自己查阅一下相关的资料,本文不作过来说明噢。

使用说明

在项目中引入Guava相关包

http://mvnrepository.com/artifact/com.google.guava/guava/21.0

maven项目



  com.google.guava
  guava
  21.0

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);
  }

  
  public SmoothBurstyInterceptor(int tps, SmoothBurstyInterceptor.LimitType limitType) {
    this.limiter = RateLimiter.create(tps);
    this.limitType = 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);
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/142836.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号