限流标准CRD

apiVersion: com.tencent.tsf/v1alpha1

  kind:rateLimit

  metadata:

    name: ${RuleName}

    servicename: ${Service Name}

    namespace: ${Namespace}

  spec:

  rules: # 限流规则,可以多条,从前到后依次匹配,全局限流只有一条生效

    - operator: {AND,OR,EXPRESSION, FREQUENCY} # 用来判定TAG的组合方式,AND为全部满足,OR为任意满足,EXPRESSION为表达式, FREQUENCY为限流特有的操作

      tags:

      - type: ${Tag Type} # 系统标签 或 用户自定义标签

        field: ${Tag Filed} # Tag 字段

        operator: ${operator} # {EQ, NE, GT, GE, LT, LE, CONTAINS, NOT CONTAINS, EXPRESSION}

        value: ${value} # Tag 的目标值,一般是 String

      duration: # 间隔,ex:1000ms,5s等

      quota: # 间隔内运行通过的请求数

      type: {CLUSTER, LOCAL} # 限流的类型,分别是集群限流,本地限流

      algorithm: # 限流算法

FREQUENCY为限流特有的操作,operatorFREQUENCY时,Tag只允许有一个,此时的行为会将该 Tag 打散,下面举例:

apiVersion: com.tencent.tsf/v1alpha1

  kind:rateLimit

  metadata:

    name:femas-provider-cbrule

    servicename: provider

    namespace: product

  spec:

    rules:

    - operator: FREQUENCY

      tags:

      - type: SystemTag

        field: "source.service.name"

      duration: "5000ms"

      quota: "10"

      algorithm: "token"

      type: "cluster"

此条规则会将上游的每个service name都作为一个单独的限流器,比如上游有3种服务{A,B,C},每个服务调用provider时,都会有一个对应的限流器,规则是5秒内通过10个请求,好处是不需要配置枚举的规则,但是如果FREQUENCY Tag对应的值太多的话,会大幅降低性能

下面的CRD描述了Provider服务的限流规则:

所有访问Provider服务“insert”接口的流量,每5秒只允许通过2个。

以及所有流量每台实例1秒最多通过十个

apiVersion: com.tencent.tsf/v1alpha1
  kind:rateLimit
  metadata:
    name:femas-provider-cbrule
    servicename: provider
    namespace: product
  spec:
    rules:
    - operator: "AND"
      tags:
      - type: systemTag
        field: "destnation.interface.name"
        operator: "EQUAL"
        value: "insert"
      duration: "5000ms"
      quota: "2"
      algorithm: "token"
      type: "cluster"
    - duration: "1000ms"
      quota: "10"
      type: "local"