Nacos implementation of SpringCloud service registration

As shown in the figure below, org.springframework.cloud.spring-cloud-commonsa series of interfaces are defined under the package, including serviceregistrya series of specifications, and the interface is implemented through the SPI mechanism.
insert image description here
In the file of the package META-INF/spring.factories, you can find this item EnableAutoConfiguration.classin 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
insert image description here
AutoServiceRegistrationAutoConfiguration
insert image description here
com.alibaba.cloud.nacosMETA-INF/spring.fatories
insert image description here
NacosServiceRegistryAutoConfigurationAutoServiceRegistration


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);
	}

}

insert image description here

NacosAutoServiceRegistrationThe method in register()starts to register the service, and the whole calling process is as follows:

insert image description here
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 BeatReactorin 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

NamingClientProxyIn addition , the implementation of the interface in the last step above is as follows. Is the delegate mode (delegate) also called proxy mode? :
insert image description here

Guess you like

Origin blog.csdn.net/weixin_41866717/article/details/123224101