服务热线
135-6963-3175
Guava的RateLimiter可以实现令牌限流机制
用法:
pom引入:
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>${guava.version}</version> </dependency>
Java:
private RateLimiter limiter = RateLimiter.create(10);//创建可发放的令牌数 public void method() { double time=limiter.acquire();//获取等待时间 if(limiter.tryAcquire()){//尝试获取令牌 // do business ...... } }