{"id":37023503,"url":"https://github.com/shiyujun/syj-ratelimit","last_synced_at":"2026-01-14T02:49:33.677Z","repository":{"id":57716078,"uuid":"147318935","full_name":"shiyujun/syj-ratelimit","owner":"shiyujun","description":"无侵入的应用级网关限流框架,无需配置文件，细粒度控制，高灵活性，高可用性，使用redis+lua脚本实现。","archived":false,"fork":false,"pushed_at":"2025-10-28T06:58:09.000Z","size":136,"stargazers_count":152,"open_issues_count":26,"forks_count":79,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-10-28T08:30:52.158Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shiyujun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2018-09-04T09:09:46.000Z","updated_at":"2025-06-12T02:00:37.000Z","dependencies_parsed_at":"2023-01-29T13:01:01.280Z","dependency_job_id":"0af33067-ea40-458c-a415-09fbb04bffe0","html_url":"https://github.com/shiyujun/syj-ratelimit","commit_stats":null,"previous_names":["2388386839/syj-ratelimit"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/shiyujun/syj-ratelimit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shiyujun%2Fsyj-ratelimit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shiyujun%2Fsyj-ratelimit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shiyujun%2Fsyj-ratelimit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shiyujun%2Fsyj-ratelimit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shiyujun","download_url":"https://codeload.github.com/shiyujun/syj-ratelimit/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shiyujun%2Fsyj-ratelimit/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28408773,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T01:52:23.358Z","status":"online","status_checked_at":"2026-01-14T02:00:06.678Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2026-01-14T02:49:33.193Z","updated_at":"2026-01-14T02:49:33.660Z","avatar_url":"https://github.com/shiyujun.png","language":"Java","funding_links":[],"categories":["容错组件"],"sub_categories":["Spring Cloud框架"],"readme":"# syj-ratelimit\n***\n\n## 项目介绍\n\u003e此项目为一个无侵入的应用级网关限流框架,如果您正在寻找一个网关限流的框架，使用syj-ratelimit是最明智的选择\u003cbr\u003e\n### 为什么选择syj-ratelimit\n1. 无需任何复杂配置文件，一个注解玩转syj-ratelimit\u003cbr\u003e\n2.  细粒度控制，您可以控制同一个类中的A方法每分钟限流100而B方法每分钟限流200\u003cbr\u003e\n3.  高灵活性，可根据自定义信息（如用户id、用户ip、用户权限等）进行限流、可灵活选择限流算法\u003cbr\u003e\n4.  高可用性，使用redis+lua脚本的原子性为分布式系统保驾护航\u003cbr\u003e\n5.  高可扩展性，可灵活添加限流算法\u003cbr\u003e\n## Quick Start\n### 1.  引入syj-ratelimit\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecn.org.zhixiang\u003c/groupId\u003e\n    \u003cartifactId\u003esyj-ratelimit\u003c/artifactId\u003e\n    \u003cversion\u003e1.1.2\u003c/version\u003e\n \u003c/dependency\u003e\n ```\n### 2.  注册syj-ratelimit\n\u003e因为并不是所有的项目都会使用SpringBoot,所以在注册这一步我们分为两种情况\n#### 1.SpringBoot或SpringCloud项目\n您需要在启动类上增加一个注解\n```java\n@EnableSyjRateLimit\n```\n#### 2.Spring\n您需要提供一个可以被Spring管理的配置类。比如说：\n```java\n@Import(EnableSyjRateLimitConfiguration.class)\n@Configuration\npublic class SyjRateLimitConfig {\n}\n```\n### 3. 配置您的redis连接\n\u003e您需要配置您的redis连接为syj-ratelimit，同2的情况我们把项目分为两种情况（注意下方的配置需要根据实际情况调整）\n#### 1.SpringBoot或SpringCloud项目\n```yaml\nspring:\n  redis:\n  \n    #cluster: 集群模式启用\n     # nodes: 10.0.20.135:7010,10.0.20.135:7011,10.0.20.135:7012\n    host: \n    port: \n    password:\n    pool:\n      max-active: 8\n      max-wait: 1\n      max-idle: 8\n      min-idle: 0\n    timeout: 2000\n```\n#### 2. Spring应用\n您只需要注册一个RedisConnectionFactory子类的bean。比如说\n```xml\n\u003cbeans\u003e\n    \u003cbean id=\"poolConfig\" class=\"redis.clients.jedis.JedisPoolConfig\" \u003e\n        \u003cproperty name=\"maxIdle\" value=\"${redis.maxIdle}\" /\u003e\n        \u003cproperty name=\"maxWaitMillis\" value=\"${redis.maxWait}\" /\u003e\n        \u003cproperty name=\"testOnBorrow\" value=\"${redis.testOnBorrow}\" /\u003e\n    \u003c/bean\u003e\n    \u003cbean id=\"connectionFactory\"  class=\"org.springframework.data.redis.connection.jedis.JedisConnectionFactory\" \u003e\n        \u003cproperty name=\"poolConfig\" ref=\"poolConfig\" /\u003e\n        \u003cproperty name=\"port\" value=\"${redis.port}\" /\u003e\n        \u003cproperty name=\"hostName\" value=\"${redis.host}\" /\u003e\n        \u003cproperty name=\"timeout\" value=\"${redis.timeout}\" \u003e\u003c/property\u003e\n        \u003cproperty name=\"database\" value=\"1\"\u003e\u003c/property\u003e\n    \u003c/bean\u003e\n\u003c/beans\u003e\n```\n### 4.  使用syj-ratelimit\n\u003e其实看到这一步的时候您已经可以使用syj-ratelimit来进行限流了哦。\u003cbr\u003e\n\n##### syj-ratelimit为您提供了两个注解来进行限流，它们是@ClassRateLimit和@MethodRateLimit。顾名思义，它们一个是用在类上的一个是用在方法上的。他们的功能是一样的，之所以分出来两个注解的原因就是为了解决当一个类的不同接口需要进行不同的限流方案问题\n\n因为两个注解的内容一样，所以我们先以@ClassRateLimit为例看一下其中的几个属性\n\n```java\npublic @interface ClassRateLimit {\n    /**\n     * 限流类型。默认值：ALL。可选值：ALL,IP,USER,CUSTOM\n     */\n    public CheckTypeEnum checkType() default CheckTypeEnum.ALL;\n    /**\n     * 限流次数。默认值10\n     */\n    public long limit() default 10;\n    /**\n     * 限流时间间隔,以秒为单位。默认值60\n     */\n    public long refreshInterval() default 60;\n\n}\n```\n来几个使用的例子吧\u003cbr\u003e\n1. 限流总资源数。（例如，需要每个方法每30秒只允许调用10次）\u003cbr\u003e\n```java\n@ClassRateLimit(limit = 10,refreshInterval=30)\n@RestController\n@RequestMapping(\"/testClass\")\npublic class TestClassRateLimitController {\n\n    @PostMapping(\"/havaParam\")\n    public void havaParam(@RequestBody Map\u003cString,String\u003e map){\n        System.out.println(\"业务逻辑。。。。\");\n    }\n\n    @GetMapping(\"/noParam\")\n    public void noParam(){\n        System.out.println(\"业务逻辑。。。。\");\n    }\n\n}\n```\n2.  根据IP限流总资源数\n```java\n@ClassRateLimit(limit = 10,refreshInterval=30,checkType = CheckTypeEnum.IP)//每个IP每30秒可以访问10次\n```\n3. 根据自定义信息限流总资源数（自定义时推荐在controller中查出能标识用户唯一性的值放入request中，然后把限流注解添加到service中进行限流）\n```java\n@RestController\n@RequestMapping(\"/testAnnotation\")\npublic class TestRateLimitController {\n\n    @Autowired\n    private TestService testCstom;\n\n    @PostMapping(\"/custom\")\n    public void custom(HttpServletRequest httpServletRequest){\n        //根据一系列操作查出来了用户id\n        httpServletRequest.setAttribute(Const.CUSTOM,\"用户id\");//限流时在httpServletRequest中根据Const.CUSTOM的值进行限流\n        testCstom.testCstom();\n    }\n}\n\n@ClassRateLimit(limit = 10,refreshInterval=30,checkType = CheckTypeEnum.CUSTOM)\npublic class TestService {\n    public  void testCstom(){\n        System.out.println(\"此方法每个key为Const.CUSTOM的用户每30秒可以进入10次\");\n    }\n}\n```\n4.  限流某个方法的并发数\n```java\n@RestController\n@RequestMapping(\"/testAnnotation\")\npublic class TestRateLimitController {\n\n    @PostMapping(\"/defult\")\n    public void defult(){\n        System.out.println(\"没有拦截\");\n    }\n\n    @PostMapping(\"/ip\")\n    public void ip(){\n        System.out.println(\"没有拦截\");\n    }\n\n    @MethodRateLimit(checkType = CheckTypeEnum.USER)\n    @PostMapping(\"/user\")\n    public void user(){\n        System.out.println(\"根据用户信息拦截\");//用户信息取自request.getUserPrincipal()\n    }\n\n}\n```\n\n\n## 更多信息\n\n\u003e相信看完了上方的Quick Start您已经迫不及待的想要将syj-ratelimit应用于生产了。我在这里为您提供了两种限流算法。您可以根据自己系统的需求选择自己需要的算法\n\n#### 限流算法\n\u003e 如果您对限流算法不太了解的话可以先参考一下这篇文章[http://zhixiang.org.cn](http://zhixiang.org.cn)\n1.  计数器法\u003cbr\u003e\n程序默认使用计数器算法进行限流，如果您要使用计数器法的话无需要额外的配置。\n2.  令牌桶算法\u003cbr\u003e\n如果您想要使用令牌桶算法的话，那么有两个需要注意的地方。\n    1. 再配置文件中指定算法为令牌桶算法。（推荐您使用yml文件或者properties文件）\n        1. yml\n        ```yaml\n           syj-rateLimit:\n             algorithm: token\n       ```\n        2. properties\n        ```properties\n               syj-rateLimit.algorithm= token\n       ```\n    2.  您需要将目光放到@ClassRateLimit上的另外两个属性上\n    ```java\n        /**\n         * 向令牌桶中添加数据的时间间隔,以秒为单位。默认值10秒\n         */\n        public long tokenBucketTimeInterval() default 10;\n        /**\n         * 每次为令牌桶中添加的令牌数量。默认值5个\n         */\n        public long tokenBucketStepNum() default 5;\n    ```\n#### 再次开发\n\u003e 如果您想使用别的算法，您可以[在这](https://github.com/shiyujun/syj-ratelimit)fork项目进行开发\n\n为了遵守代码的开闭原则，您在添加新的限流算法时请参考包ratelimit、config和algorithm \n\n#### 作者信息\n1. [个人网站](http://shiyujun.cn)\n2. [GitHub](https://github.com/shiyujun)\n\n\n#### 项目实现设计的技术\n1.  [如何使用Redis执行Lua脚本](https://shiyujun.cn/posts/何使用Redis执行Lua脚本.html)\n2.  [我是如何把自定义注解应用到生产的](https://shiyujun.cn/posts/是如何把自定义注解应用到生产的.html)\n3.  [大型网站限流算法的实现和改造](https://shiyujun.cn/posts/型网站限流算法的实现和改造.html)\n4.  [IDEA中使用lombok插件](https://shiyujun.cn/posts/IDEA中使用lombok插件.html)\n5.  [SpringBoot条件注解@Conditional](https://shiyujun.cn/posts/SpringBoot条件注解@Conditional.html)\n6.  [策略模式](https://shiyujun.cn/posts/策略模式.html)\n7.  [如何将自己的jar包发布到mavan中央仓库](https://shiyujun.cn/posts/如何将自己的jar包发布到mavan中央仓库.html)\n\n\n#### 版本信息\n\n#####  1.0.0\n\u003esyj-ratelimit上线\n#####  1.1.0\n\u003e1. 修复计算令牌数取整问题\n\u003e2. 修复redis集群部署slot不一致问题\n#####  1.1.1\n\u003e1. 修复令牌桶算法脚本的bug\n\n#####  1.1.2\n\u003e1. 将hashTag与当前key绑定，避免集群环境中所有key路由到同一个slot，造成的集群数据倾斜\n\n\u003ch4 style=\"color:red\"\u003e\n欢迎关注我的公众号，专注于Java知识体系学习，Spring、SpringBoot、SpringCloud源码解析 \u003c/h4\u003e\n\n\n\n![1](https://shiyujun.cn/img/gongzhonghao.jpg)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshiyujun%2Fsyj-ratelimit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshiyujun%2Fsyj-ratelimit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshiyujun%2Fsyj-ratelimit/lists"}