nacos 服務註冊原理

CyrusHuang發表於2024-09-05

springboot 的各種 starter 會根據 SPI 機制,讀取 META-INFO/spring.factories 檔案,自動註冊一些 bean,spring-cloud-starter-alibaba-nacos-discovery 的 spring.factories 如下:

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
  com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration
org.springframework.boot.SpringApplicationRunListener=\
  com.alibaba.cloud.nacos.logging.NacosLoggingAppRunListener

發現並沒有自動配置的自動裝配的類?wtf?

從 spring boot2.7開始,慢慢不支援 META-INF/spring.factories 檔案了,需要匯入的自動配置類可以放在
/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports,我這是 springcloud2023 版本,對應是 spring3.x 所以要看 META-INF/spring 下的檔案

com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration
com.alibaba.cloud.nacos.endpoint.NacosDiscoveryEndpointAutoConfiguration
com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration
com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration
com.alibaba.cloud.nacos.discovery.NacosDiscoveryHeartBeatConfiguration
com.alibaba.cloud.nacos.discovery.reactive.NacosReactiveDiscoveryClientConfiguration
com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration
com.alibaba.cloud.nacos.loadbalancer.LoadBalancerNacosAutoConfiguration
com.alibaba.cloud.nacos.NacosServiceAutoConfiguration
com.alibaba.cloud.nacos.util.UtilIPv6AutoConfiguration

NacosServiceRegistryAutoConfiguration 主要看這個,這個類上面有一些註解,裝配了 3 個 bean,這是 spring 的知識,可以翻翻前面的文章,這裡就不贅述了,只要看 NacosAutoServiceRegistration 這個 bean(名字中帶有 auto,意義也是一樣的,完成自動註冊)

@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(
			NacosServiceManager nacosServiceManager,
			NacosDiscoveryProperties nacosDiscoveryProperties) {
		return new NacosServiceRegistry(nacosServiceManager, nacosDiscoveryProperties);
	}

	@Bean
	@ConditionalOnBean(AutoServiceRegistrationProperties.class)
	public NacosRegistration nacosRegistration(
			ObjectProvider<List<NacosRegistrationCustomizer>> registrationCustomizers,
			NacosDiscoveryProperties nacosDiscoveryProperties,
			ApplicationContext context) {
		return new NacosRegistration(registrationCustomizers.getIfAvailable(),
				nacosDiscoveryProperties, context);
	}

    // 觸發註冊
	@Bean
	@ConditionalOnBean(AutoServiceRegistrationProperties.class)
	public NacosAutoServiceRegistration nacosAutoServiceRegistration(
			NacosServiceRegistry registry,
			AutoServiceRegistrationProperties autoServiceRegistrationProperties,
			NacosRegistration registration) {
		return new NacosAutoServiceRegistration(registry,
				autoServiceRegistrationProperties, registration);
	}

}

這裡只能知道註冊了 3 個 bean,bean 的功能是啥現在也不知道,點進去看一下會發現:NacosAutoServiceRegistration 實現了一個介面 ApplicationListener<WebServerInitializedEvent>。事件監聽也回去看看之前的文章,作用是 spring 容器啟動會回撥 AbstractAutoServiceRegistration#onApplicationEvent() 這個方法,這個方法原始碼呼叫鏈如下

nacos 服務註冊原理

com.alibaba.cloud.nacos.registry.NacosServiceRegistry#register 看下注冊到 nacos 的服務資訊

nacos 服務註冊原理

繼續看下呼叫鏈

nacos 服務註冊原理

相關文章