public class RedisRateLimiter extends Object
基于 Redis 的分布式令牌桶,参照 Guava RateLimiter
主要属性: permitsPerSecond 每秒产生令牌数 maxBurstSeconds 最大存储的令牌秒数 nextFreeTicketMills 下一次释放令牌的时间,毫秒 storedPermits 当前存储的令牌数量 maxPermits 最大存储的令牌数量
| 限定符和类型 | 方法和说明 |
|---|---|
static RedisRateLimiter |
create(String key,
double permitsPerSecond)
创建一个实例,默认最大存储1秒的数量,存储1秒的令牌
|
static RedisRateLimiter |
create(String key,
double permitsPerSecond,
double maxBurstSeconds)
设置一个 maxBurstSeconds 的限流器,initBurstSeconds = maxBurstSeconds
|
static RedisRateLimiter |
create(String key,
double permitsPerSecond,
double maxBurstSeconds,
double initBurstSeconds)
创建一个实例
|
boolean |
tryAcquire(int permits)
尝试获取锁,不等待
|
boolean |
tryAcquire(int permits,
int timeout,
TimeUnit unit)
尝试获取 permits 个令牌
|
long |
tryGetAllPermits()
尝试获取当前可用的数量
|
public boolean tryAcquire(int permits)
permits - 获取的数量public long tryGetAllPermits()
public boolean tryAcquire(int permits,
int timeout,
TimeUnit unit)
throws Exception
permits - 令牌数量timeout - 超时时间unit - 单位Exceptionpublic static RedisRateLimiter create(String key, double permitsPerSecond)
key - keypermitsPerSecond - 每秒产生数量public static RedisRateLimiter create(String key, double permitsPerSecond, double maxBurstSeconds)
key - keypermitsPerSecond - 每秒产生数量maxBurstSeconds - 最大存储的秒数public static RedisRateLimiter create(String key, double permitsPerSecond, double maxBurstSeconds, double initBurstSeconds)
key - keypermitsPerSecond - 每秒产生数量maxBurstSeconds - 最大存储描述initBurstSeconds - 第一次初始化,默认含有的数量Copyright © 2020. All rights reserved.