Coder Social home page Coder Social logo

ratelimiter4j's Introduction

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

作者还写了一篇科普文章,发表于Infoq的架构公众号(聊聊架构):微服务接口限流的设计与思考

更加详细配置及其使用方法及其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 未开发

ratelimiter4j's People

Contributors

wangzheng0822 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.