关于dubbo的Exception堆栈被吃处理,网上已经有比较多的解决方法,在我们的应用场景中,不希望RPC调用对方抛出业务exception,而是通过Resp中的errorCode,errorMsg来处理,例如有如下的定义:

    @Override
public ResultModel<String> createExpress(CreateExpressDTO dto) {
// 参数验证
String group = "";
if (StringUtils.isNotBlank(dto.getPartyId())) {
group = Group.PARTY_TYPE_PARTY;
} else if (DictCons.BANK_ACCOUNT_TYPE__PUBLIC.equals(dto.getBankAccountType())) {
group = Group.PARTY_TYPE_NOT_PARTY_PUB;
} else {
group = Group.PARTY_TYPE_NOT_PARTY_PRI;
}
ValidationResult validationResult = ValidationUtils.validateEntity(group, dto);
if (!validationResult.isSuccess()) {
return new ResultModel<>(ErrorCons.ERR_BS_BAD_PARAM,
FastJsonUtil.serializeFromObject(validationResult.getErrorPair()));
}
此处省去N行。。。。。

假设createExpress执行异常的时候,我们希望错误通过ResultModel<String>而不是RuntimeException回去,dubbo的exceptionfilter不符合我们的要求,如果要改动的话,需要到序列化层进行修改(原来在自己开发的RPC框架上就是这么处理的)。这种情况,只能借助AOP,如下:

package tf56.lf.common.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean; import tf56.lf.base.ResultModel;
import tf56.lf.base.metadata.err.ErrorCons;
import tf56.lf.common.exception.LfException; import com.alibaba.fastjson.JSON; @Aspect
public class ServiceAroundAdvice implements InitializingBean { private static final Logger logger = LoggerFactory.getLogger(ServiceAroundAdvice.class); @Around("execution(* tf56.lf.*.service..*(..))")
public Object processDubboService(ProceedingJoinPoint jp) throws Throwable{
long begin = System.currentTimeMillis();
logger.info("开始执行" + jp.getSignature() + "参数:" + JSON.toJSONString(jp.getArgs()));
Object result = null;
try {
result = jp.proceed();
} catch (Exception e) {
logger.error("",e);
Class<?> clz = ((MethodSignature)jp.getSignature()).getMethod().getReturnType();
if ("void".equals(clz.getName())) {
return result;
} ResultModel<?> resultModel = ((ResultModel<?>)clz.newInstance());
if (e instanceof LfException) {
resultModel.setCode(((LfException) e).getCode());
resultModel.setMsg(((LfException) e).getErrorInfo());
} else {
resultModel.setCode(ErrorCons.ERR_FAIL);
}
result = resultModel;
}
long end = System.currentTimeMillis();
logger.info("完成执行" + jp.getSignature() + ",共" + (end - begin) + "毫秒!");
return result;
} @Override
public void afterPropertiesSet() throws Exception {
logger.info("加载" + this.getClass().getCanonicalName() + "成功!");
}
}

增加配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <bean class="tf56.lf.common.advice.ServiceAroundAdvice"></bean>
<aop:aspectj-autoproxy proxy-target-class="true" />

这样就可以保证所有的Exception都会被优雅的处理,同时泛型也都能恰当的被处理。

dubbo rpc调用抛出的Exception处理的更多相关文章

  1. 记录一次dubbo不能正常抛出特定异常

    BUG场景 今天同事的代码中出现一个问题,让我帮忙排查一下.原代码大致如下 dubbo服务消费者: @Resource private IPayWayService payWayService; @R ...

  2. 【M12】了解“抛出一个exception”与“传递一个参数”或“调用一个虚函数”之间的差异

    1.方法参数的声明语法和catch语句的语法是一样的,你可能会认为主调方法调用一个方法,并向其传递参数,与抛出一个异常传递到catch语句是一样的,是的,有相同之处,但也有更大的不同. 2.主调方法调 ...

  3. Dubbo RPC调用参数校验---错误message自动返回

    Dubbo 的RPC调用中Consumer 和 Provider端都可以对调用的方法做传参验证,参数的验证可以通过JSR303规范 (Java Specification Requests) 提到的 ...

  4. dubbo rpc调用,接收到的bean为null原因?

    前几天对接公司内部其他部门的系统,用dubbo调用,dubbo看起来很简单,但是却让我们调试了好久啊! 下面是调试纪录: 1. 调用该服务时,直接调不通,查看错误为 no provider ? 然后就 ...

  5. VS2013 抛出 stackoverflow exception 的追踪

    本公司使用VWG.Caslte ActiveRecord.CSLA.net .Quantz.net 等组件做为公司的开发基础,自2007年以来,一直工作正常,但最近(2015.12月)以来,打开MDA ...

  6. 从constructor中抛出exception后,constructor会返回null吗?

    刚才琢磨这个问题主要是在想,如果constructor抛出了exception,那么返回的object是什么一个情况呢?如果我这个object中有一些关键的资源没有初始化,比如说Database co ...

  7. Spring异常抛出触发事务回滚

    Spring.EJB的声明式事务默认情况下都是在抛出unchecked exception后才会触发事务的回滚 /** * 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*) ...

  8. JAVA异常的捕获与抛出原则

    在可能会出现exception的地方,要使用try-catch或者throws或者两者都要.我的判断依据是:如果对可能出现的exception不想被外部(方法的调用者)知道,就在方法内部try-cat ...

  9. 【开发技术】java异常的捕获与抛出原则

    在可能会出现exception的地方,要使用try-catch或者throws或者两者都要.我的判断依据是:如果对可能出现的exception不想被外部(方法的调用者)知道,就在方法内部try-cat ...

随机推荐

  1. heapy() :python自带的堆排序

    堆是一个二叉树,其中每个父节点的值都小于或等于其所有子节点的值.整个堆的最小元素总是位于二叉树的根节点.python的heapq模块提供了对堆的支持. 堆数据结构最重要的特征是heap[0]永远是最小 ...

  2. React对比Vue(05 生命周期的对比)

    先来vue的吧,先上图,生命周期就好比一个人重出生到青少年再到青年再到中年在到老年到死亡的一个过程,不同的过程做不同的事情. 好了,上代码 beforeCreate :数据还没有挂载呢,只是一个空壳 ...

  3. 用URLRewriter重写url

    用url重新一般都是使用URLRewriter库,基本上都是一些配置,在webconfig中 首先配置configuration节点 <configSections> <sectio ...

  4. mybatis之注解式开发

    注解: 注解是用于描述代码的代码.例如:@Test(用于描述方法进行junit测试),@Override(用于描述方法的重写),@Param(用于描述属性的名称) 注解的使用风格:@xxx(属性),使 ...

  5. 发布网站配置文件和SSL

    1.将cert下新建一个文件将所有证书文件放在新建的文件下 例如:cert/medcard 2.配置网站的.conf文件 <VirtualHost *:443> ServerName ww ...

  6. 【Spring学习笔记-MVC】Spring MVC之多文件上传 (zhan)

    http://www.cnblogs.com/ssslinppp/p/4607330.html (zhan)

  7. oracle 修改表结构,增加列,删除列等

    增加一列:ALTER TABLE yourTabbleName ADD columnName dataType; 增加多列:ALTER TABLE yourTabbleName ADD (column ...

  8. Python全栈-day14-模块和包

    一.模块 1.模块 1)定义 一系列功能的集合体,在Python中py文件就是一个模块 2)模块的类别 a.使用Python编写的py文件 b.已经被编译成共享库或者DLL的C 或者 C++ 扩展 c ...

  9. Qt & VS2013 报错:There's no Qt version assigned to this project for platform Win32

    如果你想了解关于Qt与VS2013开发环境搭建,可以至此翻页. 这里主要分享环境已搭建成功,在构建项目时遇到的报错解决方案. [1]Qt 与 VS2013开发环境构建时报错 报错界面如下: 注意:对话 ...

  10. tomcat 、NIO、netty 本质

    tomcat 基于 Socket,面向 web 浏览器的通信容器 nio 同步非阻塞的I/O模型 netty 通信框架,对 nio 的封装