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 {@Beanpublic 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);}// 自动注册到 nacos 的核心方法@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() 这个方法,这个方法源码调用链如下