最近在整Dubbo的框架,使用zookeeper做注册中心,项目中有service层作为provider(提供者),web层作为consumer(消费者),当在做自定义异常的抛出时,遇到了问题:
1、web层不能识别异常为自定义异常类型,异常类型为RuntimeException。
2、当修改识别类型为RuntimeException时,从异常当中获取到的message,多出了一大堆异常堆栈的信息。
就这两个问题,在网上搜索了很多资料,大部分都把Dubbo中的ExceptionFilter代码贴出来,然后分析逻辑,这里也将代码贴一下(以下是改过之后的代码,因为最终我选用了改源码的形式解决问题。)。
1/* 2 * Copyright 1999-2011 Alibaba Group. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package com.alibaba.dubbo.rpc.filter; 17 18import java.lang.reflect.Method; 19 20import com.alibaba.dubbo.common.Constants; 21import com.alibaba.dubbo.common.extension.Activate; 22import com.alibaba.dubbo.common.logger.Logger; 23import com.alibaba.dubbo.common.logger.LoggerFactory; 24import com.alibaba.dubbo.common.utils.ReflectUtils; 25import com.alibaba.dubbo.common.utils.StringUtils; 26import com.alibaba.dubbo.rpc.Filter; 27import com.alibaba.dubbo.rpc.Invocation; 28import com.alibaba.dubbo.rpc.Invoker; 29import com.alibaba.dubbo.rpc.Result; 30import com.alibaba.dubbo.rpc.RpcContext; 31import com.alibaba.dubbo.rpc.RpcException; 32import com.alibaba.dubbo.rpc.RpcResult; 33import com.alibaba.dubbo.rpc.service.GenericService; 34 35/** 36 * ExceptionInvokerFilter 37 * <p> 38 * 功能: 39 * <ol> 40 * <li>不期望的异常打ERROR日志(Provider端)<br> 41 * 不期望的日志即是,没有的接口上声明的Unchecked异常。 42 * <li>异常不在API包中,则Wrap一层RuntimeException。<br> 43 * RPC对于第一层异常会直接序列化传输(Cause异常会String化),避免异常在Client出不能反序列化问题。 44 * </ol> 45 * 46 * @author william.liangf 47 * @author ding.lid 48 */ 49@Activate(group = Constants.PROVIDER) 50public class ExceptionFilter implements Filter { 51 52 private final Logger logger; 53 54 public ExceptionFilter() { 55 this(LoggerFactory.getLogger(ExceptionFilter.class)); 56 } 57 58 public ExceptionFilter(Logger logger) { 59 this.logger = logger; 60 } 61 62 public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { 63 try { 64 Result result = invoker.invoke(invocation); 65 if (result.hasException() && GenericService.class != invoker.getInterface()) { 66 try { 67 Throwable exception = result.getException(); 68 69 // 如果是checked异常,直接抛出 70 if (!(exception instanceof RuntimeException) && (exception instanceof Exception)) { 71 return result; 72 } 73 // 在方法签名上有声明,直接抛出 74 try { 75 Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes()); 76 Class<?>[] exceptionClassses = method.getExceptionTypes(); 77 for (Class<?> exceptionClass : exceptionClassses) { 78 if (exception.getClass().equals(exceptionClass)) { 79 return result; 80 } 81 } 82 } catch (NoSuchMethodException e) { 83 return result; 84 } 85 86 // 未在方法签名上定义的异常,在服务器端打印ERROR日志 87 logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost() 88 + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() 89 + ", exception: " + exception.getClass().getName() + ": " + exception.getMessage(), exception); 90 91 // 异常类和接口类在同一jar包里,直接抛出 92 String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface()); 93 String exceptionFile = ReflectUtils.getCodeBase(exception.getClass()); 94 if (serviceFile == null || exceptionFile == null || serviceFile.equals(exceptionFile)) { 95 return result; 96 } 97 // 是JDK自带的异常,直接抛出 98 String className = exception.getClass().getName(); 99 if (className.startsWith("java.") || className.startsWith("javax.")) { 100 return result; 101 } 102 if (className.startsWith("******") || className.startsWith("******")) { 103 logger.debug("===========自定义异常,直接抛出==========="); 104 return result; 105 } 106 107 // 是Dubbo本身的异常,直接抛出 108 if (exception instanceof RpcException) { 109 return result; 110 } 111 112 // 否则,包装成RuntimeException抛给客户端 113 return new RpcResult(new RuntimeException(StringUtils.toString(exception))); 114 } catch (Throwable e) { 115 logger.warn("Fail to ExceptionFilter when called by " + RpcContext.getContext().getRemoteHost() 116 + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() 117 + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e); 118 return result; 119 } 120 } 121 return result; 122 } catch (RuntimeException e) { 123 logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost() 124 + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() 125 + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e); 126 throw e; 127 } 128 } 129 130}
其中的
1if (className.startsWith("******") || className.startsWith("******")) { 2 logger.debug("===========自定义异常,直接抛出==========="); 3 return result; 4}
这一部分是我自己新添的代码,其中的**********是我们项目内部的自定义异常的包名,为项目保密在这里以******代替。
关于这里面的逻辑,在这里我就不过多赘述,注释都写的很清楚了。
而出现我所说的1和2两个情况,是在这段代码中,并没有检测自定义异常,甚至RuntimeException。
而是直接将不符合条件的异常全部进行了RuntimeException的包装。
1 // 否则,包装成RuntimeException抛给客户端 2 return new RpcResult(new RuntimeException(StringUtils.toString(exception)));
注意这里的StringUtils.toString。就是这个操作,将本来就几个字的message,直接给整成了一大堆带异常堆栈的信息。
这里要说明一下,为什么要贴出来ExceptionFilter的代码,因为provider抛出异常后,经过dubbo,然后在consumer捕获异常时,进行了异常的反序列化,ExceptionFilter负责的就是将异常还原包装。
所以,看到这里,问题就很明确了:
1、ExceptionFilter并不认识我自定义的异常。
2、ExceptionFilter没有检测RuntimeException异常(我的自定义异常继承了RuntimeException)。
依照ExceptionFilter的逻辑,解决办法有以下几种:
1、 将该异常的包名以"java.或者"javax. " 开头,但自己的项目都有自己特定的包名,所以弃用。
2、使用受检异常(继承Exception),继承根Exception,鬼知道会出现其他什么BUG,pass。
3、 把自定义异常放到provider的api模块中,每个api都定义自定义异常的话,平白增加了很多工作量,pass。
4、 provider实现GenericService接口,查了很多资料,也看到有人使用了这种方法,要弃用$invoke方法,最终也没有成功,懒惰的我也就没去试,有兴趣的朋友可以试试,然后跟我说说是个什么情况(我得懒到什么程度)。
5、provider的api明确写明抛出的异常,我连自定义异常放到api里面都懒得做,你让我每个接口都写抛出异常?开个玩笑。弃用这种方法的原因有三:1)在做这个异常处理的时候,已经完成了一部分代码了,要加抛出异常,是个不小的工作量。2)抛出的自定义异常只为了中止代码执行,返回给前台做消息提示,所以不涉及到业务的处理。3)添加抛出异常,增加了开发的工作量,也会影响到开发时专注于业务处理的初衷。
6、让ExceptionFilter支持自定义异常,修改源码使之直接抛出自定义异常。这种解决办法无疑是工作量最小,影响最小的了。但这其中也有一个隐患,如果要进行dubbo的版本更新的话,就必须将新的版本的ExceptionFilter也去修改。在此希望Dubbo能够支持配置自定义的ExceptionFilter。