限流器 github的ratelimiter

一、pom依赖

java 复制代码
  <!--限流器-->
        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-ratelimiter</artifactId>
            <version>1.7.0</version>
        </dependency>

二、限流器配置

java 复制代码
package com.test.config;

import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

@Configuration
public class RateConfig {
    @Value("${ratelimit.qps.handshake:100}")
    private int handshakeQps; // 默认QPS 100

    @Value("${ratelimit.qps.msg:5000}")
    private int msgQps;       // 默认QPS 5000


    //3s内只能发送一次请求
    @Bean(name = "test")
    public RateLimiter test() {
        RateLimiterConfig rateLimiterConfig = RateLimiterConfig.custom()
                .timeoutDuration(Duration.ofMillis(0))                   // 不等待 直接失败
                .limitRefreshPeriod(Duration.ofMillis(3000))             // 表示每隔多长时间刷新一次限流计数器   这里3s
                .limitForPeriod(1)                                      // 一个限流周期内允许的最大请求数       这里1次
                .build();
        return RateLimiterRegistry.of(rateLimiterConfig).rateLimiter("handshake");
    }


    //握手的限流策略(0.1s内允许10个握手连接)
    @Bean(name = "handshake")
    public RateLimiter handshakeRateLimiter() {
        int tenMillis = 10;  // 时间单位同时除 10
        RateLimiterConfig config = RateLimiterConfig.custom()
                .timeoutDuration(Duration.ofMillis(0))                   // 不等待 直接失败
                .limitRefreshPeriod(Duration.ofMillis(1000 / tenMillis)) // 按 100ms 刷新 更平滑
                .limitForPeriod(handshakeQps / tenMillis)                // 平均 QPS
                .build();
        return RateLimiterRegistry.of(config).rateLimiter("handshake");
    }

    //发送消息的限流策略(0.1s内允许发送500个消息)
    @Bean(name = "sendMsg")
    public RateLimiter sendMsgRateLimiter() {
        int tenMillis = 10;  // 时间单位同时除 10
        RateLimiterConfig config = RateLimiterConfig.custom()
                .timeoutDuration(Duration.ofMillis(0))                    // 不等待 直接失败
                .limitRefreshPeriod(Duration.ofMillis(1000 / tenMillis))  // 按 100ms 刷新 更平滑
                .limitForPeriod(msgQps / tenMillis)                       // 平均 QPS
                .build();
        return RateLimiterRegistry.of(config).rateLimiter("sendMsg");
    }


}

三、限流器使用

java 复制代码
@RestController
public class TestController {


    @Resource(name = "test")
    RateLimiter test;
    @Resource(name = "handshake")
    RateLimiter handshake;
    @Resource(name = "sendMsg")
    RateLimiter sendMsg;

    @GetMapping("/test")
    public String test() {
        boolean b1 = test.acquirePermission();
        boolean b2 = handshake.acquirePermission();
        boolean b3 = sendMsg.acquirePermission();
        System.out.println("sendMsg: " + b1 + "   handshake: " + b2 + "   sendMsg: " + b3);

        return "test";
    }
}
相关推荐
松哥_ai自动化2 小时前
从抓包GitHub Copilot认证请求,认识OAuth 2.0技术
github·copilot
qianmoQ10 小时前
GitHub 趋势日报 (2025年07月15日)
github
handsomestWei10 小时前
GitHub Jekyll博客本地Win开发环境搭建
github·jekyll·blog博客·windows开发环境
DogDaoDao12 小时前
GitHub开源轻量级语音模型 Vui:重塑边缘智能语音交互的未来
大模型·github·音视频·交互·vui·语音模型·智能语音
一小池勺14 小时前
🚀 Git 如何让文件存在于远程仓库却不被本地追踪?
git·github
小华同学ai14 小时前
惊喜! Github 10k+ star 的国产流程图框架,LogicFlow 能解你的图编辑痛点?
前端·后端·github
mortimer14 小时前
为 Index-TTS 打造一个开箱即用的 Windows 整合包:从环境隔离到依赖难题的解决
人工智能·python·github
Cyan_RA914 小时前
写译 — 我靠!短进程优先调度算法究竟是怎么一回事?
面试·github·代码规范
Albert_Lsk20 小时前
【2025/07/16】GitHub 今日热门项目
人工智能·开源·github·开源协议
用户77853718369621 小时前
从抓包GitHub Copilot认证请求,认识OAuth 2.0技术
黑客·github·设计