dubbo全局异常处理_dubbo对异常的一些处理

看了一下这个文章《Dubbo如何处理业务异常,这个一定要知道哦》,但是不赞同文章中的一些论点,这里整理一下dubbo源码中Provider端的ExceptionFilter中

对异常的处理逻辑。

在Dubbo的provider端的com.alibaba.dubbo.rpc.filter.ExceptionFilter中,有如下几个规则:如果是checked异常,直接抛出

在方法签名上有声明,直接抛出, 未在方法签名上定义的异常,在Provider端打印ERROR日志

异常类和接口类在同一jar包里,直接抛出

是JDK自带(以java.或者javax.开头)的异常,直接抛出

是Dubbo本身的异常(RpcException),直接抛出

如果以上几个规则都不满足的话,则将异常包装为RuntimeException抛给Consumer端

其中一个比较有意思的点就是如何根据一个class判断他所属的jar:

在ExceptionFilter中的逻辑是如下的:// 异常类和接口类在同一jar包里,直接抛出

String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface());

String exceptionFile = ReflectUtils.getCodeBase(exception.getClass());

if (serviceFile == null || exceptionFile == null || serviceFile.equals(exceptionFile)){

return result;

}

其中ReflectUtils.getCodeBase的代码如下:public static String getCodeBase(Class> cls) {

if (cls == null)

return null;

ProtectionDomain domain = cls.getProtectionDomain();

if (domain == null)

return null;

CodeSource source = domain.getCodeSource();

if (source == null)

return null;

URL location = source.getLocation();

if (location == null)

return null;

return location.getFile();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值