根据面试经验而言,能在简历上写上原理、源码等关键词的,是非常具备核心竞争力的.上周和粉丝交流面试情况如下
面试的时候,把源码一波分析,令面试官虎躯一震!在一阵前戏过后,以为接下来无非就是身体的一顿抽搐一切变得索然无味,不料面试官来了句令剧情发生了反转
"你对Dubbo源码这么熟悉,那请问你使用的时候,有没有遇到什么坑"
我擦,毫无准备的他,菊花顿时一紧!此时就面临唬住了50K,唬不住就只能5K的局面,慌了!
论如何反杀
相信大家面试都遇到过类似问题,因为源码解析网上很多,很多人"考前突击"一下,但是遇到喜欢问细节的面试官,终究难逃法眼,无处遁形.遇到这个问题,我们如何反杀一波?那么我就从一次聊天记录说起,拥有真实场景的源码实战(非常重要),遇到这类问题,才不至于出现猛虎落泪的情形
真实场景描述
那么我们把业务相关去掉,抽取一个最简模型.我们在公司,一般都会有自己的自定义异常,然后这个自定义异常一般放在common.jar给其他模块依赖,比如我这里定义一个HelloException
1public class HelloException extends RuntimeException {23 public HelloException() {4 }56 public HelloException(String message) {7 super(message);8 }910}
然后我们写一个最简单的Dubbo的demo,如下
interface
1public interface DemoService {23 String sayHello(Stringname);45}
provider
1public class DemoServiceImpl implements DemoService {23 public String sayHello(Stringname) {4 thrownewHelloException("公众号:肥朝");5 }67}
consumer
1public class DemoAction {23 private DemoService demoService;45 public void setDemoService(DemoService demoService) {6 this.demoService = demoService;7 }89 public void start() throws Exception {10 try {11 String hello = demoService.sayHello("公众号:肥朝");12 } catch (HelloException helloException) {13 System.out.println("这里捕获helloException异常");14 }15 }1617}
按照聊天记录的描述,此时consumer调用provider,provider抛出HelloException.但是consumer捕获到的,却不是HelloException.
那么我们运行看看
果然如该同事所言.为什么会这样呢?之前没看过肥朝Dubbo源码解析系列的同学这种时候往往采用最低效的解决办法,把异常栈往微信群一丢,各种求助.但是往往毫无收获,然后感叹社会为何如此冷漠!
但是相信公众号的老粉丝们早已掌握阅读源码的技能,和肥朝一样坐怀不乱,九浅一深直入源码.出现异常我们首先看一下异常栈
除非撸多了看不清(建议戒撸),否则这行异常和肥朝一样,就像漆黑中的萤火虫一样,那么鲜明,那么出众
1com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:108)
那么我们一探究竟
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();
// 如果是checked异常,直接抛出
if(! (exception instanceof RuntimeException) && (exception instanceof Exception)) {
returnresult;
}
// 在方法签名上有声明,直接抛出
try {
Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());
Class[] exceptionClassses = method.getExceptionTypes();
for(Class exceptionClass : exceptionClassses) {
if(exception.getClass().equals(exceptionClass)) {
returnresult;
}
}
}
catch (NoSuchMethodException e) {
returnresult;
}
// 未在方法签名上定义的异常,在服务器端打印ERROR日志
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);
// 异常类和接口类在同一jar包里,直接抛出
String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface());
String exceptionFile = ReflectUtils.getCodeBase(exception.getClass());
if(serviceFile == null || exceptionFile == null || serviceFile.equals(exceptionFile)){
returnresult;
}
// 是JDK自带的异常,直接抛出
String className = exception.getClass().getName();
if(className.startsWith("java.") || className.startsWith("javax.")) {
returnresult;
}
// 是Dubbo本身的异常,直接抛出
if(exception instanceof RpcException) {
returnresult;
}
// 否则,包装成RuntimeException抛给客户端
returnnewRpcResult(newRuntimeException(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); returnresult;
}
}
returnresult;
}
catch (RuntimeException e) {
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);
throw e;
}
}
1.如果是checked异常,直接抛出.很明显,我们的HelloException是RuntimeException,不符合
2.在方法签名上有声明,直接抛出.很明显,我们接口并未声明该异常,不符合
3.异常类和接口类在同一jar包里,直接抛出.很明显,我们的异常类是在common.jar的,接口是在api.jar的,不符合
4.是JDK自带的异常,直接抛出.很明显,这个HelloException是我们自定义的,不符合
5.是Dubbo本身的异常(RpcException),直接抛出.很明显,这个HelloException是我们自定义的,和RpcException几乎没有半毛钱关系.
6.否则,包装成RuntimeException抛给客户端.因为以上5点均不满足,所以该异常会被包装成RuntimeException异常抛出(重要)
这也就是为什么我们catchHelloException是catch不到的,因为他包装成RuntimeException了
Dubbo为什么这么设计
也许你看到这里会觉得这个判断好坑.Dubbo为什么要这么设计?我们看源码,最重要的是知道作者为什么这么设计,只有知道为什么这么设计才是经过了深度的思考,否则看时高潮,看后就忘.讲清楚为什么这么设计,也是大家关注肥朝公众号的一个重要原因.
其实Dubbo的这个考虑,是基于序列化来考虑的.你想想,如果provider抛出一个仅在provider自定义的一个异常,那么该异常到达consumer,明显是无法序列化的.所以你注意看Dubbo的判断.我们来看下他的判断
1.如果是checked异常,直接抛出.很明显,我们的HelloException是RuntimeException,不符合
2.在方法签名上有声明,直接抛出.很明显,我们接口并未声明该异常,不符合
3.异常类和接口类在同一jar包里,直接抛出.很明显,我们的异常类是在common.jar的,接口是在api.jar的,不符合
4.是JDK自带的异常,直接抛出.很明显,这个HelloException是我们自定义的,不符合
5.是Dubbo本身的异常(RpcException),直接抛出.很明显,这个HelloException是我们自定义的,和RpcException几乎没有半毛钱关系.
6.否则,包装成RuntimeException抛给客户端.因为以上5点均不满足,所以该异常会被包装成RuntimeException异常抛出(重要)
如何解决
既然都知道了原理了,那么很好解决,我随便列举一下,比如从规范上要求业务方接口声明HelloException
写在最后
当然肥朝面试的时候,也曾经被问过类似问题,你用XXX有没有遇到过什么坑.在一波操作猛如虎的分析下,面试官说
"你真帅".
肥朝会心一笑
结果他却说
"你笑起来更帅"!