|
| 1 | +package io.github.dunwu.distributed.ratelimit; |
| 2 | + |
| 3 | +import cn.hutool.core.collection.CollectionUtil; |
| 4 | +import cn.hutool.core.io.FileUtil; |
| 5 | +import cn.hutool.core.io.resource.ResourceUtil; |
| 6 | +import cn.hutool.core.util.RandomUtil; |
| 7 | +import cn.hutool.core.util.StrUtil; |
| 8 | +import redis.clients.jedis.Jedis; |
| 9 | +import redis.clients.jedis.exceptions.JedisConnectionException; |
| 10 | + |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.List; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | + |
| 15 | +/** |
| 16 | + * 基于 Redis + Lua 实现的令牌桶限流算法 |
| 17 | + * |
| 18 | + * @author <a href="mailto:forbreak@163.com">Zhang Peng</a> |
| 19 | + * @date 2024-01-23 |
| 20 | + */ |
| 21 | +public class RedisTokenBucketRateLimiter implements RateLimiter { |
| 22 | + |
| 23 | + private static final String REDIS_HOST = "localhost"; |
| 24 | + |
| 25 | + private static final int REDIS_PORT = 6379; |
| 26 | + |
| 27 | + private static final Jedis JEDIS; |
| 28 | + |
| 29 | + public static final String SCRIPT; |
| 30 | + |
| 31 | + static { |
| 32 | + // Jedis 有多种构造方法,这里选用最简单的一种情况 |
| 33 | + JEDIS = new Jedis(REDIS_HOST, REDIS_PORT); |
| 34 | + |
| 35 | + // 触发 ping 命令 |
| 36 | + try { |
| 37 | + JEDIS.ping(); |
| 38 | + System.out.println("jedis 连接成功"); |
| 39 | + } catch (JedisConnectionException e) { |
| 40 | + e.printStackTrace(); |
| 41 | + } |
| 42 | + |
| 43 | + SCRIPT = FileUtil.readString(ResourceUtil.getResource("scripts/token_bucket_rate_limit.lua"), |
| 44 | + StandardCharsets.UTF_8); |
| 45 | + } |
| 46 | + |
| 47 | + private final long qps; |
| 48 | + private final long capacity; |
| 49 | + private final String tokenKey; |
| 50 | + private final String timeKey; |
| 51 | + |
| 52 | + public RedisTokenBucketRateLimiter(long qps, long capacity, String tokenKey, String timeKey) { |
| 53 | + this.qps = qps; |
| 54 | + this.capacity = capacity; |
| 55 | + this.tokenKey = tokenKey; |
| 56 | + this.timeKey = timeKey; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean tryAcquire(int permits) { |
| 61 | + long now = System.currentTimeMillis(); |
| 62 | + List<String> keys = CollectionUtil.newLinkedList(tokenKey, timeKey); |
| 63 | + List<String> args = CollectionUtil.newLinkedList(String.valueOf(permits), String.valueOf(qps), |
| 64 | + String.valueOf(capacity), String.valueOf(now)); |
| 65 | + Object eval = JEDIS.eval(SCRIPT, keys, args); |
| 66 | + long value = (long) eval; |
| 67 | + return value != -1; |
| 68 | + } |
| 69 | + |
| 70 | + public static void main(String[] args) throws InterruptedException { |
| 71 | + |
| 72 | + int qps = 20; |
| 73 | + int bucket = 100; |
| 74 | + RedisTokenBucketRateLimiter redisTokenBucketRateLimiter = |
| 75 | + new RedisTokenBucketRateLimiter(qps, bucket, "token:rate:limit", "token:rate:limit:time"); |
| 76 | + |
| 77 | + // 先将令牌桶预热令牌申请完,后续才能真实反映限流 QPS |
| 78 | + redisTokenBucketRateLimiter.tryAcquire(bucket); |
| 79 | + TimeUnit.SECONDS.sleep(1); |
| 80 | + |
| 81 | + // 模拟在一分钟内,不断收到请求,限流是否有效 |
| 82 | + int seconds = 60; |
| 83 | + long okNum = 0L; |
| 84 | + long total = 0L; |
| 85 | + long beginTime = System.currentTimeMillis(); |
| 86 | + for (int second = 0; second < seconds; second++) { |
| 87 | + int num = RandomUtil.randomInt(qps, 100); |
| 88 | + for (int i = 0; i < num; i++) { |
| 89 | + total++; |
| 90 | + if (redisTokenBucketRateLimiter.tryAcquire(1)) { |
| 91 | + okNum++; |
| 92 | + System.out.println("请求成功"); |
| 93 | + } else { |
| 94 | + System.out.println("请求限流"); |
| 95 | + } |
| 96 | + } |
| 97 | + TimeUnit.SECONDS.sleep(1); |
| 98 | + } |
| 99 | + long endTime = System.currentTimeMillis(); |
| 100 | + long time = (endTime - beginTime) / 1000; |
| 101 | + System.out.println(StrUtil.format("请求通过数:{},总请求数:{},实际 QPS:{}", okNum, total, okNum / time)); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments