Java Dubbo泛化调用之自定义异常处理

Dubbo 肯定是家常便饭使用了,最近因为项目需要,对一个特定的异常,进行相关处理,并且这个异常不是checked异常,所以就没有在方法上加上异常签名,想通过消费方来直接进行异常捕获。

TODO 接下来是遇到的坑

  • 泛化调用和非泛化调用的区别
  • Dubbo调用Filter责任链
  • 异常包装

泛化调用和非泛化调用在provider上的区别提现,GenericFilter 实现?

 

问题:Java Dubbo 泛化调用不会返回自定义异常

解决:

自定义两个Filter:

DubboExceptionFilter:替换原生dubbo的Exception,抛出自定义异常,用于常规调用

@Activate(group={ Constants.PROVIDER})
public class DubboExceptionFilter implements Filter {
    private static final Logger LOGGER = LoggerFactory.getLogger(DubboExceptionFilter.class);

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

            if (result.hasException() && GenericService.class != invoker.getInterface()) {
                try {
                    Throwable exception = result.getException();

                    // directly throw if it's checked exception
                    if (!(exception instanceof RuntimeException) && (exception instanceof Exception)) {
                        return result;
                    }
                    // 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 result;
                            }
                        }
                    } catch (NoSuchMethodException e) {
                        return result;
                    }
                    // for the exception not found i
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值