限流规则管理


创建限流规则

为服务创建多个限流规则,以对服务进行流量限制,按优先级顺序进行匹配,匹配到一个则执行该规则。

请求示例:

  1. POST /naming/v1/ratelimits
  2. # 开启北极星服务端针对控制台接口鉴权开关后,需要添加下面的 header
  3. Header X-Polaris-Token: {访问凭据}
  4. [
  5. {
  6. "name": "rule1",
  7. "service": "testsvc1",
  8. "namespace": "default",
  9. "method": {
  10. "type": "EXACT",
  11. "value": "/getsomething"
  12. },
  13. "arguments": [
  14. {
  15. "type": "HEADER",
  16. "key": "host",
  17. "value": {
  18. "type": "EXACT",
  19. "value": "www.baidu.com"
  20. }
  21. },
  22. {
  23. "type": "CALLER_SERVICE",
  24. "key": "default",
  25. "value": {
  26. "type": "IN",
  27. "value": "testsvc1,testsvc2"
  28. }
  29. }
  30. ],
  31. "resource": "QPS",
  32. "type": "LOCAL",
  33. "amounts": [
  34. {
  35. "maxAmount": 1000,
  36. "validDuration": "1s"
  37. },
  38. {
  39. "maxAmount": 2000,
  40. "validDuration": "1m"
  41. }
  42. ],
  43. "regex_combine": false,
  44. "disable": false,
  45. "failover": "FAILOVER_LOCAL"
  46. }
  47. ]

回复示例:

  1. {
  2. "code": 200000,
  3. "info": "execute success",
  4. "size": 1,
  5. "responses": [
  6. {
  7. "code": 200000,
  8. "info": "execute success",
  9. "rateLimit": {
  10. "id": "e04f201e7b7e4599b42a9b6631a7ba08", //规则ID
  11. "service": "testsvc1",
  12. "namespace": "default",
  13. "name": "rule2"
  14. }
  15. }
  16. ]
  17. }

数据结构:

Ratelimit结构参数

参数名类型描述是否必填
namestring规则名
servicestring规则所属的服务名,创建规则时,如果服务不存在,则会自动创建服务。
namespacestring规则所属的命名空间
methodMatchString规则所针对的服务接口
argumentsMatchArgument请求参数匹配条件,需全匹配才通过
resourcestring限流资源,默认为QPS(针对QPS进行限流)
typestring限流类型,支持LOCAL(单机限流), GLOBAL(分布式限流)
amountsAmount[]限流配额,包含限流周期和配额总数,可配置多个
regex_combinebool合并计算配额,对于匹配到同一条正则表达式规则的多个不同的请求进行合并计算,默认为false
disablebool是否启用该限流规则,默认为false(启用)
actionstring限流效果,支持REJECT(直接拒绝),UNIRATE(匀速排队),默认REJECT
failoverstring失败降级措施,仅分布式限流有效,当远程token服务出现故障时,本地如何降级。支持FAILOVER_LOCAL(降级到单机限流),FAILOVER_PASS(直接通过)。默认FAILOVER_LOCAL
max_queue_delayint最大排队时长,单位秒,仅对匀速排队生效。默认1秒

Amount结构参数

参数名类型描述是否必填
maxAmountuint32周期内最大配额数
validDurationstring周期描述,支持duration类型的字符串,比如1s, 1m, 1h等

MatchString结构参数

参数名类型描述是否必填
typestring匹配类型,枚举,支持:EXACT(全匹配,默认),REGEX(正则表达式匹配),NOT_EQUALS(不等于),IN(包含),NOT_IN(不包含)
valuestring匹配的目标值,如果选择的是包含和不包含,则通过逗号分割多个值

MatchArgument结构参数

参数名类型描述是否必填
typestring参数类型,枚举,支持:CUSTOM(自定义,默认),METHOD(方法),HEADER(请求头),QUERY(请求参数),CALLER_SERVICE(主调方服务),CALLER_IP(主调方IP)
keystring参数键,对于HEADER、QUERY、CUSTOM,对应的是key值;对于CALLER_SERVICE,对应的是服务的命名空间值
valueMatchString参数值,对于HEADER、QUERY、CUSTOM,对应的是key所关联的value;对于CALLER_SERVICE,对应的是服务名,其他类型则是具体的值,支持多种匹配模式(见MatchString的定义)

查询限流规则

请求示例:

  1. GET /naming/v1/ratelimits?参数名=参数值
  2. # 开启北极星服务端针对控制台接口鉴权开关后,需要添加下面的 header
  3. Header X-Polaris-Token: {访问凭据}
参数名类型描述是否必填
idstring规则ID
namestring规则名
servicestring服务名
namespacestring命名空间
methodstring限流接口名,默认为模糊匹配
disablebool规则是否启用,true为不启用,false为启用
briefbool是否只显示概要信息,brief=true时,则不返回规则详情,只返回规则列表概要信息,默认为false
offsetint分页的起始位置,默认为0
limitint每页行数,默认100

