preface

Recently, I wrote a series of articles about the source code of Nacos. Many friends are interested in asking: What source code are you reading these days, and how do you read it? Today’s article begins with a reading of the Nacos source code. Friends who are interested in reading source code or technology can also add wechat exchange (wechat: Zhuan2quan).

As you read this article, it depends on what you want to achieve. Because this article is also about how to read the Nacos source code, and how to read the source code. Don’t get bogged down in the technology stack, refine your own approach. Whatever you want, take what you need.

Read the source code for purpose

It is not clear why we want to read the source code, to talk about the purpose of personal reading source code, perhaps can be used for reference.

Learn the underlying principles and implementation

Read the source code of a framework, the most important purpose is to learn its underlying implementation and principle more deeply. The underlying implementation and principles here are relatively macro. Reading the Nacos source code, for example, I wanted to know how it implements service registration, service discovery, and how those service instances are stored.

Articles like “Nacos, the Soul Ferryman of Microservices,” came from such readings. You can also do what I did by drawing flow charts, architecture diagrams, data structures, and even articles to help you learn and understand.

Learn good code design.

This one contains a lot of points, such as architectural design, functional implementation ideas, good code demonstrations, design patterns, algorithms, and so on. We can learn from all the good practices we can see.

Using Nacos as an example, a simple entry method for logging off, how much can you see to learn?

The image above is the most intuitive feeling the code gives me when I look at it. Then you can look at the code in your own project and ask, can you meet that high standard? Can it be modified?

Another example from Nacos Client involves the retry mechanism and the logic of registering multiple servers when calling the Server API in the Client. See how Nacos is implemented.

Regardless of the pros and cons of the algorithm, does it feel like you’ve learned another implementation here? And you see how the Nacos Client handles request retries and exceptions when calling the Server. Interesting.

Of course, there are more in-depth aspects to this level, such as consistency algorithms and many other solutions.

Learn the use of knowledge points

This term is a little bit more granular. We already have Optional use cases in Nacos, it’s time to be careful about this syntax, and Why is the String. Intern method used in Nacos source code? Are the knowledge points excavated when reading the source code.

There’s a nice point about this layer that you really have to grasp. You’ve probably read a lot of articles about the same thing, but also some simple examples. But if you haven’t had a chance to practice it, or if you haven’t used it on a large project, it’s interesting to look at the implementation and think about it in the source code.

The use of the String.intern method in Nacos, for example, is not as easy as you might think. And if you think about it a little bit more, you’ll see that not every scenario is appropriate, and caching in the constant pool is only appropriate if the string doesn’t change much.

Here is another example of the use of common knowledge points in Nacos to see whether we think in the same dimension. The ServiceManager has the following member variables:

You probably know the above by heart, but have you ever seen how to use it? Have you seen how it works in combination? How to support distributed, highly concurrent scenarios?

As long as you carefully analyze, read the implementation of the source code, you can get the answer, but also a practice.

There are too many things to learn from the source code, I will not explain one by one here, and I will gradually form a series of articles in the form of the source code technology and ideas to share with you.

How to read source code

With the goal of reading the source code, the next step is execution. Here are some of the ways I read the source code, not necessarily suitable for you, but can be referenced and improved. Nacos is taken as an example and no special explanation will be given in the future.

Code download

The source code for Nacos is available on two platforms: GitHub and the Code Cloud. Code cloud library as synchronization, regularly updated. I’ll use GitHub as the source, and maybe contribute some code at some point.

You can directly execute git command to pull open source library code:

git clone [email protected]:alibaba/nacos.git
Copy the code

Personally, however, it is not recommended to pull code directly from the repository fork of NacOS to your GitHub account. This keeps you in sync with the trunk and makes it easy to make changes.

The project structure

After downloading, open the project directly through IDEA. After the dependent class library is introduced, the following project structure can be seen:

At this point you can roughly understand the directory structure, basic can see the name of the meaning.

  • Address: address service related.
  • API: API extraction of Naming and config;
  • Auth: permission control related;
  • CMDB: Connects to a third-party CMDB to obtain CMDB data.
  • Client: client code;
  • Common: Common tool classes;
  • Config: implementation of Nacos configuration center;
  • Consistency: implementation of consistency;
  • Console: Nacos console related implementation;
  • Console-uri: implementation of the CONSOLE UI part;
  • Core: property loading, initialization, listener dependent;
  • Distribution: distribution related;
  • Example:
  • Istio: Supports IStio, such as K8S.
  • Naming: Nacos core function, dynamic service discovery;

Project initiation and testing

To read the source tracking process, you must get the project up and running. Since Nacos is built on Spring Boot, you simply execute the main method of the corresponding entry class.

In the Console project, execute the main method of Nacos to start the project. Nacos starts cluster mode by default, so the single-machine mode can be started in the study code first. Here we specify the single-machine mode by adding arguments to the main method:

Public class Nacos {public static void main(String[] args) System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "true"); System.setproperty ("nacos.core.auth.enabled", "false"); SpringApplication.run(Nacos.class, args); }}Copy the code

After the program is started, the NamingTest and ConfigTest classes can be seen in the unit test (Test) of the client project. Directly executing the methods in the unit test can connect to the newly started Server for code trace debugging.

At this point, regarding the operation of different APIS, you can also refer to the official documentation for the call verification, which will not be demonstrated.

Sort out the project process

Once the code is downloaded and launched, how do you comb through the business logic of the source code? Many friends may encounter some confusion, such as not understand, or read and forget, have to comb through again each time.

This kind of condition is easy to appear in the early stage, also be considered normal situation. What I do is I branch the code for fork, for example I branch for comment. On this branch, comments are added every time you see a line of code that is a bit difficult or that needs to be commented.

About Nacos himself fork code address https://github.com/secbr/nacos, of which the comment in gradually add comments in the branch, for Nacos source interested friends can see.

For example, in the figure above, the client requests the server to register part of the code logic. I will describe the combed path in the form of comments. So you don’t have to brush it every time you see it.

When you see more of the above logic, you will not forget it. Add comments logic by logic, class by class, and when you enjoy combing through flowcharts or architecture diagrams, simply refine and summarize the contents of the comments.

The client is registered with the server.

At this point, the core of reading the source code is done. The rest is to sort out business processes one by one and learn technical points one by one. If you encounter parts that you don’t understand, it’s a good idea to trace them in Debug mode to see what the data is at this point in time.

summary

Personally feel that reading source code is a necessary skill for every programmer, but also standing on the shoulders of giants to improve their own necessary means. Only by reading more will you know what is better written and implemented. Of course, everyone at this stage is limited, there are a lot of technical points or design ideas may not be able to see the current stage, but it doesn’t matter, you can also take me to do a cushion, after all, I plan to write a source code analysis series.

Nacos series

Nacos, the soul ferry of microservices, is a complete guide to the principle. Nacos has Optional use cases in Nacos. It’s time to take this syntax seriously. Why is the String. Intern method used in Nacos source code? \


Program new horizon

\

The public account “program new vision”, a platform for simultaneous improvement of soft power and hard technology, provides massive information

\