[Fei Chao] One-to-one handwriting of SPI in Dubbo source code, with git address attached

[Fei Chao] One-to-one handwriting of SPI in Dubbo source code, with git address attached


I have shared SPIfour articles about dubbo before . Everyone has some preliminary understanding of this concept. When it comes to how to advance the level of programming, everyone may say three words in unison, but I always think that programming is light , I will never learn. But many times, I can't even look at the source code. You told me to write a copy of the source code. Isn't this a joke?

We can recall how we write code in the company. 1. the product puts forward the requirements, then the requirements review meeting, then the code design, and finally the coding.

Today we will imitate this process and implement dubbo by ourselvesspi

Make a demand

In ? What's the difference Dubbo and the JDK SPI exactly , we have analyzed the dubbo in spi and spi What is the difference in jdk, jdk dubbo compared to changes along the following lines:

(1) The extension point adds caching and improves the performance (2) spiThe default value is added (3) keyThe form to obtain the extension point is added (4) IocAnd the AOPfunction is added

Here is a digression, we can't look at the source code for the sake of looking at the source code. The most important thing is, in the process of looking at the source code, learn to analyze the idea of the problem. If there is a new RPC framework, such as Fei Chao RPC, I ask you Fei What are the differences between the SPI in RPC and the SPI in JDK? In Dubbo JDK SPI ?this article, I will show how I analyzed the entire mental journey of these four points. You can boldly enter my inner world.

Requirements review

Since it is a requirement review, in fact, to put it bluntly, it is to cut the requirements with the product. The requirements put forward by the product are the above four functions. (1), (2) and (3) are all basic and core functions. These are cut down in this version. It's boring, (four) this can be placed in the next version for iterative development.

Code design

As long as the key-valueacquisition of extension points is achieved , then it is easy to obtain the default extension points, because this default extension point is only key-valuea special form, and his default value, which is this key, is in the SPIannotation. In fact, the idea is just one sentence. Well, because we have configured the implementation class permissions in the extension point configuration file. 1. we load all these configuration files, parse out the fully qualified name, and cache it. Then you want to get the specific extension point, I'm here The name of his permission is taken out in the cache, and the reflection is instantiated into an object and returned. That is, 1. Read and parse the file content 2. Put it into the map cache 3. Reflection generate the object. These three knowledge points, I believe this No one will not know this article.

According to the above analysis, it is not difficult to write the code. What, can't write? Then look down


The project structure is shown in the figure:

The results of the operation are shown in the figure

The code has been uploaded to Code Cloud. It can be downloaded and run. I originally wanted to use the timing diagram and flowchart to sort out the ideas with everyone, but after a few hours, I think it is better to look at the code clearly. The core code code is less than 300 OK. If you have any questions, you can leave a message.

Address: gitee.com/HelloToby/t


Because some of the previous students' feedback is not very clear about the ideas in the source code. So this time I will directly extract the functional modules in the source code into a demo, and then everyone will run the demo first, and then repeat the code in the demo. Twice. Because the demo and the source code are highly imitated (variable names, method names and even if not changed lines are as consistent as possible). So at this time, looking at the source code again should be the same as looking at the code you wrote. This way The idea is much clearer. I hope this method is helpful for you to read the source code.

Write at the end

Fei Chao is a technical public account that focuses on principles, source code, and development skills. The original thematic source code analysis in the account, and the actual source code principles of real scenes (emphasis). Scan the QR code below to pay attention to Fei Chao, so that you, who should have made rockets, stop screwing the screws!