Write on the front
Last week, two articles were updated in the Dubbo series《The glacier is starting to attack Dubbo!》And《Overlooking the overall situation of Dubbo, you must master these before reading the source code!!》, received a lot of wechat private chat messages from small partners, most of them are asking how to quickly master the principle and source code of Dubbo. In view of this problem, I am also thinking about how to make my friends better grasp the principle and source code of Dubbo in a simpler and easier way. Today, I’d like to share with you how I quickly mastered the principle and source code of Dubbo.
The article has been included in:
The premise of reading the source code
Before reading a technical framework, or the source code of an open source project, you must understand what the framework is for. To put it bluntly, you must at least understand how to use the framework, what scenarios to use, what pitfalls to encounter in the process of use, and how to solve them. The simplest and most effective way to learn a framework is its official documents. Dubbo is no exception. When I was studying Dubbo, I also read the official documents of Dubbo first. I didn’t read any other articles or materials. Why? Because Dubbo’s official documents are the most authoritative and credible.
Even if you have not used the Dubbo framework in the project, you can also write a simple sample program based on Dubbo according to the official documents of Dubbo. Through the example, you can quickly understand Dubbo.
Why read the source code
Before reading the source code, you should clearly know why you want to read the source code, and look at the source code with questions.If you want to see the source code of the framework, there are no more than two cases: one is to find a solution through the source code when you encounter problems that cannot be solved through documents or other technical materials in the project; the other is to be curious about the underlying implementation of the framework. How is such a powerful technology realized?And I, basically, belong to the latter. In the high parallel power system with hundreds of millions of users, Dubbo is used in some core subsystems. After performance tuning, Dubbo performs well. I’m curious: how does Dubbo resist such high concurrency? How is it realized internally?
Secondly, before reading the source code, you should ask yourself a few questions. For example, before I read the Dubbo source code, I made a lot of questions clear, such as: how does Dubbo implement RPC service? How do service providers and callers interact? How does Dubbo encapsulate network calls to make them look like local methods? How does Dubbo achieve service governance? And so on a series of questions, I remember I asked myself hundreds of questions before I looked at the source code.Looking at the source code with problems can get twice the result with half the effort, not only for Dubbo, but also for other frameworks.
Also, don’t look at the source code just for the sake of looking at the source code. Many partners have looked at the source code of many frameworks, but they soon forget it. It’s the same as not looking at the source code, and they waste a lot of time. This is a typical way of looking at the source code for the sake of looking at the source code, without going into the implementation principles and details of this framework. If you don’t understand it, you can skip it first, record it, and then go back to sort out the source code that you don’t understand.
What technologies are used in the framework
Before reading the source code, it is also important to understand which technologies are used in the framework. Have a general understanding of the technology used. For example, SPI, time round timing task, service registration and discovery, netty, serialization, custom protocol and other technologies are used in Dubbo. Therefore, before reading the source code, we should have a general understanding of these technologies.
For example, before looking at the source code of Dubbo, I already knew that netty is used in the bottom layer of Dubbo’s network interaction. Just when I was working for a short time, I deeply studied the source code of netty, which helps me better understand the network programming part of Dubbo when I look at the source code of Dubbo.
So, one minute on stage and ten years off stage, I was fully prepared before I saw the Dubbo source code.
What should I pay attention to when reading the source code
Any technical framework, or open source project, must be created to solve some scenarios, such as Dubbo. I believe that as a distributed service governance framework, a typical use scenario of Dubbo is distributed system.
Secondly, an excellent technical framework or open source project must contain the principles and implementation of many other technologies. We also take Dubbo as an example. In Dubbo, in order to realize that calling remote RPC service is just like calling local service in a distributed scenario, it needs to realize at least the following functions:
- Communication between services.
- Service invocation needs to achieve load balancing, high availability and current limiting.
- Service governance.
- High reliability and fault tolerance.
- Services can be automatically registered and discovered.
What excellent open source frameworks does Dubbo use to implement these functions? How are these open source frameworks integrated? Is it hard coded integration? Or is it better? All of these require us to find the answers in the source code when we read the source code.
How to read the source code
I summarize the ways to read the source code as follows:First the whole and then the part, first the macro and then the micro, first the rough and then the details.
What do you mean? It’s time to look at the source code, first grasp the design principle and design concept of the source code on the whole, and first understand how the source code is realized on the whole. For example, in Dubbo, network programming is based on netty, so you can learn about it first. After mastering the basic framework source code as a whole, we can make specific implementation from the details. For example, how to use netty to realize network interaction in Dubbo?
Remember, when reading the source code, don’t start sticking to the details of the source code before you understand the overall design principle and the overall source code design. Otherwise, you will lose yourself in the source code.
How to verify that you have the source code?
It’s not enough to read the source code only, but also to do it yourself. It’s just the so-called “practice is the only standard for mastering and sorting”. If you don’t practice, how do you know if you have mastered it.
I divide the practice part into two partsPractice in the process of looking at the source code; practice after looking at the source code.
In the process of reading the source code, hands-on practice means: in the process of reading the source code, record what you don’t understand, consult the official website documents or other information, if it is due to your lack of technical ability, you need to charge yourself in time to supplement the relevant knowledge points. If you think it is a better way to implement it, you can mark your understanding in the form of comments on the code of the framework. For example, when I read the Dubbo source code, I annotated a lot of comments.
After looking at the source code, we must practice. After reading the overall source code of Dubbo, if you think you have mastered Dubbo, you may as well implement an RPC framework yourself to see the differences between your own framework and Dubbo framework, and why Dubbo framework is implemented in this way. If you look back at the source code of Dubbo with questions, you will certainly understand it more deeply. And I, after reading the source code of the Dubbo framework, did the same thing.
Summary of reading source code
Finally, let’s talk to our friends: it took me less than two months from the beginning to see the source code of Dubbo to master the principle and source code of Dubbo. Remember, before reading the source code, you must be clear about why you read the source code, read the source code with questions, and understand other technologies or frameworks involved in the source code. In the process of reading the source code, we should follow the following principles: first the whole, then the part, first the macro, then the micro, first the rough, then the details. The most important point is: in the process of reading the source code and after reading the source code, we should practice.
OK, that’s all for today. I’m glacier. If you have any questions, you can leave a message below. You can also add my wechat to exchange technology, upgrade and force together~~