dubbo建议请求异常返回堆栈而不是错误码(异常也是dubbo判断调用是否成功的一个因素,如果返回错误码集群的失败重试就会失效),下面我们分别从provider与consumer进行分析。

provider

我们知道dubbo将服务包装为invoker,下面为AbstractInvoker源码

public Result invoke(Invocation inv) throws RpcException {省略...try {//实际执行业务方法return doInvoke(invocation);} catch (InvocationTargetException e) { // 反射调用发生异常,将异常封装到RpcResultThrowable te = e.getTargetException();if (te == null) {return new RpcResult(e);} else {if (te instanceof RpcException) {((RpcException) te).setCode(RpcException.BIZ_EXCEPTION);}return new RpcResult(te);}} catch (RpcException e) {if (e.isBiz()) {return new RpcResult(e);} else {throw e;}} catch (Throwable e) {return new RpcResult(e);}}

异常过滤器 ExceptionFilter

public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {try {Result result = invoker.invoke(invocation);if (result.hasException() && GenericService.class != invoker.getInterface()) {try {Throwable exception = result.getException();// 如果是检查异常直接返回结果if (!(exception instanceof RuntimeException) && (exception instanceof Exception)) {return result;}// 如果接口已经声明了抛出该异常直接返回结果try {Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());Class<?>[] exceptionClassses = method.getExceptionTypes();for (Class<?> exceptionClass : exceptionClassses) {if (exception.getClass().equals(exceptionClass)) {return result;}}} catch (NoSuchMethodException e) {return result;}// 如果是未声明的运行时异常在日志中输出异常(换句话说,除了这种情况,provider是不会打印异常的)logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost()+ ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName()+ ", exception: " + exception.getClass().getName() + ": " + exception.getMessage(), exception);// directly throw if exception class and interface class are in the same jar file.String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface());String exceptionFile = ReflectUtils.getCodeBase(exception.getClass());if (serviceFile == null || exceptionFile == null || serviceFile.equals(exceptionFile)) {return result;}// directly throw if it's JDK exceptionString className = exception.getClass().getName();if (className.startsWith("java.") || className.startsWith("javax.")) {return result;}// directly throw if it's dubbo exceptionif (exception instanceof RpcException) {return result;}// otherwise, wrap with RuntimeException and throw back to the clientreturn new RpcResult(new RuntimeException(StringUtils.toString(exception)));} catch (Throwable e) {logger.warn("Fail to ExceptionFilter when called by " + RpcContext.getContext().getRemoteHost()+ ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName()+ ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);return result;}}return result;} catch (RuntimeException e) {logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost()+ ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName()+ ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);throw e;}}

Consumer

InvokerInvocationHandler 调用 invoker.invoke(invocation).recreate();


public class RpcResult implements Result, Serializable {public Object recreate() throws Throwable {if (exception != null) {throw exception;}return result;}
}
由上图调用链可以看出只有集群调用失败,才是最终失败,才会把provider传来的异常抛出。(如果集群配置为失败重试,那么只有最后一次失败才会走到这里来抛出异常)

dubbo异常源码分析处理相关推荐

  1. illegalstateexception是什么异常_异常源码分析—告诉你学习“源码”究竟有什么用!...

    点击上方"服务端思维",选择"设为星标" 回复"669"获取独家整理的精选资料集 回复"加群"加入全国服务端高端社群「后 ...

  2. Dubbo 源码分析 - SPI 机制

    1.简介 SPI 全称为 Service Provider Interface,是一种服务发现机制.SPI 的本质是将接口实现类的全限定名配置在文件中,并由服务加载器读取配置文件,加载实现类.这样可以 ...

  3. Dubbo源码分析系列-深入Dubbo SPI机制

    导语   在之前的博客中介绍过关于Java中SPI的机制,也简单的分析了关于Java中SPI怎么去使用.SPI的全称Service Provider Interface,是一种服务发现机制.SPI的本 ...

  4. 源码分析Dubbo系列文章

       本系列文章主要针对Dubbo2.6.2(dubbox2.8.4)版本,从源码的角度分析Dubbo内部的实现细节,加深对Dubbo的各配置参数底层实现原理的理解,更好的指导Dubbo实践,其目录如 ...

  5. dubbo注册服务IP解析异常及IP解析源码分析

    https://www.cnblogs.com/leo-li-3046/p/5702479.html 在使用dubbo注册服务时会遇到IP解析错误导致无法正常访问. 比如: 本机设置的IP为172.1 ...

  6. Dubbo 源码分析 - 集群容错之 Cluster

    1.简介 为了避免单点故障,现在的应用至少会部署在两台服务器上.对于一些负载比较高的服务,会部署更多台服务器.这样,同一环境下的服务提供者数量会大于1.对于服务消费者来说,同一环境下出现了多个服务提供 ...

  7. Dubbo 源码分析 - 集群容错之Directory

    1. 简介 前面文章分析了服务的导出与引用过程,从本篇文章开始,我将开始分析 Dubbo 集群容错方面的源码.这部分源码包含四个部分,分别是服务目录 Directory.服务路由 Router.集群 ...

  8. Dubbo 源码分析 - 服务导出

    1.服务导出过程 本篇文章,我们来研究一下 Dubbo 导出服务的过程.Dubbo 服务导出过程始于 Spring 容器发布刷新事件,Dubbo 在接收到事件后,会立即执行服务导出逻辑.整个逻辑大致可 ...

  9. Dubbo 源码分析 - 自适应拓展原理

    1.原理 我在上一篇文章中分析了 Dubbo 的 SPI 机制,Dubbo SPI 是 Dubbo 框架的核心.Dubbo 中的很多拓展都是通过 SPI 机制进行加载的,比如 Protocol.Clu ...

  10. dubbo源码分析系列——dubbo-cluster模块源码分析

    2019独角兽企业重金招聘Python工程师标准>>> 模块功能介绍 该模块的使用介绍请参考dubbo官方用户手册如下章节内容. 集群容错 负载均衡 路由规则 配置规则 注册中心参考 ...

最新文章

  1. matlab车牌识别课程设计,matlab车牌识别课程设计报告模板(附源代码).doc
  2. 网页测试本地服务器_音视频开发搭建一个直播服务器
  3. from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver,
  4. Cortex-M3中断的具体行为
  5. 熟悉MyEclipse
  6. 注意numpy与pandas里std中的有偏和无偏
  7. 只需 45 秒,Python 给故宫画一组手绘图!
  8. ubuntu之录屏软件kazam的安装
  9. 解决Please define the NDK_PROJECT_PATH variable to point to it.
  10. 还在用百度找资源?试试这3个顶级资源搜索网站,没有找不到的!
  11. 嵌入式软件工程师岗位要求
  12. Windows 10 build Error !include: could not find: ****StdUtils.nsh
  13. matlab解坐标方程,matlab程序(解泊松方程)
  14. azkaban上传zip报错:Error Chunking during uploading files to db
  15. 渗透测试的灵魂:信息收集
  16. unable to connect to 192.168.1.110:5555 解决办法
  17. Azureus源码剖析(二) ---解析Torrent种子文件
  18. pytorch,torch,torchvision的gpu版本安装避坑
  19. vue -- 初级(二)
  20. MathJax 支持的 Latex 符号总结(微积分常用符号)

热门文章

  1. 聚划算创意新零售,这个“加油站”不加油只加奶?
  2. C/S架构的优点和缺点
  3. linux下10款markdown软件
  4. 【毕业设计】基于大数据的京东消费行为分析与可视化 - python 机器学习
  5. 世界五大黑客:代码创造他们,他们改变世界!...
  6. 数据挖掘:概念与技术笔记(一)引言
  7. arcgis制作超链接
  8. C++判断输入内容是否为整数
  9. 我国首个5G地铁站开通:TCL集团重组方案通过;苹果2019新品图泄露|雷锋早报...
  10. 敏捷开发中团队如何面对失败的Sprint