Giter Club home page Giter Club logo

limiter-parent's Introduction

Travis Build Status Coverage Status Maven Central License

Introduction

Limiter 是一个面向具体业务的,轻量级限流器。 Limiter提供的核心能力包括:

  1. Spring Boot支持。自动化配置提供默认功能实现,也可根据业务自定义
  2. 身轻如燕。没有Sentinel心宽体胖。使用Guava LimiterRedisson RRateLimiter实现核心限流逻辑。
  3. 简单的设计。使用Hitter(命中器)和Limiter(限流器)抽象资源命中过程和限流过程
  4. 配置完全抽离。可以拆分到配置中心,动态加载限流策略
  5. 灵活的配置。功能上,即支持静态配置指定资源,又支持动态匹配请求资源,几乎满足常见的业务限流;使用上,提供注解,URL匹配和自定义配置配限制的资源
  6. 多个限流项可重入。多个限流项同时限制访问频率。

Quick Start

源码中limiter-test模块提供了相关Demo,可快速上手。

1. 添加依赖

Note: Limiter需要JDK8以上版本, Spring boot 2.0.

添加maven依赖,其他构建环境参照maven配置

<dependency>
    <groupId>me.fmeng</groupId>
    <artifactId>limiter</artifactId>
    <version>x.y.z</version>
</dependency>

2. 配置

  1. application.yaml配置
    limiter:
      appId: "appId"
      enable: true
      allLimiterTimeoutMilliseconds: 60000
      items:
        - name: "guavaLimiter"
          permits: 1
          limiterStrategyType: ANNOTATION
  2. 被限制访问方法配置
    // 名字和配置文件中保持一致
    @Limiter("guavaLimiter")
    public void limiteMethod() {
        System.out.println("你可以访问");
    }

3. 验证限流结果

// 使用单元测试验证限流效果
TicketServiceTest.guavaAnnotationServiceGuava10pTest

复杂点的例子

1. 业务配置

  1. 限制指定用户ID的访问频率
    resource:
      requestMethods: [GET, POST]
      pathRegex: ".*/test/url/.*"
      params:
      - paramName: "userId"
        paramValues: ["user111", "user222"]
  2. 限制指定用户ID指定请求IP的访问频率
    resource:
      requestMethods: [GET, POST]
      pathRegex: ".*/test/url/.*"
      params:
      - paramName: "userId"
        paramValues: ["user111", "user222"]
      - paramName: "requestIp"
        paramValues: ["127.0.0.1"]
  3. 限制 除了 指定用户ID的访问频率
    resource:
      requestMethods: [GET]
      pathRegex: ".*/test/url/.*"
      reverse: true
      params:
      - paramName: "userId"
        paramValues: ["user111", "user222"]
  4. 根据请求动态构建限流,限制用户ID的访问频率
    resource:
      requestMethods: [GET]
      pathRegex: ".*/test/url/.*"
      params:
      - paramName: "userId"
        dynamic: true
  5. 黑白名单
    # 访问频率设置为0
    permits: 0
    # 配置被限制的资源
    resource:
      ...

2. 自定义扩展,请查看单元测试

  1. 限流器的实现me.fmeng.limiter.CustomLimiterFactory
  2. 在获取请求后,被限流前的自定义操作(添加属性,校验参数等)me.fmeng.limiter.CustomLimiterInterceptor
  3. 重写前端统一返回视图和异常处理策略me.fmeng.limiter.CustomLimiterTranslator

书难尽言,请君指瑕

  1. GitHub Issues
  2. Contact me: [email protected], [email protected]

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.