限流器 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";
    }
}
相关推荐
油泼辣子多加6 小时前
2025年06月30日Github流行趋势
github
ai小鬼头7 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
寻月隐君11 小时前
Rust 异步编程实践:从 Tokio 基础到阻塞任务处理模式
后端·rust·github
bingGO5499112 小时前
github 集成CICD自动化部署
github
超龄超能程序猿12 小时前
Bitvisse SSH Client 安装配置文档
运维·ssh·github
Natsume171015 小时前
嵌入式开发:GPIO、UART、SPI、I2C 驱动开发详解与实战案例
c语言·驱动开发·stm32·嵌入式硬件·mcu·架构·github
荔枝吻16 小时前
【AI总结】Git vs GitHub vs GitLab:深度解析三者联系与核心区别
人工智能·git·github
幻凡ss16 小时前
github pages使用免费CDN加速-netlify
github·github pages·github cdn·github pages加速·netlify加速github·hexo博客免费cdn加速·个人博客免费cdn加速
我是哪吒19 小时前
分布式微服务系统架构第155集:JavaPlus技术文档平台日更-Java线程池实现原理
后端·面试·github
DeepSeek-大模型系统教程1 天前
推荐 7 个本周 yyds 的 GitHub 项目。
人工智能·ai·语言模型·大模型·github·ai大模型·大模型学习