面试官问我,使用Dubbo有没有遇到一些坑?我笑了。
1 public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
2 try {
3 Result result = invoker.invoke(invocation);
4 if (result.hasException() && GenericService.class != invoker.getInterface()) {
5 try {
6 Throwable exception = result.getException();
7
8 // 如果是checked异常,直接抛出
9 if (! (exception instanceof RuntimeException) && (exception instanceof Exception)) {
10 return result;
11 }
12 // 在方法签名上有声明,直接抛出
13 try {
14 Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());
15 Class<?>[] exceptionClassses = method.getExceptionTypes();
16 for (Class<?> exceptionClass : exceptionClassses) {
17 if (exception.getClass().equals(exceptionClass)) {
18 return result;
19 }
20 }
21 } catch (NoSuchMethodException e) {
22 return result;
23 }
24
25 // 未在方法签名上定义的异常,在服务器端打印ERROR日志
26 logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost()
27 + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName()
28 + ", exception: " + exception.getClass().getName() + ": " + exception.getMessage(), exception);
29
30 // 异常类和接口类在同一jar包里,直接抛出
31 String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface());
32 String exceptionFile = ReflectUtils.getCodeBase(exception.getClass());
33 if (serviceFile == null || exceptionFile == null || serviceFile.equals(exceptionFile)){
34 return result;
35 }
36 // 是JDK自带的异常,直接抛出
37 String className = exception.getClass().getName();
38 if (className.startsWith("java.") || className.startsWith("javax.")) {
39 return result;
40 }
41 // 是Dubbo本身的异常,直接抛出
42 if (exception instanceof RpcException) {
43 return result;
44 }
45
46 // 否则,包装成RuntimeException抛给客户端
47 return new RpcResult(new RuntimeException(StringUtils.toString(exception)));
48 } catch (Throwable e) {
49 logger.warn("Fail to ExceptionFilter when called by " + RpcContext.getContext().getRemoteHost()
50 + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName()
51 + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
52 return result;
53 }
54 }
55 return result;
56 } catch (RuntimeException e) {
57 logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost()
58 + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName()
59 + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
60 throw e;
61 }
62 }
结果他却说
"你笑起来更帅"
有关Dobbo更多面试题 , 请移步到下列文章查阅 :
https://mp.weixin.qq.com/s/PDWFZbHRfcDg875oGzgEVg
好啦!文章就给看官们分享到这儿
如果觉得有帮助,记得关注、转发、收藏哟~
下期精彩预告
你想要的Dubbo学习资料尽在这里
推荐学习路线:
1、Java学习求职路线(资料、视频、源码、项目实战)
2 、java项目20套 +完整版java学习视频
推荐面试技巧:
推荐阅读:
注:加群要求 学习交流qq群:482670956