apache dubbo 实现自定义异常拦截器

apache dubbo 实现自定义异常拦截器

    • 1.yum文件配置
    • 2.配置exceptionFilter
    • 3.创建AyaDubboExceptionFilter类
    • 总结
    • 上一篇:[dubbo 2.6.X 自定义异常处理](https://blog.csdn.net/qq_22611671/article/details/90669083)

有朋友反馈apache dubbo 按照上一篇博客配置不生效,本文结合上一篇文章讲解一下apache dubbo(version = 2.7.5) 配置 与 alibaba dubbo 配置的区别以及解决思路

1.yum文件配置

apache dubbo 实现自定义异常拦截器_第1张图片

exceptionFilter 为我们配置的异常拦截器,-exception为取消默认拦截器【必须设置否则不生效】

2.配置exceptionFilter

apache dubbo 实现自定义异常拦截器_第2张图片
与之前对比,resoures 下文件夹变更及文件名变更

3.创建AyaDubboExceptionFilter类

@Activate(group = CommonConstants.PROVIDER)
public class AyaDubboExceptionFilter implements Filter, Filter.Listener {
    private Logger logger = LoggerFactory.getLogger(ExceptionFilter.class);

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        return invoker.invoke(invocation);
    }

    @Override
    public Result onResponse(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 appResponse;
                }
                // 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 appResponse;
                        }
                    }
                } catch (NoSuchMethodException e) {
                    return appResponse;
                }

                // for the exception not found in method's signature, print ERROR message in server's log.
                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 appResponse;
                }
                // directly throw if it's JDK exception
                String className = exception.getClass().getName();
                if (className.startsWith("java.") || className.startsWith("javax.")) {
                    return appResponse;
                }
                // directly throw if it's dubbo exception
                if (exception instanceof RpcException) {
                    return appResponse;
                }

                if (exception instanceof YtException) {
                    logger.debug("ytException");
                    YtException rrException = (YtException) exception;
                    appResponse.setException(rrException);
                    return appResponse;
                }
                // otherwise, wrap with RuntimeException and throw back to the client
                appResponse.setException(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 appResponse;
    }

    @Override
    public void onMessage(Result result, Invoker<?> invoker, Invocation invocation) {

    }

    @Override
    public void onError(Throwable e, Invoker<?> invoker, Invocation invocation) {
        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);
    }

    // For test purpose
    public void setLogger(Logger logger) {
        this.logger = logger;
    }


}

其中自己需要变更部分,此处写自己需要设定的自定义异常

 if (exception instanceof YtException) {
                    logger.debug("ytException");
                    YtException rrException = (YtException) exception;
                    appResponse.setException(rrException);
                    return appResponse;
                }

总结

解决思路,面对dubbo不同的版本号,也许处理方式会不一致
1.首先需对dubbo内部异常处理需要有一定的了解, 下载dubbo源码
2.
apache dubbo 实现自定义异常拦截器_第3张图片apache dubbo 实现自定义异常拦截器_第4张图片

通过阅读源码,我们会发现消费者无法接收自定义原因如下:

appResponse.setException(new RuntimeException(StringUtils.toString(exception)));

dubbo将其他异常信息,统一转化为运行时异常,所以我们的处理思路就是在此前判断我们系统自定义异常,直接返回,处理如第三步

上一篇:dubbo 2.6.X 自定义异常处理

你可能感兴趣的:(dubbo)