关于java:Guava-RateLimiter-实现-API-限流这才是正确的姿势

2次阅读

共计 4275 个字符,预计需要花费 11 分钟才能阅读完成。

Guava 提供的 RateLimiter 能够限度物理或逻辑资源的被拜访速率,咋一听有点像 java 并发包下的 Samephore,然而又不雷同,RateLimiter 管制的是速率,Samephore 管制的是并发量。

RateLimiter 的原理相似于令牌桶,它次要由许可收回的速率来定义,如果没有额定的配置,许可证将按每秒许可证规定的固定速度调配,许可将被平滑地散发,若申请超过 permitsPerSecond 则 RateLimiter 依照每秒 1/permitsPerSecond 的速率开释许可。

<dependency>
   <groupId>com.google.guava</groupId>
   <artifactId>guava</artifactId>
   <version>23.0</version>
</dependency>
public static void main(String[] args) {String start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    RateLimiter limiter = RateLimiter.create(1.0); // 这里的 1 示意每秒容许解决的量为 1 个
    for (int i = 1; i <= 10; i++) {limiter.acquire();// 申请 RateLimiter, 超过 permits 会被阻塞
        System.out.println("call execute.." + i);
    }
    String end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    System.out.println("start time:" + start);
    System.out.println("end time:" + end);
}

能够看到,我假设了每秒解决申请的速率为 1 个,当初我有 10 个工作要解决,那么 RateLimiter 就很好的实现了管制速率,总共 10 个工作,须要 9 次获取许可,所以最初 10 个工作的耗费工夫为 9s 左右。那么在理论的我的项目中是如何应用的呢??

理论我的项目中应用

@Service
public class GuavaRateLimiterService {
    /* 每秒管制 5 个许可 */
    RateLimiter rateLimiter = RateLimiter.create(5.0);
 
    /**
     * 获取令牌
     *
     * @return
     */
    public boolean tryAcquire() {return rateLimiter.tryAcquire();
    }
    
}
  @Autowired
    private GuavaRateLimiterService rateLimiterService;
    
    @ResponseBody
    @RequestMapping("/ratelimiter")
    public Result testRateLimiter(){if(rateLimiterService.tryAcquire()){return ResultUtil.success1(1001,"胜利获取许可");
        }
        return ResultUtil.success1(1002,"未获取到许可");
    }

jmeter 起 10 个线程并发拜访接口,测试后果如下:

能够发现,10 个并发拜访总是只有 6 个能获取到许可,论断就是能获取到 RateLimiter.create(n) 中 n + 1 个许可,总体来看 Guava 的 RateLimiter 是比拟优雅的。本文就是简略的提了下 RateLimiter 的应用。

翻阅发现应用上述形式应用 RateLimiter 的形式不够优雅,只管咱们能够把 RateLimiter 的逻辑包在 service 外面,controller 间接调用即可,然而如果咱们换成:自定义注解 + 切面 的形式实现的话,会优雅的多,具体见上面代码:

自定义注解类

import java.lang.annotation.*;
 
/**
 * 自定义注解能够不蕴含属性,成为一个标识注解
 */
@Inherited
@Documented
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface RateLimitAspect {}

自定义切面类

import com.google.common.util.concurrent.RateLimiter;
import com.simons.cn.springbootdemo.util.ResultUtil;
import net.sf.json.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
 
@Component
@Scope
@Aspect
public class RateLimitAop {
 
    @Autowired
    private HttpServletResponse response;
 
    private RateLimiter rateLimiter = RateLimiter.create(5.0); // 比如说,我这里设置 "并发数" 为 5
 
    @Pointcut("@annotation(com.simons.cn.springbootdemo.aspect.RateLimitAspect)")
    public void serviceLimit() {}
 
    @Around("serviceLimit()")
    public Object around(ProceedingJoinPoint joinPoint) {Boolean flag = rateLimiter.tryAcquire();
        Object obj = null;
        try {if (flag) {obj = joinPoint.proceed();
            }else{String result = JSONObject.fromObject(ResultUtil.success1(100, "failure")).toString();
                output(response, result);
            }
        } catch (Throwable e) {e.printStackTrace();
        }
        System.out.println("flag=" + flag + ",obj=" + obj);
        return obj;
    }
    
    public void output(HttpServletResponse response, String msg) throws IOException {response.setContentType("application/json;charset=UTF-8");
        ServletOutputStream outputStream = null;
        try {outputStream = response.getOutputStream();
            outputStream.write(msg.getBytes("UTF-8"));
        } catch (IOException e) {e.printStackTrace();
        } finally {outputStream.flush();
            outputStream.close();}
    }
}

举荐一个 Spring Boot 基础教程及实战示例:
https://www.javastack.cn/cate…

测试 controller 类

import com.simons.cn.springbootdemo.aspect.RateLimitAspect;
import com.simons.cn.springbootdemo.util.ResultUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
/**
 * 类形容:RateLimit 限流测试(基于 注解 + 切面 形式)* 创建人:simonsfan
 */
@Controller
public class TestController {
 
    @ResponseBody
    @RateLimitAspect         // 能够十分不便的通过这个注解来实现限流
    @RequestMapping("/test")
    public String test(){return ResultUtil.success1(1001, "success").toString();}

这样通过自定义注解 @RateLimiterAspect 来动静的加到须要限流的接口上,集体认为是比拟优雅的实现吧。

压测后果:

能够看到,10 个线程中无论压测多少次,并发数总是限度在 6,也就实现了限流。

作者:饭一碗 \
起源:blog.csdn.net/fanrenxiang/*article/details/80949079

近期热文举荐:

1.1,000+ 道 Java 面试题及答案整顿 (2021 最新版)

2. 别在再满屏的 if/ else 了,试试策略模式,真香!!

3. 卧槽!Java 中的 xx ≠ null 是什么新语法?

4.Spring Boot 2.5 重磅公布,光明模式太炸了!

5.《Java 开发手册(嵩山版)》最新公布,速速下载!

感觉不错,别忘了顺手点赞 + 转发哦!

正文完
 0