限流器 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";
    }
}
相关推荐
赵文宇(温玉)1 天前
构建内网离线的“github.com“,完美解决内网Go开发依赖
开发语言·golang·github
牛奶咖啡131 天前
利用Github与Hexo搭建属于自己的在线个人博客
github·hexo创建静态博客·免费部署博客到公网上·创建自定义静态博客·将静态博客上传到github·将自己的网站发布到网上
散峰而望1 天前
C++入门(一)(算法竞赛)
c语言·开发语言·c++·编辑器·github
qq_5470261791 天前
OAuth 2.0 安全授权
git·安全·github
uhakadotcom1 天前
基于 TOON + Next.js 来大幅节省 token 并运行大模型
前端·面试·github
孟陬1 天前
别再社死了!`includeIf` 一招搞定 Git 提交者信息错乱,守护你的邮箱隐私
git·github
Cyril_KI1 天前
大模型长文生成中的幻觉与事实性:研究进展综述
大模型·llm·github·综述·幻觉
逛逛GitHub1 天前
3 个近期"优质"的 AI 开源项目, 有点绝。
架构·github
Pluto5382 天前
第一个app产品的迭代
ios·github
liuccn2 天前
Ubuntu 22.04 离线升级 OpenSSH 到 9.8p1
linux·ubuntu·github