[Spring Cloud] Nacos service registration process source code

1. Nacos client depends on the project version

2.2.2.RELEASE

2. @EnableDiscoveryClient controls the service registration function

To enable the service registration of the nacos client side, you need to add the @EnableDiscoveryClient annotation to the main startup class

Let's first see what this annotation does

image-20220328192658892

There is an attribute boolean autoRegister() default true; and the default value is true, that is, the automatic registration of the service is enabled by default

Imported an EnableDiscoveryClientImportSelector selector component

Inherited SpringFactoryImportSelector, look at its selectImports method

image-20220328205620753

Get the value of autoRegister from the attribute of the annotation. The default is true. If it is true, add it to the container

org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration

image-20220328205907264

@EnableConfigurationProperties({AutoServiceRegistrationProperties.class})

Make the AutoServiceRegistrationProperties configuration class take effect

image-20220328193229478

have default values

That is to say, the @EnableDiscoveryClient annotation can register the service normally without adding it.

If the @EnableDiscoveryClient annotation is added and the property autoRegister is set to false, the nacos service will not be added

or can be set

spring.cloud.service-registry.auto-registration.enabled= false   ///默认值为 true

3. Automatic configuration mechanism

image-20220328193250359

  com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration,\
  com.alibaba.cloud.nacos.ribbon.RibbonNacosAutoConfiguration,\
  com.alibaba.cloud.nacos.endpoint.NacosDiscoveryEndpointAutoConfiguration,\
  com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration,\
  com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration,\
  com.alibaba.cloud.nacos.discovery.reactive.NacosReactiveDiscoveryClientConfiguration,\
  com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration,\
  com.alibaba.cloud.nacos.NacosServiceAutoConfiguration

Which added a NacosServiceRegistryAutoConfiguration automatic configuration class

4. NacosServiceRegistryAutoConfiguration

image-20220328193354641

new NacosAutoServiceRegistration object

Follow up to the NacosAutoServiceRegistration class

5.NacosAutoServiceRegistration

com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration

public class NacosAutoServiceRegistration
      extends AbstractAutoServiceRegistration<Registration> 

Inherited the AbstractAutoServiceRegistration abstract class

The architecture is shown in the figure

image-20220328193627564

image-20220328193824377

First initialize the parent class and click on the super(serviceRegistry, autoServiceRegistrationProperties) method

Note that the configuration information class that calls the parent class constructor will not have an instance of AutoServiceRegistrationProperties in the container if the @EnableDiscoveryClient annotation is not added.

image-20220328193956208

6.AbstractAutoServiceRegistration

org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration

image-20220328194128411

Note that this abstract class implements the ApplicationListener interface and rewrites the void onApplicationEvent(E event) method of the modified interface to monitor the startup event of the Spring container and the type ApplicationListener is specified here

image-20220328194634156

org.springframework.boot.web.context.WebServerInitializedEvent

That is to listen to the events of the WebServer

6.1 onApplicationEvent Method

Go back to the onApplicationEvent method to see what the AbstractAutoServiceRegistration.onApplicationEvent listener event method does

image-20220328195019756

The bind method of this class is called

6.2 The bind method

image-20220328195615271

In the modified method, the port property of the current AbstractAutoServiceRegistration object is changed to the port number of the web container through cas

Then call the start method of the current object

6.3 start method

image-20220328200113667

6.4 register() method

image-20220328200134343

The ServiceRegistry.register method is called

ServiceRegistry is an interface with only one implementation class NacosServiceRegistryimage-20220328200444670

7.NacosServiceRegistry

com.alibaba.cloud.nacos.registry.NacosServiceRegistry

7.1 register method

image-20220328201004426

Simply look at getNacosInstanceFromRegistration to generate Instance

image-20220328201213381

image-20220328201406601

The NamingService.registerInstance method is called

NamingService is also an interface and has only one implementation class NacosNamingService

8. NacosNamingService

8.1 The registerInstance method

image-20220328201514946

serverProxy.registerService is called

The serverProxy type is NamingProxy

9.NamingProxy

com.alibaba.nacos.client.naming.net.NamingProxy

9.1 registerService method

See what this method does

image-20220328201814165

Created a new HashMap Automatically initialized the container to 16 Encapsulated 11 parameters Why the default size of 11 parameters is set to 16 2️⃣ The capacity of hashmap is the power of 2

Finally call the reqApi method of this class

Pay attention to the three parameters passed in calling this method

  1. `UtilAndComs.nacosUrlInstance constant class here is /nacos/v1/ns/instance

image-20220328202229020

  1. `params

​ hashmap encapsulated parameters

  1. `“POST”

    ​ A simple string is used to see what is done in the method

9.2 reqApi method

image-20220328202435324

The overloaded method of this class is called

image-20220328203144550

The callServer method of this class is called

9.3 callServer method

image-20220328203953073

Finally, the nacosRestTemplate.exchangeForm(url, header, params, body, method, String.class) method of this class is called

nacosRestTemplate is of type NacosRestTemplate

10 NacosRestTemplate

final send request

image-20220328204214330

Remember the address to send the request to http://xxxx:8848/nacos/v1/ns/instance

So far, the registration process of the Nacos client service is over.

Guess you like

Origin blog.csdn.net/JAVAlife2021/article/details/123806777