{"id":18600615,"url":"https://github.com/houbb/rate-limit","last_synced_at":"2025-06-14T15:32:27.769Z","repository":{"id":105212090,"uuid":"142883622","full_name":"houbb/rate-limit","owner":"houbb","description":"🎃Rate limit for java.(java 渐进式限流工具框架，支持 spring springboot 字节码)","archived":false,"fork":false,"pushed_at":"2025-06-02T11:08:50.000Z","size":255,"stargazers_count":30,"open_issues_count":0,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-02T23:04:13.320Z","etag":null,"topics":["cache","limit","limiter","rate-limit","rate-limiter","rate-limiting"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/houbb.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-07-30T14:02:54.000Z","updated_at":"2025-06-02T11:08:55.000Z","dependencies_parsed_at":"2023-05-19T16:00:30.871Z","dependency_job_id":null,"html_url":"https://github.com/houbb/rate-limit","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/houbb/rate-limit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houbb%2Frate-limit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houbb%2Frate-limit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houbb%2Frate-limit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houbb%2Frate-limit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/houbb","download_url":"https://codeload.github.com/houbb/rate-limit/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/houbb%2Frate-limit/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259837768,"owners_count":22919465,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","limit","limiter","rate-limit","rate-limiter","rate-limiting"],"created_at":"2024-11-07T02:04:44.357Z","updated_at":"2025-06-14T15:32:27.760Z","avatar_url":"https://github.com/houbb.png","language":"Java","funding_links":[],"categories":["容错组件"],"sub_categories":["Spring Cloud框架"],"readme":"# 项目简介\n\n[rate-limit](https://github.com/houbb/rate-limit) 是一个为 java 设计的渐进式限流工具。\n\n目的是为了深入学习和使用限流，后续将会持续迭代。\n\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.houbb/rate-limit/badge.svg)](http://mvnrepository.com/artifact/com.github.houbb/rate-limit)\n[![Build Status](https://www.travis-ci.org/houbb/rate-limit.svg?branch=master)](https://www.travis-ci.org/houbb/rate-limit?branch=master)\n[![](https://img.shields.io/badge/license-Apache2-FF0080.svg)](https://github.com/houbb/rate-limit/blob/master/LICENSE.txt)\n[![Open Source Love](https://badges.frapsoft.com/os/v2/open-source.svg?v=103)](https://github.com/houbb/rate-limit)\n\n## 特性\n\n- 渐进式实现\n\n- 支持独立于 spring 使用\n\n- 支持整合 spring\n\n- 支持整合 spring-boot\n\n- 内置多种限流策略\n\n# 变更日志\n\n\u003e [CHANGELOG](https://github.com/houbb/rate-limit/blob/master/CHANGELOG.md)\n\n# 快速开始\n\n## 需求\n\n- jdk 1.7 \n\n- maven 3.x+\n\n## maven 导入\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.houbb\u003c/groupId\u003e\n    \u003cartifactId\u003erate-limit-core\u003c/artifactId\u003e\n    \u003cversion\u003e1.1.1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## 入门例子\n\n### 方法定义\n\n`@RateLimit` 限流注解放在方法上，指定对应的限制频率。\n\n也可以定义在类上，默认下面的所有方法生效。方法上的优先级高于类。\n\n| 属性       | 说明           | 默认值                |\n|:---------|:-------------|:-------------------|\n| value    | 方法访问一次消耗的令牌数 | `1`                |\n| timeUnit | 时间单位         | `TimeUnit.SECONDS` |\n| interval | 时间间隔         | `60`               |\n| count    | 可调用次数        | `1000`             |\n| enable   | 是否启用         | true               |\n\n默认为 60S 内，可以调用 1000 次。\n\n```java\npublic class UserService {\n\n    @RateLimit(interval = 2, count = 5)\n    public void limitCount() {\n        log.info(\"{}\", Thread.currentThread().getName());\n    }\n\n}\n```\n\n这个例子中我们 2S 内最多调用 5 次。\n\n### 代码测试\n\n`RateLimitProxy.getProxy(xxx)` 通过字节码获取方法对应的方法代理。\n\n```java\n@Test(expected = RateLimitRuntimeException.class)\npublic void limitCountErrorTest() {\n    UserService userService = RateLimitProxy.getProxy(new UserService());\n    for(int i = 0; i \u003c 3; i++) {\n        userService.limitCount();\n    }\n}\n```\n\n当调用超出限制时，默认抛出 `RateLimitRuntimeException` 异常。\n\n这里默认使用的是令牌桶算法，所以会出现异常。\n\n### 重复注解 @RateLimits\n\n有时候我们希望同时做多个的限制：\n\n（1）一分钟不超过 10 次\n\n（2）一小时不超过 30 次\n\n为了支持多个配置，我们引入了新的注解 `@RateLimits`，可以指定一个 `@RateLimit` 数组。\n\n方法上同时使用 `@RateLimits` + `@RateLimit` 是可以同时生效的，不过为了简单，一般不建议混合使用。\n\n```java\n@RateLimits({@RateLimit(interval = 2, count = 5)})\npublic void limitCount() {\n    //...\n}\n```\n\n\n### 指定引导类\n\n```java\nRateLimitProxy.getProxy(new UserService());\n```\n\n等价于 \n\n```java\nRateLimitProxy.getProxy(new UserService(), RateLimitBs.newInstance());\n```\n\n下面我们来一起看一下 RateLimitBs 引导类。\n\n## 引导类\n\n`RateLimitBs` 作为引导类，便于用户自定义配置。\n\n| 方法                | 说明        | 默认值                                        |\n|:------------------|:----------|:-------------------------------------------|\n| rateLimit         | 限流策略      | `RateLimits.tokenBucket()` 令牌桶算法           |\n| timer             | 时间策略      | `Timers.system()` 系统时间                     |\n| cacheService      | 缓存策略      | `CommonCacheServiceMap` 基于本地 map 的缓存策略     |\n| cacheKeyNamespace | 缓存KEY命名空间 | `RATE-LIMIT` 避免不同的应用，命名冲突。                 |\n| configService     | 限制配置策略    | `RateLimitConfigService` 默认基于方法上的注解        |\n| tokenService      | 身份标识策略    | `RateLimitTokenService` 默认基于 IP            |\n| methodService     | 方法标识策略    | `RateLimitMethodService` 默认基于方法名+参数类型      |\n| rejectListener    | 拒绝策略      | `RateLimitRejectListenerException` 限流时抛出异常 |\n\n其中 rateLimit 内置 `RateLimits` 工具中的策略如下：\n\n| 方法 | 说明 |\n|:---|:---|\n| fixedWindow() | 固定窗口 |\n| slideWindow(int windowNum) | 滑动窗口，可指定窗口大小 |\n| slideWindow() | 滑动窗口，默认为 10 |\n| slideWindowQueue() | 滑动窗口，基于队列的实现 |\n| leakyBucket() | 漏桶算法 |\n| tokenBucket() | 令牌桶算法 |\n\n### 配置建议\n\n1. 分布式系统，cacheService 建议使用基于 redis 的集中式缓存策略。\n\n2. configService 如果想更加灵活，可以基于数据库的配置查询\n\n### RateLimitBs 引导类\n\nRateLimitBs 默认配置如下：\n\n```java\nRateLimitBs.newInstance()\n      .timer(Timers.system())\n      .methodService(new RateLimitMethodService())\n      .tokenService(new RateLimitTokenService())\n      .rejectListener(new RateLimitRejectListenerException())\n      .configService(new RateLimitConfigService())\n      .cacheService(new CommonCacheServiceMap())\n      .rateLimit(RateLimits.tokenBucket())\n      .cacheKeyNamespace(RateLimitConst.DEFAULT_CACHE_KEY_NAMESPACE);\n```\n\n# spring 整合\n\n## maven 引入\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.houbb\u003c/groupId\u003e\n    \u003cartifactId\u003erate-limit-spring\u003c/artifactId\u003e\n    \u003cversion\u003e1.1.1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## 类定义\n\n### 方法\n\n和上面使用类似，直接在方法上声明 `@RateLimit` 注解即可。\n\n```java\n@Service\npublic class UserService {\n\n    private static final Log log = LogFactory.getLog(UserService.class);\n\n    @RateLimit(interval = 2, count = 5)\n    public void limitCount() {\n        log.info(\"{}\", Thread.currentThread().getName());\n    }\n\n}\n```\n\n### 配置\n\n通过 `@EnableRateLimit` 声明启用限流。\n\n```java\n@Configuration\n@ComponentScan(\"com.github.houbb.rate.limit.test.core\")\n@EnableRateLimit\npublic class SpringConfig {\n\n}\n```\n\n`@EnableRateLimit` 的属性配置和 RateLimitBs 属性是以一一对应的。\n\n| 方法 | 说明 | 默认值 |\n|:---|:---|:---|\n| rateLimit | 限流策略 | 令牌桶算法 |\n| timer | 时间策略 | 系统时间 |\n| cacheService | 缓存策略 | 基于本地 map 的缓存策略 |\n| cacheKeyNamespace | 缓存KEY命名空间 | `RATE-LIMIT` 避免不同的应用，命名冲突。|\n| configService | 限制配置策略 | 默认基于方法上的注解 |\n| tokenService | 身份标识策略 | 默认基于 IP |\n| methodService | 方法标识策略 | 默认基于方法名+参数类型 |\n| rejectListener | 拒绝策略 | 限流时抛出异常 |\n\n这里的属性值，都是对应的 spring bean 名称，支持用户自定义。\n\n# spring-boot 整合\n\n## maven 引入\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.houbb\u003c/groupId\u003e\n    \u003cartifactId\u003erate-limit-springboot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e1.1.1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## 使用\n\n其他和 spring 保持一致。\n\n# 后期 Road-MAP\n\n- [x] `@RateLimit` 类级别 public 方法支持\n\n- [x] `@RateLimit` 多注解支持\n\n## 开源矩阵\n\n下面是一些缓存系列的开源矩阵规划。\n\n| 名称 | 介绍 | 状态  |\n|:---|:---|:----|\n| [resubmit](https://github.com/houbb/resubmit) | 防止重复提交核心库 | 已开源 |\n| [rate-limit](https://github.com/houbb/rate-limit) | 限流核心库 | 已开源 |\n| [cache](https://github.com/houbb/cache) | 手写渐进式 redis | 已开源 |\n| [lock](https://github.com/houbb/lock) | 开箱即用的分布式锁 | 已开源 |\n| [common-cache](https://github.com/houbb/common-cache) | 通用缓存标准定义 | 已开源 |\n| [redis-config](https://github.com/houbb/redis-config) | 兼容各种常见的 redis 配置模式 | 已开源 |\n| [quota-server](https://github.com/houbb/quota-server) | 限额限次核心服务 | 待开始 |\n| [quota-admin](https://github.com/houbb/quota-admin) | 限额限次控台 | 待开始 |\n| [flow-control-server](https://github.com/houbb/flow-control-server) | 流控核心服务 | 待开始 |\n| [flow-control-admin](https://github.com/houbb/flow-control-admin) | 流控控台 | 待开始 |","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoubb%2Frate-limit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhoubb%2Frate-limit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoubb%2Frate-limit/lists"}