[Source code analysis] Nacos automatic registration source code analysis

Service Registration (AP Protocol)

Nacos provides the registerInstance method of NamingService to provide the function of service registration.
So as long as we can get this NamingService, we can complete the registration of the service.
insert image description here

We can get NamingService and configuration center ConfigService through NacosFactory.
insert image description here

And the bottom-level acquisition method of this registry is actually to rebuild a registry class through reflection.
insert image description here

So we can register and get all the instances of the registry as follows, and monitor the changes of the corresponding instances of the registry, the code is as follows:

public class RegisterMain {
    
    
    public static void main(String[] args) throws NacosException {
    
    
        NamingService namingService = NacosFactory.createNamingService("123.249.97.220:8848");
        namingService.registerInstance("config-center","localhost",8808);
        namingService.registerInstance("config-center","localhost",8809);

        List<Instance> allInstances = namingService.getAllInstances("config-center");
        System.out.println(allInstances);
        System.out.println("---------------");
        namingService.subscribe("config-center", new EventListener() {
    
    
            @Override
            public void onEvent(Event event) {
    
    
                NamingEvent namingEvent = (NamingEvent) event;
                System.out.println("监听器监听到服务变更,服务名称为:"+namingEvent.getServiceName());
                System.out.println("监听器监听到服务变更,当前实例列表为:"+namingEvent.getInstances());
            }
        });
        while(true){
    
    

        }
    }
}

The specific method of registering an instance is as follows. At this time, Nacos will choose to use grpcproxy or httpproxy proxy to register the instance according to whether the created instance is a short one.
insert image description here
insert image description here
insert image description here
In fact, it can be seen that the service registration is to send a POST request to the server and register the service.

Service discovery (CP protocol)

When we obtain services from the registration center, we must ensure the consistency of the services. Otherwise, some services hang up, and you can still get his instances. As a result, every request is reported as an error, so the service discovery chooses the CP protocol. , which is Raft.
In fact, the bottom layer of getAllInstances in service discovery must be the same process as the above one.
insert image description here

We choose httpclientproxy to view, and we can find that the bottom layer is a get request
insert image description here

How is Nacos integrated into SpringCloudAlibaba?

We know that SpringBoot provides an extension point based on Spring, and it is very complicated to directly use Spring to integrate third parties.
With SpringBoot, based on the idea that SpringBoot's convention is better than configuration, we only need to write the classes we need to automatically load in the spring.factories file. After SpringBoot2.7, the way of automatic injection has been changed and become simpler.
insert image description here

At this point, you can see that the registration service of Nacos has been loaded
insert image description here
insert image description here

So the above set of processes can be understood as: after we introduce Nacos as the registration center.
SpringBoot will automatically load our configuration class, and then this configuration class contains the configuration of our registration center, that is, the registration method has been provided for us. Then our problem at this time becomes, how to automatically call the register method of NacosServiceRegistry to register the current service to the registry after the current service is started.
The above bean is used to complete the encapsulation of configuration properties, and the bottom bean is used to complete the automatic registration of our services. It can also be seen from the name that Nacos automatically registers services.
insert image description here

We now focus on the NacosAutoServiceRegistration class.
We clicked on this class and found that its inheritance relationship is as follows:
insert image description here

The focus is on this ApplicationListener. We know that after Spring completes the container creation, it will call the refresh method of AbstractApplicationContext, and this method will register the listener and publish an event.
insert image description here
And this event is the container refresh event.
insert image description here
And this event will eventually trigger the web container initialization event, thereby triggering the corresponding listener.
insert image description here
After this event is published, it will be monitored by our listener and the corresponding logic will be executed, as follows:
insert image description here
insert image description here
It can be found that the event processing here includes An automatic registration class provided by Nacos to complete. The reason is that after the above Nacos automatic registration class implements ApplicationListener,
insert image description here
insert image description here
the start method will register
insert image description here
and this register method will call the specific registration method implemented by it.
insert image description here
This method is the register method provided by NacosServiceRegistry, and it has been introduced in us. The dependency of Nacos-discovery has been automatically injected.
insert image description here
We know that SpringCloud is actually a kind of SPI idea. It is only responsible for providing a group of interfaces, and other manufacturers implement specific methods based on this interface. Alibaba is one of them, so we can see that NacosServiceRegistry actually implements the interface provided by SpringCloud. That's all.
insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/Zhangsama1/article/details/132129757