Java 限流方案
在 Java 生态中,除了 Guava 的 RateLimiter,还有多种限流方案可供选择。以下是几种常见的替代方案:
1. Spring Cloud Gateway / Spring Cloud Alibaba Sentinel
适用于: Spring Cloud 微服务架构
// 在Spring Cloud Gateway中的配置
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    return builder.routes()
        .route("qrcode_route", r -> r.path("/api/qrcode/**")
            .filters(f -> f.requestRateLimiter()
                .rateLimiter(RedisRateLimiter.class, config -> {
                    config.setBurstCapacity(20);
                    config.setReplenishRate(10);
                }))
            .uri("http://localhost:8080"))
        .build();
}
2. Resilience4j RateLimiter
适用于: 需要更丰富熔断限流功能的场景
// 添加依赖
implementation 'io.github.resilience4j:resilience4j-ratelimiter:1.7.1'
// 使用示例
RateLimiterConfig config = RateLimiterConfig.custom()
    .limitRefreshPeriod(Duration.ofSeconds(1))
    .limitForPeriod(10)
    .timeoutDuration(Duration.ofMillis(100))
    .build();
RateLimiter rateLimiter = RateLimiter.of("qrcodeLimiter", config);
CheckedRunnable restrictedCall = RateLimiter
    .decorateCheckedRunnable(rateLimiter, () -> generateQrcode());
Try.run(restrictedCall)
    .onFailure(throwable -> response.sendError(429, "请求过于频繁"));
3. Bucket4j
适用于: 需要分布式限流的场景
// 添加依赖
implementation 'com.github.vladimir-bukhtoyarov:bucket4j-core:7.0.0'
// 本地限流示例
Bandwidth limit = Bandwidth.classic(10, Refill.intervally(10, Duration.ofSeconds(1)));
Bucket bucket = Bucket.builder().addLimit(limit).build();
if (bucket.tryConsume(1)) {
    // 处理请求
} else {
    response.sendError(429, "请求过于频繁");
}
4. Redis + Lua 分布式限流
适用于: 分布式环境下的精确限流
// Redis限流脚本
private static final String LIMIT_SCRIPT =
    "local key = KEYS[1]\n" +
    "local limit = tonumber(ARGV[1])\n" +
    "local expire = tonumber(ARGV[2])\n" +
    "local current = tonumber(redis.call('get', key) or 0\n" +
    "if current + 1 > limit then\n" +
    "    return 0\n" +
    "else\n" +
    "    redis.call('INCR', key)\n" +
    "    if current == 0 then\n" +
    "        redis.call('EXPIRE', key, expire)\n" +
    "    end\n" +
    "    return 1\n" +
    "end";
public boolean tryAcquire(String key, int limit, int expireSec) {
    Long result = redisTemplate.execute(
        new DefaultRedisScript<>(LIMIT_SCRIPT, Long.class),
        Collections.singletonList(key),
        String.valueOf(limit), String.valueOf(expireSec));
    return result != null && result == 1;
}
// 使用
if (!redisLimiter.tryAcquire("qrcode:"+ip, 10, 60)) {
    response.sendError(429, "请求过于频繁");
    return;
}
5. Spring AOP 实现简单限流
适用于: 简单的单体应用限流
@Aspect
@Component
public class RateLimitAspect {
    
    private final Map<String, AtomicInteger> counters = new ConcurrentHashMap<>();
    private final Map<String, Long> timestamps = new ConcurrentHashMap<>();
    
    @Around("@annotation(rateLimit)")
    public Object limit(ProceedingJoinPoint pjp, RateLimit rateLimit) throws Throwable {
        String key = rateLimit.key();
        int limit = rateLimit.limit();
        long interval = rateLimit.interval();
        
        long now = System.currentTimeMillis();
        if (timestamps.getOrDefault(key, 0L) + interval < now) {
            timestamps.put(key, now);
            counters.put(key, new AtomicInteger(0));
        }
        
        if (counters.getOrDefault(key, new AtomicInteger(0)).incrementAndGet() > limit) {
            throw new ResponseStatusException(HttpStatus.TOO_MANY_REQUESTS, "请求过于频繁");
        }
        
        return pjp.proceed();
    }
}
// 自定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RateLimit {
    String key() default "";
    int limit() default 10;
    long interval() default 1000; // ms
}
// 在Controller方法上使用
@RateLimit(key = "qrcode", limit = 5, interval = 60000)
@GetMapping("/generate")
public void generateQrcode(...) { ... }
6. Apache Commons Pool 对象池限流
适用于: 需要控制资源并发使用的场景
GenericObjectPool<Object> pool = new GenericObjectPool<>(new BasePooledObjectFactory<>() {
    @Override
    public Object create() {
        return new Object();
    }
});
pool.setMaxTotal(10); // 最大并发数
try {
    Object permit = pool.borrowObject();
    try {
        generateQrcode(...);
    } finally {
        pool.returnObject(permit);
    }
} catch (Exception e) {
    response.sendError(429, "系统繁忙,请稍后再试");
}
选择建议
- 单体应用:Spring AOP 或 Resilience4j
- 微服务架构:Spring Cloud Gateway 或 Sentinel
- 分布式系统:Redis + Lua 或 Bucket4j
- 需要丰富特性:Resilience4j(支持熔断、限流、重试等)
- 简单需求:Guava RateLimiter 仍然是不错的选择
所有方案都可以与你的二维码生成接口集成,根据你的架构复杂度和具体需求选择合适的限流方案。
本文来自博客园,作者:VipSoft 转载请注明原文链接:https://www.cnblogs.com/vipsoft/p/18913881
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号