限流器 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";
    }
}
相关推荐
wangruofeng7 小时前
7.1 万 Star 的 MinerU,把 PDF 变成 LLM 能读懂的 Markdown
github
wangruofeng7 小时前
75k Star 的 OCR 传奇——Tesseract 的 40 年,从 HP 实验室到 LSTM 神经网络
github
逛逛GitHub10 小时前
又挖到 3 个不错的 GitHub 项目,尤其是第 2 个。
github
逛逛GitHub10 小时前
GitHub 上 13 万星的爬虫神器,不要 API Key 就能用了。
github
一点一木17 小时前
🚀 2026 年 6 月 GitHub 十大热门项目排行榜 🔥
人工智能·github
OpenTiny社区2 天前
从零开发 AI 聊天页要两周?试试这款 Vue3 垂直对话组件库 TinyRobot,直接开箱即用
前端·vue.js·github
逛逛GitHub2 天前
2 万多 Star!Google 开源了这个神级 GitHub 项目。
github
逛逛GitHub2 天前
免费 Token 烧掉 5 万亿之后,他们出了个一站式创作平台。
github
用户805533698032 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
inhere2 天前
eget:不用等中央仓库,直接安装 GitHub 和任意下载站的工具
程序员·开源·github