Skip to content

Particle

Yizzuide edited this page Nov 3, 2020 · 5 revisions

Instructions(模块说明)

Particle is a limiter which includes idempotent, times, bloom and extendable barrier limiter.

Particle是一个限制器模块,包含了去重,次数限制,布隆过滤,可扩展组合限制器

Dependencies(依赖)

<dependency>
  <groupId>com.github.yizzuide</groupId>
  <artifactId>milkomeda-spring-boot-starter</artifactId>
  <version>${milkomeda-last-version}</version>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

Enable with annotation(启用模块)

@EnableParticle
@SpringBootApplication
public class MilkomedaDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(MilkomedaDemoApplication.class, args);
    }
}

Config with application.yml(在Spring Boot项目的application.yml里配置)

milkomeda:
  particle:
    enable-filter: true
    # Add multiple limiters
    # 添加多个限制器
    limiters:
      -
        # Set a limiter instance name
        # 设置限制器实例名
        name: noRepeatLimiter
        # The URL that needs to be filtered
        # 需要过滤请求URL
        include-urls: ["/particle/pay"]
        # Config what to reponse if limited successed
        # 如果被限制,配置响应返回
        response:
          # HTTP standard status code
          # 实际HTTP响应码
          status: 200
          # Customize the code in the reponse body, the 'code' name can be modified by express 'code[error]'
          # 自定义消息体里的code,这个'code'名可以使用表达式'code[error]'替换,实际响应字段将会是error
          code: -1
          # Customize the message in the reponse body
          # 自定义消息体里的message
          message: Too many requests, please try again later!
      -
        name: timesLimiter
        # Set current limiter type, which default is idempotent (case insensitive)
        # 设置当前限制器的类型,默认为idempotent(大小写不敏感)
        type: TIMES
        # Customize the properties of the current limiter (Below is no more than 3 times per minute)
        # 自定义当前限制器的属性(如下是一分钟不超过3次)
        props:
          timesType: MIN
          limitTimes: 3
        include-urls: ["/particle/pay"]
        # Modify the response content type, which default is application/json
        # 修改响应类型,默认为application/json
        # response-content-type: text/plain 
        response:
          status: 200
          # Set response content if reponse content type not application/json
          # 非application/json类型的响应内容
          # content: success
          code: -1
          message: Request over limit, please try again later!
          # You can add custom response fields with 'addition'
          # 你可以通过addition添加自定义响应字段
          addition:
            data: ${collections.emptyMap}
      -
        name: barrierLimiter
        type: BARRIER
        props:
          # Add limiters to the filter chain
          # 添加限制器到过滤链
          chain: ["noRepeatLimiter", "timesLimiter"]
        # Set filter chain order (small Numbers are executed first)
        # 设置排序(数字小的先执行)
        order: -1
        include-urls: ["/particle/pay"]
      -
        name: userBloomLimiter
        type: BLOOM
        props:
          # Bitmap key
          bitKey: bit_user
          # The amount of data
          # 用户量
          insertions: 2000000
        # Custom limit key, instructions for using expressions refer to Comet usage documentation
        # 自定义限制key,表达式参考Comet的使用文档
        key-tpl: limit_{method}_{uri}_{$params.phone}
        include-urls: ["/particle/exists"]
        response:
          status: 200
          code: -1
          message: The user does not exist, please register first
    # Filter urls that need to be excluded globally
    # 全局需要排除的过滤URL
    exclude-urls: ["/favicon.ico"]