Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Hi, everyone, I am three days of fishing, two days of sun net small 66, welcome to pay attention to my public number: “six pulse Shenjian program life”, learn together, progress together

omg

I have analyzed some core principles of Nacos as a distributed configuration center for services. I think our project also uses Nacos to register and discover services. So? It is necessary to take some time to analyze him.

  • Principle of SpringBoot2.2.0+Nacos as distributed configuration center (I)
  • Principle of Nacos as a Distributed Configuration Center (II)
  • @refreshScope Automatic refresh principle (3)

The service registry

Let’s start with the official example. Nacos is an implementation of Spring Cloud. Let’s take a look at himIt’s actually quite simple. Let’s go inside and follow the code

Create the NamingService code as follows

In fact, it is the use of reflection, find his construction method, sure enough, a person wrote, and configuration center is exactly the same routine, we continue to follow

So the important thing is his init method below

Let’s take a look at the next few

EventDispatcher: the EventDispatcher manages the EventListener. The scheduler within defined ScheduledThreadPoolExecutor cycle, Create a com. Alibaba. Nacos. Naming. Client. The listener thread cycle Notifier mission (registered in the EventListener NamingEvent incident via the Notifier, can be used for local extension (implementation Appl The icationListener interface monitors NamingEvent events)

BeatReactor: The client and the server class cycle heartbeat detection, defined inside scheduler ScheduledThreadPoolExecutor cycle, Create a com. Alibaba. Nacos. Naming. Beat the sender thread cycle BeatTask tasks (such as an inner class of BeatReactor for heartbeat for sending information to the server, then sent via httpclient path for/instance /beat HTTP request), internal maintenance of the map with serviceName+groupName+ IP +host as key, BeatInfo as value, when the client initial registration instance to the server will create a BeatInfo object, Write to addBeatInfo() in the BeatReactor and schedule a BeatTask after the beatInfo attribute period(default 5s). For details about heartbeat detection, see the follow-up description of heartbeat mechanism

HostReactor: Client cycle to pull the server code, internal definition ScheduledThreadPoolExecutor cycle scheduler, Create a com. Alibaba. Nacos. Client. Naming. Updater thread cycle UpdateTask tasks (such as HostReactor in an inner class used to update the client cache service registration list information, at the same time of access list, The server generates a PushClient object that tells the server its UDP port number. If the server changes its UDP port number, the server can send the change message through PushClient. UpdateTask is updated periodically in the unit of service-cluster. The update frequency is 1s by default. Send HTTP requests via updateServiceNow() -/instance/list)

Registration method

Let’s go ahead and look at this method

In fact, it is very simple, one is to maintain the state of mind detection, the following is the real registration

Client service registration flowchart

Daily for praise

All right, everybody, that’s all for this article. All the people here are talented.

Creation is not easy, your support and recognition, is the biggest motivation for my creation, we will see in the next article

Six pulse excalibur | article “original” if there are any errors in this blog, please give criticisms, be obliged!