服務提供方和服務消費方調用過程攔截,Dubbo 自己的大多功能均基於此擴展點實現,每次遠程方法執行,該攔截都會被執行,請注意對性能的影響。java
約定:git
default
,表示缺省擴展點插入的位置。好比:filter="xxx,default,yyy"
,表示 xxx
在缺省 filter 以前,yyy
在缺省 filter 以後。-
,表示剔除。好比: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" />
簡單示例: githubgithub
1, 代碼api
import com.alibaba.dubbo.common.Constants; import com.alibaba.dubbo.common.extension.Activate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.dubbo.rpc.*; import java.util.List; @Activate(group = Constants.PROVIDER, order = -999) public class AuthorityFilter implements Filter { private static final Logger logger = LoggerFactory.getLogger(AuthorityFilter.class); @Override public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { logger.info("接口名:{}",invocation.getInvoker().getInterface().getName()); logger.info("方法名:{}",invocation.getMethodName()); String clientIp = RpcContext.getContext().getRemoteHost(); logger.info("訪問ip爲{}", clientIp); return invoker.invoke(invocation); } }
2, 配置 MATA-INF\dubbo\com.alibaba.dubbo.rpc.Filter 文件下面內容ide
AuthorityFilter=zzhiDubboprovider.filter.AuthorityFilter
3,測試:性能
2017-12-21 10:47:01.906 INFO 10820 --- [:20880-thread-2] z.filter.AuthorityFilter : 接口名:zzhiDubboapi.service.PeopleService 2017-12-21 10:47:01.908 INFO 10820 --- [:20880-thread-2] z.filter.AuthorityFilter : 方法名:findPeople 2017-12-21 10:47:01.908 INFO 10820 --- [:20880-thread-2] z.filter.AuthorityFilter : 訪問ip爲10.255.2.122