After reading the source code of Dubbo3, I summed up these skills for reading the source code

Recently, I have received many WeChat private chat messages from friends, most of whom are asking how to quickly grasp the principle and source code of a framework, such as: Spring, Dubbo, MyBatis, etc. In response to this problem, I briefly summarized it over the weekend. Today, I will share with my friends how I used less than a month of spare time (less than 2 hours a day) to quickly grasp the principles and source code of Dubbo.

Prerequisites for reading the source code

Before reading a certain technical framework, or the source code of an open source project, you must understand what the framework is used for. What pits are encountered and how to solve them. The easiest and most effective way to learn a framework is its official documentation. Dubbo is no exception. When I was learning Dubbo, I read Dubbo's official documents first, and basically didn't read other articles or materials. why? Because Dubbo's official documents are the most authoritative and most credible.

Even if you have not used the Dubbo framework in your project, you can write a simple sample program based on Dubbo according to Dubbo's official documentation, and you can quickly understand Dubbo through examples.

Why read the source code

Before reading the source code, you must 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 two situations: one is that you encounter problems that cannot be solved through documents or other technical materials in the project, and you want to find a solution through the source code; the other is the underlying implementation of the framework Curious, how is such a powerful technology realized?  And I, basically belong to the latter. In the high-concurrency e-commerce system with hundreds of millions of users that I have experienced, some core subsystems use Dubbo. After performance tuning, Dubbo performs well. I'm curious: How does Dubbo resist such high concurrency? How is it implemented internally?

Secondly, before reading the source code, you have to ask yourself a few questions. For example, before I read the Dubbo source code, I clarified many questions, such as: How does Dubbo implement RPC services? How do service providers and callers interact? How does Dubbo encapsulate network calls to make them look the same as calling local methods? How does Dubbo implement service governance? Waiting for a series of questions, I remember that I asked myself hundreds of questions before looking at the source code. Looking at the source code with questions can make you 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 for the sake of looking at the source code. Many friends have read the source code of many frameworks, but they forget it after a while. This is a typical look at the source code for the sake of looking at the source code, without delving into the implementation principles and details of this framework. Go back and reorganize the source code that you don't understand.

Which technologies are used by the framework

Before reading the source code, another important task is to understand which technologies are used in the framework. Have a general understanding of the technology used. For example: SPI, time wheel timing tasks, service registration and discovery, Netty, serialization, custom protocols and other technologies are used in Dubbo. Therefore, before reading the source code, you must first have a general understanding of these technologies.

For example, before looking at the source code of Dubbo, I already knew about Netty, which is used at the bottom layer of Dubbo’s network interaction. Just when I was working, I studied the source code of Netty in depth, which helped me to read the source code of Dubbo. , can better understand Dubbo's network programming part.

So, one minute on stage, ten years of work off stage, I also made full preparations before watching the Dubbo source code.

What to pay attention to when reading the source code

Any technical framework, or open source project, must be born to solve certain scenarios, such as Dubbo, I believe everyone knows that Dubbo is a distributed service governance framework, and a typical usage scenario is distributed system.

Secondly, an excellent technical framework or open source project must contain the principles and implementations of many other technologies. We also take Dubbo as an example. In Dubbo, in order to realize that calling a remote RPC service is like calling a local service in a distributed scenario, at least the following functions need to be implemented:

  • High-performance communication between services.

  • Service calls need to achieve load balancing, high availability, and current limiting.

  • Service Governance.

  • High reliability and fault tolerance.

  • Services can be automatically registered and discovered.

When implementing these functions, which excellent open source frameworks does Dubbo use? How are these open source frameworks Dubbo integrated? Is the integration hardcoded? Or is there another better way? These all require us to find the answer in the source code when we read the source code.

How to read the source code

I summarize the way of reading the source code as: first the whole and then the part, first the macro and then the micro, and first the rough and then the details.

What does that mean? That is, when looking at the source code, first grasp the design principles and concepts of the source code as a whole, and first understand how the source code is implemented as a whole. For example, in Dubbo, network programming is implemented based on Netty, so you can get to know it first. After grasping the basic source code of the framework as a whole, we will focus on the 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 deadlocking the details of the source code before you understand the overall design principles and overall source code design. Otherwise, you will lose yourself in the source code.

How to verify that you have mastered the source code?

Reading the source code is not enough to just look at the source code, you have to do it yourself. As the saying goes, "practice is the only criterion for mastering and organizing". If you don't practice, how will you know if you have mastered it.

I divide the practical part into two components: hands-on practice while looking at the source code; hands-on practice after looking at the source code.

Hands-on practice in the process of looking at the source code means: in the process of looking at the source code, record what you don’t understand, and consult the official website documents or other materials. It is time to recharge yourself in time to supplement relevant knowledge points. If you encounter a better implementation method that you think, you can mark your understanding in the form of comments on the code of the framework. For example, when I was looking at the Dubbo source code, I marked a lot of comments.

After looking at the source code, you must do it yourself. After reading the overall source code of Dubbo, if you think you have mastered Dubbo, you may wish to implement an RPC framework by yourself, and see what is the difference between the framework you implemented and the Dubbo framework, and why the Dubbo framework is implemented in this way. Then turn around and look at Dubbo's source code with questions, and you will definitely understand it more deeply. And I did the same after reading the source code of the Dubbo framework.

Read the summary of the source code

Finally, let me reveal to my friends: From the beginning of reading Dubbo source code to mastering the principle and source code of Dubbo, it only took me less than a month, and it was all in my spare time (less than 2 hours a day).

Remember, before reading the source code, you must clarify 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, you must follow the principle of: first the whole and then the part, first the macro and then the micro, and the first rough and then the details. The most important point is: you must do it in the process of reading the source code and after reading the source code.

Guess you like

Origin blog.csdn.net/g6U8W7p06dCO99fQ3/article/details/131345824