87

GitHub - wangzheng0822/ratelimiter4j: Java rate limiting library/framework...

 5 years ago
source link: https://github.com/wangzheng0822/ratelimiter4j
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

Ratelimiter4j

Features

RateLimiter是一个高度容错,低延迟,高性能的限流开发库/框架,提供了对HTTP接口的访问限流功能。其特点:

  • 提供了各种灵活的配置方式,同时支持零配置纯编程使用模式
  • 支持yaml, json等多种限流规则配置格式
  • 支持本地文件配置限流规则或者zookeeper集中配置方式
  • 使用SPI插件式开发模式,支持自定义限流规则格式,限流算法等
  • 提供了基于内存的单机限流算法和基于Redis的分布式限流算法
  • 高度容错,限流框架的任何异常都不会影响业务接口
  • 低延迟,限流框架较小影响业务接口响应时间
  • 支持不同粒度的接口限流,支持接口中包含restful url template variables
  • 灵活的集成方式,方便集成spring开发框架

Prerequisite

RateLimit4J需要Java 8及以上版本

Building

$ git clone https://github.com/wangzheng0822/ratelimiter4j
$ cd ratelimiter4j/
$ gradle build

Quickstart

基于内存的限流,如果不需要特殊配置,使用起来非常简单,如下几行代码即可:

UrlRateLimiter ratelimiter = new MemoryUrlRateLimiter();
try {
  ratelimiter.limit("app-1", "http://www.eudemon.com/v1/user/12345");
  System.out.println("passed");
} catch (OverloadException e) {
  // business logic
} catch (InvalidUrlException e) {
  // business logic
} catch (InternalErrorException e) {
  // business logic
}

限流规则配置,放置在classpath下面:ratelimiter-rule.yaml

configs:
- appId: app-1
  limits:
  - api: /v1/user
    limit: 100
  - api: /v1/order
    limit: 50
- appId: app-2
  limits:
  - api: /v1/user
    limit: 50
  - api: /v1/order
    limit: 50

基于Redis的分布式限流,如果不需要特殊设置,只需要简单配置一下Redis地址,如下:

System.setProperty("ratelimiter.redis.address", "127.0.0.1:6379");
UrlRateLimiter ratelimiter = new DistributedUrlRateLimiter();
try {
  ratelimiter.limit("app-1", "http://www.eudemon.com/v1/user/12345");
  System.out.println("passed");
} catch (OverloadException e) {
  // business logic
} catch (InvalidUrlException e) {
  // business logic
} catch (InternalErrorException e) {
  // business logic
}

限流规则配置,放置在classpath下面:ratelimiter-rule.yaml,同上面的相同

Documentation

更加详细配置及其使用方法及其examples,请参看User Guide开发手册

使用前请先确认性能是否满足应用场景,请务必阅读Benchmark性能测试报告

必要情况下请参照测试文档和benchmark code自行测试RateLimiterBenchmarkTest.java

Roadmap

功能 优先级 进度 分布式限流算法支持Redis cluster & sharding p1 未开发 支持limit接口超时参数 p1 未开发 限流规则支持指定time unit p1 in progress 支持不区分app ID的限流模式 p1 in progress 支持线程并发限制 p1 未开发 限流规则支持GET/POST等http schema p1 in progress 分布式限流算法性能优化 p1 未开发 支持黑白名单和鉴权功能 p1 未开发 支持更多的限流算法 p1 未开发 支持更加通用的限流:db,业务,dubbo等 p1 未开发 支持可以任意指定rule存放的位置file p1 in progress 添加更加完善的监控 p1 未开发

Contacts

提交issue或[email protected]或weixin:wangzheng0822


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK