在前一篇文章中,讲到了dubbo自定义异常如何进行传递,在consumer中如何获取到provider的业务异常信息,那篇文章的主旨只是归纳总结,这篇文章来看一下,具体如何自定义filter来实现dubbo的异常透传。
一、实现dubbo提供的Filter接口
package com.zhiri.biz.center.application.filter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.*;
import org.apache.dubbo.rpc.service.GenericService;
import java.lang.reflect.Method;
/**
* 自定义dubbo异常处理,不让dubbo包一层RuntimeException
*
* @author SongFei
* @date 2020/5/7 17:03
*/
@Activate(group = CommonConstants.PROVIDER)
@Slf4j
public class DubboExceptionFilter implements Filter, Filter.Listener {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
return invoker.invoke(invocation);
}
@Override
public void onMessage(Result appResponse, Invoker<?> invoker, Invocation invocation) {
if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {
try {
Throwable exception = appResponse.getException();
// directly throw if it's checked exception
if (!(exception instanceof RuntimeException) && (exception instanceof Exception)) {
return;
}
// directly throw if the exception appears in the signature
try {
Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());
Class<?>[] exceptionClassses = method.getExceptionTypes();
for (Class<?> exceptionClass : exceptionClassses) {
if (exception.getClass().equals(exceptionClass)) {
return;
}
}
} catch (NoSuchMethodException e) {
return;
}
// for the exception not found in method's signature, print ERROR message in server's log.
log.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;
}
// directly throw if it's JDK exception
String className = exception.getClass().getName();
if (className.startsWith("java.") || className.startsWith("javax.")) {
return;
}
// customer exception
if (className.startsWith("com.zhiri.common.base")) {
return;
}
// directly throw if it's dubbo exception
if (exception instanceof RpcException) {
return;
}
// otherwise, wrap with RuntimeException and throw back to the client
appResponse.setException(new RuntimeException(StringUtils.toString(exception)));
} catch (Throwable e) {
log.warn("Fail to ExceptionFilter when called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
}
}
}
@Override
public void onError(Throwable throwable, Invoker<?> invoker, Invocation invocation) {
log.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + throwable.getClass().getName() + ": " + throwable.getMessage(), throwable);
}
}
二、在resources目录下新建Filter文件
文件内容:
dubboExceptionFilter=com.zhiri.biz.center.application.filter.DubboExceptionFilter
文件目录层级:/META-INF/dubbo,文件全名就叫“org.apache.dubbo.rpc.Filter”,也可以叫“com.alibaba.dubbo.rpc.Filter”。
这一步是为了第三步做准备,因为这个filter不是spring的bean,不能直接在配置中引入,所以需要额外声明。
三、在dubbo配置中引入自定义的filter
dubbo.provider.filter=-exception,dubboExceptionFilter
逗号分隔,两个参数,前面那个是将dubbo默认的ExceptionFilter去除,后面那个是第二步中配置的Filter。
看完文章,有任何疑问,请加入群聊一起交流!!!
扫码关注本站微信公众号,第一时间获取网站更新动态!
本文地址:https://www.jiweichengzhu.com/article/53d62a6e4f1e4e1c9fb64b774f0e6f00
**关于转载:**若站内作品和文章得到了你的青睐,欢迎各位朋友分享到个人站长或者朋友圈,但转载请注明文章出处和原文链接!
很赞哦! (5)