调用拦截扩展
扩展说明
服务提供者和服务消费者调用过程拦截。Dubbo 本身的大部分功能都是基于此扩展点实现的。每次执行远程方法时,都会执行此拦截。请注意对性能的影响。
协议
- 默认情况下,用户定义的过滤器位于内置过滤器之后。
- 特殊值
default
,表示插入默认扩展点的位置。例如:filter="xxx,default,yyy"
,表示xxx
在默认过滤器之前,yyy
在默认过滤器之后。 - 特殊符号
-
表示剔除。例如:filter="-foo1"
,排除添加默认扩展点foo1
。例如:filter="-default"
,删除所有默认扩展点。 - 当提供者和服务同时配置过滤器时,所有过滤器都会累积而不是覆盖。例如:
<dubbo:provider filter="xxx,yyy"/>
和<dubbo:service filter="aaa,bbb" />
,则xxx
、yyy
、aaa
、bbb
都将生效。如果要覆盖,则需要配置:<dubbo:service filter="-xxx,-yyy,aaa,bbb" />
扩展端口
org.apache.dubbo.rpc.Filter
扩展配置
<!-- Consumer call process interception -->
<dubbo:reference filter="xxx,yyy" />
<!-- The default interceptor of the consumer call process, which will intercept all references -->
<dubbo:consumer filter="xxx,yyy"/>
<!-- provider call process interception -->
<dubbo:service filter="xxx,yyy" />
<!-- Provider call process default interceptor, will intercept all services -->
<dubbo:provider filter="xxx,yyy"/>
已知扩展
org.apache.dubbo.rpc.filter.EchoFilter
org.apache.dubbo.rpc.filter.GenericFilter
org.apache.dubbo.rpc.filter.GenericImplFilter
org.apache.dubbo.rpc.filter.TokenFilter
org.apache.dubbo.rpc.filter.AccessLogFilter
org.apache.dubbo.rpc.filter.CountFilter
org.apache.dubbo.rpc.filter.ActiveLimitFilter
org.apache.dubbo.rpc.filter.ClassLoaderFilter
org.apache.dubbo.rpc.filter.ContextFilter
org.apache.dubbo.rpc.filter.ConsumerContextFilter
org.apache.dubbo.rpc.filter.ExceptionFilter
org.apache.dubbo.rpc.filter.ExecuteLimitFilter
org.apache.dubbo.rpc.filter.DeprecatedFilter
扩展示例
Maven 项目结构
src
|-main
|-java
|-com
|-xxx
|-XxxFilter.java (implement the Filter interface)
|-resources
|-META-INF
|-dubbo
|-org.apache.dubbo.rpc.Filter (plain text file, content: xxx=com.xxx.XxxFilter)
XxxFilter.java
package com.xxx;
import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcException;
public class XxxFilter implements Filter {
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
// before filter...
Result result = invoker.invoke(invocation);
// after filter...
return result;
}
}
META-INF/dubbo/org.apache.dubbo.rpc.Filter
xxx=com.xxx.XxxFilter
上次修改时间:2023 年 1 月 2 日:增强英文文档 (#1798) (95a9f4f6c1c)