防暴力破解,限流策略

 

限流组件ratelimit配置

<dependency>
    <groupId>com.marcosbarbero.cloud</groupId>
    <artifactId>spring-cloud-zuul-ratelimit</artifactId>
    <version>2.0.6.RELEASE</version>
</dependency>

注:不能使用版本太低的,版本太低的在配置多个类型的时候有bug。

ratelimit项目源码地址:https://github.com/marcosbarbero/spring-cloud-zuul-ratelimit

接口级别配置实例:

zuul:

  routes:

    cjkj_action:

      path: /v1/ cjkj_action /**

      serviceId: action

  ratelimit:

    key-prefix: service_a     #(redis中shengchengkey的前缀)

    enabled: true

    behind-proxy: true    #(代理之后的ip)

    repository: REDIS      #(存储位置)

    policy-list:

      action:             #(路由服务的id)

        - limit: 2          #(限流次数)

          refresh-interval: 10   #(计算周期  默认1s)

          type:           #(限流的类型)

            - type: url

              matcher: /t1  #(接口地址)

            - type: url

              matcher: /t2

            - type: user

              matcher: system_admin     #(匹配的用户,可以不加此参数)

            - type: origin

              matcher: localhost  #(匹配的ip,可以不加此参数)        

        - limit: 5

          refresh-interval: 20

          type:

            - type: url

              matcher: /t3

           #- type: user

           #  matcher: system_admin

           #- type: origin

           #  matcher: localhost

以上type类型可以是其中一个,也可以是一个以上,如果一个以上,那个限流的策略就是组合限制,使用起来更加灵活。

服务级别配置实例

zuul:

  routes:

    cjkj_action:

      path: /v1/ cjkj_action /**

      serviceId: action

  ratelimit:

    key-prefix: service_a     #(redis中shengchengkey的前缀)

    enabled: true

    behind-proxy: true    #(代理之后的ip)

repository: REDIS      #(存储位置)

default-policy:

      limit: 50

refresh-interval: 60

type: user  

压测接口查看结果

当有两个用户不断请求拦截的接口,可以在redis中根据ip+接口地址url+用户名user作为key查看掉用的次数。

当超出限制的次数,会返回429状态码。

 

猜你喜欢

转载自blog.csdn.net/weixin_39352976/article/details/109065971