dubbo异常拦截方案及自定义

本文档展示了如何在 Apache Dubbo 中自定义异常过滤器,通过 `@Activate` 注解激活过滤器,并在 `ExceptionFilter` 类中处理未声明的异常。新定义的 `DubboExceptionFilter` 添加了对特定异常类型的检查,仅在非预期异常出现时记录错误。此外,还解释了如何配置以使用自定义过滤器并排除默认异常处理。
摘要由CSDN通过智能技术生成
package org.apache.dubbo.rpc.filter;

import java.lang.reflect.Method;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.ListenableFilter;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.Filter.Listener;
import org.apache.dubbo.rpc.service.GenericService;

@Activate(
    group = {"provider"}
)
public class ExceptionFilter extends ListenableFilter {
    public ExceptionFilter() {
        super.listener = new ExceptionFilter.ExceptionListener();
    }

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

    static class ExceptionListener implements Listener {
        private Logger logger = LoggerFactory.getLogger(ExceptionFilter.ExceptionListener.class);

        ExceptionListener() {
        }

        public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
            if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {
                try {
                    Throwable exception = appResponse.getException();
                    if (exception instanceof RuntimeException || !(exception instanceof Exception)) {
                        try {
                            Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());
                            Class<?>[] exceptionClassses = method.getExceptionTypes();
                            Class[] var7 = exceptionClassses;
                            int var8 = exceptionClassses.length;

                            for(int var9 = 0; var9 < var8; ++var9) {
                                Class<?> exceptionClass = var7[var9];
                                if (exception.getClass().equals(exceptionClass)) {
                                    return;
                                }
                            }
                        } catch (NoSuchMethodException var11) {
                            return;
                        }

                        this.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);
                        String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface());
                        String exceptionFile = ReflectUtils.getCodeBase(exception.getClass());
                        if (serviceFile != null && exceptionFile != null && !serviceFile.equals(exceptionFile)) {
                            String className = exception.getClass().getName();
                            if (!className.startsWith("java.") && !className.startsWith("javax.")) {
                                if (!(exception instanceof RpcException)) {
                                    appResponse.setException(new RuntimeException(StringUtils.toString(exception)));
                                }
                            }
                        }
                    }
                } catch (Throwable var12) {
                    this.logger.warn("Fail to ExceptionFilter when called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + var12.getClass().getName() + ": " + var12.getMessage(), var12);
                }
            }
        }

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

        public void setLogger(Logger logger) {
            this.logger = logger;
        }
    }
}

可以自定义

@Activate(
    group = {"provider"}
)
public class DubboExceptionFilter extends ListenableFilter {
    public DubboExceptionFilter() {
        super.listener = new DubboExceptionFilter.ExceptionListener();
    }

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

    static class ExceptionListener implements Listener {
        private Logger logger = LoggerFactory.getLogger(DubboExceptionFilter.ExceptionListener.class);

        ExceptionListener() {
        }

        public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
            if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {
                try {
                    Throwable exception = appResponse.getException();
                    if (exception instanceof RuntimeException || !(exception instanceof Exception)) {
                        try {
                            Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());
                            Class<?>[] exceptionClassses = method.getExceptionTypes();
                            Class[] var7 = exceptionClassses;
                            int var8 = exceptionClassses.length;

                            for(int var9 = 0; var9 < var8; ++var9) {
                                Class<?> exceptionClass = var7[var9];
                                if (exception.getClass().equals(exceptionClass)) {
                                    return;
                                }
                            }
                        } catch (NoSuchMethodException var11) {
                            return;
                        }

                        if (!(exception instanceof MyException)) {
                            this.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);
                        }

                        String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface());
                        String exceptionFile = ReflectUtils.getCodeBase(exception.getClass());
                        if (serviceFile != null && exceptionFile != null && !serviceFile.equals(exceptionFile)) {
                            String className = exception.getClass().getName();
                            if (!className.startsWith("java.") && !className.startsWith("javax.")) {
                                if (!className.startsWith("com.guzy")) {
										//发现是我自己定义的异常,即忽略
                                    if (!(exception instanceof RpcException)) {
                                        appResponse.setException(new RuntimeException(StringUtils.toString(exception)));
                                    }
                                }
                            }
                        }
                    }
                } catch (Throwable var12) {
                    this.logger.warn("Fail to ExceptionFilter when called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + var12.getClass().getName() + ": " + var12.getMessage(), var12);
                }
            }
        }

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

        public void setLogger(Logger logger) {
            this.logger = logger;
        }
    }
}

当然,如果只是自定义了也不够,还需要加上
文件org.apache.dubbo.rpc.Filter
dubboExceptionFilter=com.my.DubboExceptionFilter

这还不够,这时候,两个异常拦截都会走
需要改动配置

dubbo:
  provider:
    filter: dubboExceptionFilter,-exception

加上这段 -exception作用就是异常原生的异常处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值