In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Preface
In 17 years, due to the impulse out of control (of course, recently, some fans asked me to update again and again), combined with the interview inscription to write a series of Dubbo source code analysis. At present, most of the fans of the official account are former fans, and there is not too much introduction here.
According to my interview experience, it is very competitive to write key words such as principle and source code on my resume. Last week, I communicated with an official account fan about the interview as follows
During the interview, analyze the source code to make the interviewer's body shake! After a while of foreplay, I thought it was nothing more than a physical twitch and everything became dull, but the interviewer came and reversed the plot.
"you are so familiar with the Dubbo source code, do you encounter any holes when you use it?"
I wipe, unprepared him, chrysanthemum suddenly tight! At this time, we are faced with the situation of bluffing 50K and only 5K if we can't be bluffed.
On how to fight back
I believe everyone has encountered similar problems in the interview, because there are a lot of source code analysis on the Internet, and many people "surprise" before the exam, but when they encounter interviewers who like to ask for details, they can't escape the law and have nowhere to hide. When faced with this problem, how can we fight back? Then I will start with a chat transcript. After all, only if we follow the official account of Feichao and have the source code of the real scene (very important), when we encounter this kind of problem, the tiger will not cry.
Real scene description
So let's get rid of the business correlation and extract a simplest model. When we are in a company, we usually have our own custom exception, and then this custom exception is generally placed in common.jar for other module dependencies. For example, I define a HelloException here.
1public class HelloException extends RuntimeException {23 public HelloException () {4} 56 public HelloException (String message) {7 super (message); 8} 910}
Then we write the simplest demo for Dubbo, as follows
Interface
1public interface DemoService {23 String sayHello (String name); 45}
Provider
1public class DemoServiceImpl implements DemoService {23 public String sayHello (String name) {4 throw new HelloException ("official account: Feichao"); 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 ("official account: Fei Chao"); 12} catch (HelloException helloException) {13 System.out.println ("helloException exception caught here"); 14} 15} 1617}
According to the description of the chat transcript, consumer calls provider,provider to throw HelloException. But what consumer captured is not HelloException.
So let's take a look at it.
As the colleague said. What causes it? Students who have not seen Fei Chao Dubbo source code analysis series before tend to use the least efficient solution, throwing the abnormal stack to the WeChat group and asking for help. But often nothing, and then sigh why society is so indifferent!
But old fans of the official account have already mastered the skills of reading the source code, sitting as restlessly as Fei Chao, nine shallow and one deep into the source code. If there is an exception, let's first take a look at the exception stack.
Unless you can't see too much (it is recommended to quit), this line is as unusual as Fat Chao, like a firefly in the dark, so distinct and outstanding
1com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke (ExceptionFilter.java:108)
So let's find out.
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 () 78 / / if it is a checked exception, throw 9 if (! (exception instanceof RuntimeException) & & (exception instanceof Exception) {10 return result;11} 12 / / has a declaration on the method signature, throwing 13 try {14 Method method = invoker.getInterface () .getMethod (invocation.getMethodName (), invocation.getParameterTypes ()) directly. 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} 2425 / / for exceptions not defined on the method signature, print the ERROR log 26 logger.error ("Got unchecked and undeclared exception which called by" + RpcContext.getContext (). GetRemoteHost () 27 + "on the server side. Service: "+ invoker.getInterface (). GetName () +", method: "+ invocation.getMethodName () 28 +", exception: "+ exception.getClass () .getName () +": "+ exception.getMessage (), exception); 2930 / / exception class and interface class are in the same jar package and 31 String serviceFile = ReflectUtils.getCodeBase (invoker.getInterface ()) is thrown directly 32 String exceptionFile = ReflectUtils.getCodeBase (exception.getClass ()); 33 if (serviceFile = = null | | exceptionFile = = null | | serviceFile.equals (exceptionFile)) {34 return result;35} 36 / / is an exception inherent in JDK, and 37 String className = exception.getClass () .getName () is thrown directly 38 if (className.startsWith ("java.") | | className.startsWith ("javax.")) {39 return result;40} 41 / / is an exception of Dubbo itself. 42 if (exception instanceof RpcException) {43 return result is thrown directly. 44} 4546 / / otherwise, wrap it as RuntimeException and throw it to the client 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 escape 61} 62} 1. If it is a checked exception, throw it directly. Obviously, our HelloException is RuntimeException, which does not match 2. If there is a declaration on the method signature, throw it directly. Obviously, our interface does not declare the exception, which does not conform to 3. 5. Exception class and interface class are in the same jar package and are thrown directly. Obviously, our exception class is in common.jar and the interface is in api.jar, which does not conform to 4. 5. Is an exception that comes with JDK and is thrown directly. Obviously, this HelloException is customized by us and does not conform to 5. 5. Is an exception (RpcException) of Dubbo itself, which is thrown directly. Obviously, this HelloException is customized by us and has almost nothing to do with RpcException. 6. Otherwise, wrap it as RuntimeException and throw it to the client. Because none of the above 5 points is satisfied, the exception will be wrapped as a RuntimeException exception thrown (important)
This is why our catchHelloException is less than catch, because it is packaged as RuntimeException.
Why is Dubbo designed like this?
You may think this judgment is very bad when you see it here. Why did Dubbo design it this way? We look at the source code, the most important thing is to know why the author designed so, only know why such a design is after in-depth thinking, otherwise look at the best part, read and forget. To explain clearly why it is so designed is also an important reason for everyone to pay attention to the official account of Fei Chao.
In fact, this consideration of Dubbo is based on serialization. If you think about it, if provider throws an exception that is customized only in provider, then the exception reaches consumer and is obviously unserializable. So you pay attention to Dubbo's judgment. Let's take a look at his judgment
1. If it is a checked exception, throw it directly. Obviously, our HelloException is RuntimeException, which does not match 2. If there is a declaration on the method signature, throw it directly. Obviously, our interface does not declare the exception, which does not conform to 3. 5. Exception class and interface class are in the same jar package and are thrown directly. Obviously, our exception class is in common.jar and the interface is in api.jar, which does not conform to 4. 5. Is an exception that comes with JDK and is thrown directly. Obviously, this HelloException is customized by us and does not conform to 5. 5. Is an exception (RpcException) of Dubbo itself, which is thrown directly. Obviously, this HelloException is customized by us and has almost nothing to do with RpcException. 6. Otherwise, wrap it as RuntimeException and throw it to the client. Because none of the above 5 points is satisfied, the exception will be wrapped as a RuntimeException exception thrown (important)
How to solve
Now that we know the principle, it's easy to solve. I'll list it casually, for example, from the specification, the business side interface is required to declare HelloException.
Write at the end
Of course, during the interview with Fei Chao, I was asked similar questions. Have you ever encountered any holes in using XXX? Under a wave of fierce analysis, the interviewer said
You are so handsome.
Fat Chao smiled a knowing smile.
And then he said,
"you smile more handsome"!
Free Java data collection, covering Java, Redis, MongoDB, MySQL, Zookeeper, Spring Cloud, Dubbo/Kafka, Hadoop, Hbase, Flink and other highly concurrent distributed, big data, machine learning and other technologies. Portal: https://jq.qq.com/?_wv=1027&k=5F79bMq
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.