As shown in the figure below, org.springframework.cloud.spring-cloud-commons
a series of interfaces are defined under the package, including serviceregistry
a series of specifications, and the interface is implemented through the SPI mechanism.
In the file of the package META-INF/spring.factories
, you can find this item EnableAutoConfiguration.class
in the value of the key . Let’s take a look at this configuration class: Correspondingly, there is a nacos service registration configuration class under the package (located in the spring-cloud-starter-nacos-discovery jar package) , as shown in the figure below: This configuration class defines the SpringCloud interface above The implementation class:AutoServiceRegistrationAutoConfiguration.class
AutoServiceRegistrationAutoConfiguration
com.alibaba.cloud.nacos
META-INF/spring.fatories
NacosServiceRegistryAutoConfiguration
AutoServiceRegistration
package com.alibaba.cloud.nacos.registry;
...
/**
* @author xiaojing
* @author <a href="mailto:[email protected]">Mercy</a>
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnNacosDiscoveryEnabled
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled",
matchIfMissing = true)
@AutoConfigureAfter({
AutoServiceRegistrationConfiguration.class,
AutoServiceRegistrationAutoConfiguration.class,
NacosDiscoveryAutoConfiguration.class })
public class NacosServiceRegistryAutoConfiguration {
@Bean
public NacosServiceRegistry nacosServiceRegistry(
NacosDiscoveryProperties nacosDiscoveryProperties) {
return new NacosServiceRegistry(nacosDiscoveryProperties);
}
@Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
public NacosRegistration nacosRegistration(
ObjectProvider<List<NacosRegistrationCustomizer>> registrationCustomizers,
NacosDiscoveryProperties nacosDiscoveryProperties,
ApplicationContext context) {
return new NacosRegistration(registrationCustomizers.getIfAvailable(),
nacosDiscoveryProperties, context);
}
/**
** 这里,这个NacosAutoServiceRegistration就是AutoServiceRegistration接口的实现类
**/
@Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
public NacosAutoServiceRegistration nacosAutoServiceRegistration(
NacosServiceRegistry registry,
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
NacosRegistration registration) {
return new NacosAutoServiceRegistration(registry,
autoServiceRegistrationProperties, registration);
}
}
NacosAutoServiceRegistration
The method in register()
starts to register the service, and the whole calling process is as follows:
Continuing to trace the source, this registry()
method is in AbstractAutoServiceRegistration implements ApplicationListener<WebServerInitializedEvent>
the class onApplicationEvent()
method (onApplicationEvent->bind->start->registey).
The heartbeat of the call defaults to once every 5 seconds. For details, please refer to the methods BeatReactor
in the class buildBeatInfo()
. One of them beatInfo.setPeriod(instance.getInstanceHeartBeatInterval());
gets a constant 5s here.
For a more detailed process, please refer to: Source code reading|Young people don’t need to talk about martial arts, but you have to be able to read Naocs source code
NamingClientProxy
In addition , the implementation of the interface in the last step above is as follows. Is the delegate mode (delegate) also called proxy mode? :