dubbo rpc调用抛出的Exception处理
关于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处理的更多相关文章
- 记录一次dubbo不能正常抛出特定异常
BUG场景 今天同事的代码中出现一个问题,让我帮忙排查一下.原代码大致如下 dubbo服务消费者: @Resource private IPayWayService payWayService; @R ...
- 【M12】了解“抛出一个exception”与“传递一个参数”或“调用一个虚函数”之间的差异
1.方法参数的声明语法和catch语句的语法是一样的,你可能会认为主调方法调用一个方法,并向其传递参数,与抛出一个异常传递到catch语句是一样的,是的,有相同之处,但也有更大的不同. 2.主调方法调 ...
- Dubbo RPC调用参数校验---错误message自动返回
Dubbo 的RPC调用中Consumer 和 Provider端都可以对调用的方法做传参验证,参数的验证可以通过JSR303规范 (Java Specification Requests) 提到的 ...
- dubbo rpc调用,接收到的bean为null原因?
前几天对接公司内部其他部门的系统,用dubbo调用,dubbo看起来很简单,但是却让我们调试了好久啊! 下面是调试纪录: 1. 调用该服务时,直接调不通,查看错误为 no provider ? 然后就 ...
- VS2013 抛出 stackoverflow exception 的追踪
本公司使用VWG.Caslte ActiveRecord.CSLA.net .Quantz.net 等组件做为公司的开发基础,自2007年以来,一直工作正常,但最近(2015.12月)以来,打开MDA ...
- 从constructor中抛出exception后,constructor会返回null吗?
刚才琢磨这个问题主要是在想,如果constructor抛出了exception,那么返回的object是什么一个情况呢?如果我这个object中有一些关键的资源没有初始化,比如说Database co ...
- Spring异常抛出触发事务回滚
Spring.EJB的声明式事务默认情况下都是在抛出unchecked exception后才会触发事务的回滚 /** * 如果在spring事务配置中不为切入点(如这里的切入点可以定义成test*) ...
- JAVA异常的捕获与抛出原则
在可能会出现exception的地方,要使用try-catch或者throws或者两者都要.我的判断依据是:如果对可能出现的exception不想被外部(方法的调用者)知道,就在方法内部try-cat ...
- 【开发技术】java异常的捕获与抛出原则
在可能会出现exception的地方,要使用try-catch或者throws或者两者都要.我的判断依据是:如果对可能出现的exception不想被外部(方法的调用者)知道,就在方法内部try-cat ...
随机推荐
- [LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- 10 Free Image Hosting Sites for Your Photos
https://www.lifewire.com/free-image-hosting-sites-3486329 Wondering if there are there any good site ...
- 用于模型选择的AIC与BIC
一.AIC(Akaike information Criterion)准则 二.BIC(Bayesian information Criterion)准则 参考文献: [1]AIC与BIC区别
- 9.C# 类
1.类定义格式 [访问修饰符]class 类名 [:父类名,接口名,接口名] { //类成员 } 类的访问修饰符有public.internal两个,没有标明访问修饰符默认为internal,被int ...
- jenkins 添加 证书凭证Credentials
jenkins 添加 证书凭证Credentials 大家都知道jenkins在拉取git项目代码的时候,如果没有配置 “证书凭证Credentials” 或者配置的不对, 就会出现红色报错,最终导致 ...
- 关于bjam编译自己模块出错的问题
将这个目录下C:\Users\Regubed的user-config.jam改为下面内容 # MSVC configurationusing msvc : 9.0 ; # Python configu ...
- 1.Spring对JDBC整合支持
1.Spring对JDBC整合支持 Spring对DAO提供哪些支持 1)Spring对DAO异常提供统一处理 2)Spring对DAO编写提供支持的抽象类 3)提高编程效率,减少DAO编码量 Spr ...
- MYSQL 5.7修改密码,登录问题
mysql5.7 关于密码问题 报错: ERROR 1862 (HY000): Your password has expired. To log in you must change it usin ...
- jdbc连接oracle的几种格式
1. SID的方式.已经不推荐使用这种方式了. jdbc:oracle:thin:[<user>/<password>]@<host>[:<port>] ...
- uvm设计分析——tlm
tlm模块,用来在不同模块之间实现实时通信,主要基于两个定义在通信双方的port类来实现. 两个port之间,通过connect函数,来拿到双方的class指针,进而调用对方的function. 但是 ...