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
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
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
@EnableConfigurationProperties({AutoServiceRegistrationProperties.class})
Make the AutoServiceRegistrationProperties configuration class take effect
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
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
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
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.
6.AbstractAutoServiceRegistration
org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration
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
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
The bind method of this class is called
6.2 The bind method
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
6.4 register() method
The ServiceRegistry.register method is called
ServiceRegistry is an interface with only one implementation class NacosServiceRegistry
7.NacosServiceRegistry
com.alibaba.cloud.nacos.registry.NacosServiceRegistry
7.1 register method
Simply look at getNacosInstanceFromRegistration to generate Instance
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
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
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
- `UtilAndComs.nacosUrlInstance constant class here is /nacos/v1/ns/instance
- `params
hashmap encapsulated parameters
-
`“POST”
A simple string is used to see what is done in the method
9.2 reqApi method
The overloaded method of this class is called
The callServer method of this class is called
9.3 callServer method
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
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.