应答示例:

  1. {
  2. "code": 200000,
  3. "info": "execute success",
  4. "amount": 2,
  5. "size": 2,
  6. "rateLimits": [
  7. {
  8. "id": "e04f201e7b7e4599b42a9b6631a7ba08",
  9. "service": "testsvc1",
  10. "namespace": "default",
  11. "priority": 0,
  12. "disable": false,
  13. "ctime": "2022-07-26 21:03:50",
  14. "mtime": "2022-07-26 21:03:50",
  15. "revision": "",
  16. "method": {
  17. "value": "/getsomething2"
  18. },
  19. "name": "rule2",
  20. "etime": "2022-07-26 21:03:50"
  21. },
  22. {
  23. "id": "6942526fbac545848cd8fb32a3a55bb6",
  24. "service": "testsvc1",
  25. "namespace": "default",
  26. "priority": 0,
  27. "disable": false,
  28. "ctime": "2022-07-26 10:09:49",
  29. "mtime": "2022-07-26 11:46:07",
  30. "revision": "",
  31. "method": {
  32. "value": "/getsomething"
  33. },
  34. "name": "rule1",
  35. "etime": "2022-07-26 11:46:07"
  36. }
  37. ]
  38. }

修改限流规则

更新服务下的限流规则的相关信息

请求示例:

  1. PUT /naming/v1/ratelimits
  2. # 开启北极星服务端针对控制台接口鉴权开关后,需要添加下面的 header
  3. Header X-Polaris-Token: {访问凭据}
  4. [
  5. {
  6. "id": "e04f201e7b7e4599b42a9b6631a7ba08",
  7. "name": "rule1",
  8. "service": "testsvc1",
  9. "namespace": "default",
  10. "method": {
  11. "type": "EXACT",
  12. "value": "/getsomething"
  13. },
  14. "arguments": [
  15. {
  16. "type": "HEADER",
  17. "key": "host",
  18. "value": {
  19. "type": "EXACT",
  20. "value": "www.baidu.com"
  21. }
  22. },
  23. {
  24. "type": "CALLER_SERVICE",
  25. "key": "default",
  26. "value": {
  27. "type": "IN",
  28. "value": "testsvc1,testsvc2"
  29. }
  30. }
  31. ],
  32. "resource": "QPS",
  33. "type": "LOCAL",
  34. "amounts": [
  35. {
  36. "maxAmount": 1000,
  37. "validDuration": "1s"
  38. },
  39. {
  40. "maxAmount": 2000,
  41. "validDuration": "1m"
  42. }
  43. ],
  44. "regex_combine": false,
  45. "disable": true,
  46. "failover": "FAILOVER_LOCAL"
  47. }
  48. ]

应答示例:

  1. {
  2. "code": 200000,
  3. "info": "execute success",
  4. "size": 1,
  5. "responses": [
  6. {
  7. "code": 200000,
  8. "info": "execute success",
  9. "rateLimit": {
  10. "id": "e04f201e7b7e4599b42a9b6631a7ba08", //规则ID
  11. "service": "testsvc1",
  12. "namespace": "default",
  13. "name": "rule2"
  14. }
  15. }
  16. ]
  17. }

删除限流规则

请求示例:

  1. POST /naming/v1/ratelimits/delete
  2. # 开启北极星服务端针对控制台接口鉴权开关后,需要添加下面的 header
  3. Header X-Polaris-Token: {访问凭据}
  4. [
  5. {
  6. "id": "6942526fbac545848cd8fb32a3a55bb6" //规则ID,必填
  7. }
  8. ]

应答示例:

  1. {
  2. "code": 200000,
  3. "info": "execute success",
  4. "size": 1,
  5. "responses": [
  6. {
  7. "code": 200000,
  8. "info": "execute success",
  9. "rateLimit": {
  10. "id": "6942526fbac545848cd8fb32a3a55bb6"
  11. }
  12. }
  13. ]
  14. }

启用限流规则

请求示例:

  1. PUT /naming/v1/ratelimits/enable
  2. [
  3. {
  4. "id": "6942526fbac545848cd8fb32a3a55bb6", //规则ID,必填
  5. "disable": true // 是否禁用,true为不启用,false为启用
  6. }
  7. ]

应答示例:

  1. {
  2. "code": 200000,
  3. "info": "execute success",
  4. "size": 1,
  5. "responses": [
  6. {
  7. "code": 200000,
  8. "info": "execute success",
  9. "rateLimit": {
  10. "id": "e04f201e7b7e4599b42a9b6631a7ba08",
  11. "disable": false
  12. }
  13. }
  14. ]
  15. }