访问限流

Dubbo 分类

Dubbo当前常用的有2个分支版本,一个是apache dubbo(GroupID是org.apache.dubbo), 一个是dubbox (GroupID是com.alibaba)。两个分支的dubbo,对应不同的接入插件,大家接入之前可以先通过GroupID判断下当前项目依赖的是哪个分支的dubbo。

Apache Dubbo 接入

支持版本

  • dubbo 2.x 版本的接入,最新版本为 0.2.2
  • dubbo 3.x 版本的接入,最新版本为 0.2.0-3.2.7

引入依赖

首先,动态路由需要依赖以下2个插件,需要将2个插件引入POM中,插件版本建议使用最新版本。

<!-- 北极星注册发现插件 -->
<dependency>
     <groupId>com.tencent.polaris</groupId>
     <artifactId>dubbo-registry-polaris</artifactId>
     <version>${version}<version>
</dependency>
<!-- 北极星限流功能插件 -->
<dependency>
     <groupId>com.tencent.polaris</groupId>
     <artifactId>dubbo-ratelimit-polaris</artifactId>
     <version>${version}<version>
</dependency>

配置北极星注册中心地址

接下来,需要添加北极星 registry 的配置,指定北极星的地址及相关配置信息,可以通过配置文件及代码的方式进行指定:

配置文件方式添加:

<dubbo:registry address="polaris://127.0.0.1:8091"/>

dubbo.registry.address=polaris://127.0.0.1:8091

限流规则字段

访问限流可以实现在被调端基于dubbo的请求消息内容来对请求进行流量限制,比如将带了某些user标签的请求限制速率为100/s。

当前支持针对dubbo消息的以下内容进行流量限制:

应用级注册发现

消息类型 dubbo消息内容 路由规则请求类型
消息头 attachment 请求头(HEADER)
RPC接口 dubbo interface 全限定名 接口名称
RPC方法 dubbo interface 下的具体某个方法的名称 请求匹配规则-方法(METHOD)
请求体参数 dubbo 方法入参某个字段 请求匹配规则-请求参数(QUERY)

接口级注册发现

消息类型 dubbo消息内容 路由规则请求类型
消息头 attachment 请求头(HEADER)
RPC方法 method 方法(METHOD)
请求体参数 dubbo 方法入参某个字段 请求匹配规则-请求参数(QUERY),字段格式

功能验证

针对 Dubbo 接口级别的限流

针对 Dubbo 接口下某个方法的限流

代码中无需额外添加限流判断代码,插件会自动在被调端进行请求的限流计算和拦截。

最终限流效果

18:21:58.748 |-ERROR [DubboServerHandler-10.21.22.75:20880-thread-8] .apache.dubbo.rpc.filter.ExceptionFilter:    -|  
[DUBBO] Got unchecked and undeclared exception which called by 10.21.22.75. service: com.tencent.polaris.dubbo.example.
api.GreetingService, method: sayHi, exception: org.apache.dubbo.rpc.RpcException: Failed to invoke service com.tencent.
polaris.dubbo.example.api.GreetingService.sayHi because exceed max service tps., dubbo version: 3.2.6, current host: 
192.168.255.10, error code: 5-36. This may be caused by , go to https://dubbo.apache.org/faq/5/36 to find instructions. 
org.apache.dubbo.rpc.RpcException: Failed to invoke service com.tencent.polaris.dubbo.example.api.GreetingService.sayHi 
because exceed max service tps.

访问限流样例包括2个工程,代表的是主调端和被调端,限流行为工作在被调端。

环境变量设置

export CONFIG_CENTER_ADDR=127.0.0.1:8093
export METADATA_REPORT_ADDR=127.0.0.1:8091
export POLARIS_DISCOVERY_ADDR=127.0.0.1:8091
export REGISTER_MODE=all
export POD_IP={IP}

代码中无需额外添加限流判断代码,插件会自动在被调端进行请求的限流计算和拦截。

最终限流效果

18:21:58.748 |-ERROR [DubboServerHandler-10.21.22.75:20880-thread-8] .apache.dubbo.rpc.filter.ExceptionFilter:    -|  
[DUBBO] Got unchecked and undeclared exception which called by 10.21.22.75. service: com.tencent.polaris.dubbo.example.
api.GreetingService, method: sayHi, exception: org.apache.dubbo.rpc.RpcException: Failed to invoke service com.tencent.
polaris.dubbo.example.api.GreetingService.sayHi because exceed max service tps., dubbo version: 3.2.6, current host: 
192.168.255.10, error code: 5-36. This may be caused by , go to https://dubbo.apache.org/faq/5/36 to find instructions. 
org.apache.dubbo.rpc.RpcException: Failed to invoke service com.tencent.polaris.dubbo.example.api.GreetingService.sayHi 
because exceed max service tps.

代码中无需额外添加限流判断代码,插件会自动在被调端进行请求的限流计算和拦截,假如出现限流,则会返回PolarisBlockException异常给调用端。

访问限流样例包括2个工程,代表的是主调端和被调端,限流行为工作在被调端。