spring-cloud-starter-alibaba-nacos-discovery
查看源码可知,自动将配置以下配置都加载到当前SpringBoot创建并使用的IoC容器
将各个bean注入到容器中,以便于后期完成注册
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
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
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration
com.alibaba.cloud.nacos.discovery.NacosDiscoveryAutoConfiguration
/**
* 配置nacos服务发现的自动化配置
* 注入NacosDiscoveryProperties、NacosServiceDiscovery
* @author <a href="mailto:echooy.mxq@gmail.com">echooymxq</a>
**/
@Configuration(proxyBeanMethods = false)
@ConditionalOnDiscoveryEnabled
@ConditionalOnNacosDiscoveryEnabled
public class NacosDiscoveryAutoConfiguration {
//注入对应的配置项,处理配置项null的数据为"",判断并设置ip,并从环境中覆盖nacos发现配置
//如果是nacos服务信息发生变化,则发布事件NacosDiscoveryInfoChangedEvent
@Bean
@ConditionalOnMissingBean
public NacosDiscoveryProperties nacosProperties() {
return new NacosDiscoveryProperties();
}
//注入服务发现类
@Bean
@ConditionalOnMissingBean
public NacosServiceDiscovery nacosServiceDiscovery(
NacosDiscoveryProperties discoveryProperties,
NacosServiceManager nacosServiceManager) {
return new NacosServiceDiscovery(discoveryProperties, nacosServiceManager);
}
}
将NacosDiscoveryProperties、NacosServiceDiscovery注入到容器中,其中nacosServiceDiscovery需要先自动配置NacosServiceAutoConfiguration
注入NacosServiceManager
再运行
com.alibaba.cloud.nacos.NacosServiceAutoConfiguration
@Configuration(proxyBeanMethods = false)
@ConditionalOnDiscoveryEnabled
@ConditionalOnNacosDiscoveryEnabled
public class NacosServiceAutoConfiguration {
//注入nacos服务管理,并设置监听事件InstancePreRegisteredEvent,监听服务注册之前的事件发布
@Bean
public NacosServiceManager nacosServiceManager() {
return new NacosServiceManager();
}
}
注入NacosServiceManager
com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration
@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 {
//注入nacos服务注册中心
@Bean
public NacosServiceRegistry nacosServiceRegistry(
NacosDiscoveryProperties nacosDiscoveryProperties) {
return new NacosServiceRegistry(nacosDiscoveryProperties);
}
//注入nacos注册配置,初始化元数据,并且提供自定义元数据的修改方式
@Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
public NacosRegistration nacosRegistration(
ObjectProvider<List<NacosRegistrationCustomizer>> registrationCustomizers,
NacosDiscoveryProperties nacosDiscoveryProperties,
ApplicationContext context) {
return new NacosRegistration(registrationCustomizers.getIfAvailable(),
nacosDiscoveryProperties, context);
}
//注入nacos自动服务注册类,并监听事件NacosDiscoveryInfoChangedEvent,该事件在NacosDiscoveryProperties中进行的发布配置
@Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
public NacosAutoServiceRegistration nacosAutoServiceRegistration(
NacosServiceRegistry registry,
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
NacosRegistration registration) {
return new NacosAutoServiceRegistration(registry,
autoServiceRegistrationProperties, registration);
}
}
com.alibaba.cloud.nacos.discovery.reactive.NacosReactiveDiscoveryClientConfiguration
/**
* Nacos反应式发现客户端配置
**/
@Configuration(proxyBeanMethods = false)
@ConditionalOnDiscoveryEnabled
@ConditionalOnReactiveDiscoveryEnabled
@ConditionalOnNacosDiscoveryEnabled
@AutoConfigureAfter({ NacosDiscoveryAutoConfiguration.class,
ReactiveCompositeDiscoveryClientAutoConfiguration.class })
@AutoConfigureBefore({ ReactiveCommonsClientAutoConfiguration.class })
public class NacosReactiveDiscoveryClientConfiguration {
//注入nacos反应式服务发现客户端
@Bean
@ConditionalOnMissingBean
public NacosReactiveDiscoveryClient nacosReactiveDiscoveryClient(
NacosServiceDiscovery nacosServiceDiscovery) {
return new NacosReactiveDiscoveryClient(nacosServiceDiscovery);
}
}
com.alibaba.cloud.nacos.ribbon.RibbonNacosAutoConfiguration
配置ribbon客户端
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnBean(SpringClientFactory.class)
@ConditionalOnRibbonNacos
@ConditionalOnNacosDiscoveryEnabled
@AutoConfigureAfter(RibbonAutoConfiguration.class)
@RibbonClients(defaultConfiguration = NacosRibbonClientConfiguration.class)
public class RibbonNacosAutoConfiguration {
}
------------------------------
@Configuration(proxyBeanMethods = false)
@ConditionalOnRibbonNacos
public class NacosRibbonClientConfiguration {
@Autowired
private PropertiesFactory propertiesFactory;
//注入服务列表
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config,
NacosDiscoveryProperties nacosDiscoveryProperties) {
if (this.propertiesFactory.isSet(ServerList.class, config.getClientName())) {
ServerList serverList = this.propertiesFactory.get(ServerList.class, config,
config.getClientName());
return serverList;
}
NacosServerList serverList = new NacosServerList(nacosDiscoveryProperties);
serverList.initWithNiwsConfig(config);
return serverList;
}
//注入Nacos服务器自检器
@Bean
@ConditionalOnMissingBean
public NacosServerIntrospector nacosServerIntrospector() {
return new NacosServerIntrospector();
}
}
由于ribbonNacos需要在RibbonAutoConfiguration
配置完成之后才进行自动配置,而RibbonAutoConfiguration
又需要在org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration
完成配置之后才会进行自动配置,默认不带这eureka相关jar包,所以,在默认情况下,不会进行ribbon
com.alibaba.cloud.nacos.endpoint.NacosDiscoveryEndpointAutoConfiguration
/**
* The AutoConfiguration class for Nacos Discovery's Endpoints.
* 暴露nacos服务发现的端点,包括NacosDiscoveryProperties和subscribe
* @author xiaojing
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Endpoint.class)
@ConditionalOnNacosDiscoveryEnabled
public class NacosDiscoveryEndpointAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnAvailableEndpoint
public NacosDiscoveryEndpoint nacosDiscoveryEndpoint(
NacosServiceManager nacosServiceManager,
NacosDiscoveryProperties nacosDiscoveryProperties) {
return new NacosDiscoveryEndpoint(nacosServiceManager, nacosDiscoveryProperties);
}
@Bean
@ConditionalOnEnabledHealthIndicator("nacos-discovery")
public HealthIndicator nacosDiscoveryHealthIndicator(
NacosServiceManager nacosServiceManager,
NacosDiscoveryProperties nacosDiscoveryProperties) {
Properties nacosProperties = nacosDiscoveryProperties.getNacosProperties();
return new NacosDiscoveryHealthIndicator(
nacosServiceManager.getNamingService(nacosProperties));
}
}
其中NacosDiscoveryEndpoint
通过Endpoint
和ReadOperation
两个注解将该方法通过JMX自动公开,并且在web应用程序中也通过http get方式公开,通过actuator
来暴露对应的端点
com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration
@Configuration(proxyBeanMethods = false)
@ConditionalOnDiscoveryEnabled
@ConditionalOnBlockingDiscoveryEnabled
@ConditionalOnNacosDiscoveryEnabled
@AutoConfigureBefore({ SimpleDiscoveryClientAutoConfiguration.class,
CommonsClientAutoConfiguration.class })
@AutoConfigureAfter(NacosDiscoveryAutoConfiguration.class)
public class NacosDiscoveryClientConfiguration {
//注入nacos服务发现客户端
@Bean
public DiscoveryClient nacosDiscoveryClient(
NacosServiceDiscovery nacosServiceDiscovery) {
return new NacosDiscoveryClient(nacosServiceDiscovery);
}
//输入nacoswatch,该类在ApplicationContext启动后扫描到lifeCycle的实现类并调用start,订阅EventListener,如果是NamingEvent,则在需要的情况下重置元数据,另外,此类还会发布一次心跳HeartbeatEvent
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.nacos.discovery.watch.enabled",
matchIfMissing = true)
public NacosWatch nacosWatch(NacosServiceManager nacosServiceManager,
NacosDiscoveryProperties nacosDiscoveryProperties,
ObjectProvider<TaskScheduler> taskExecutorObjectProvider) {
return new NacosWatch(nacosServiceManager, nacosDiscoveryProperties,
taskExecutorObjectProvider);
}
}
com.alibaba.cloud.nacos.discovery.configclient.NacosConfigServerAutoConfiguration
/**
* Extra configuration for config server if it happens to be registered with Nacos.
* 如果配置服务器恰巧已在Nacos中注册,则需要对配置服务器进行额外的配置
*
* @author JevonYang
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnClass({NacosDiscoveryProperties.class, ConfigServerProperties.class})
public class NacosConfigServerAutoConfiguration {
@Autowired(required = false)
private NacosDiscoveryProperties properties;
@Autowired(required = false)
private ConfigServerProperties server;
@PostConstruct
public void init() {
if (this.properties == null || this.server == null) {
return;
}
String prefix = this.server.getPrefix();
if (StringUtils.hasText(prefix) && !StringUtils
.hasText(this.properties.getMetadata().get("configPath"))) {
this.properties.getMetadata().put("configPath", prefix);
}
}
}
com.alibaba.cloud.nacos.discovery.configclient.NacosDiscoveryClientConfigServiceBootstrapConfiguration
@ConditionalOnClass(ConfigServicePropertySourceLocator.class)
@ConditionalOnProperty(value = "spring.cloud.config.discovery.enabled",
matchIfMissing = false)
@Configuration(proxyBeanMethods = false)
@ImportAutoConfiguration({ NacosDiscoveryAutoConfiguration.class,
NacosDiscoveryClientConfiguration.class,
NacosReactiveDiscoveryClientConfiguration.class })
public class NacosDiscoveryClientConfigServiceBootstrapConfiguration {
}
将NacosDiscoveryAutoConfiguration.class,NacosDiscoveryClientConfiguration.class,NacosReactiveDiscoveryClientConfiguration.class
注入到Bootstrap
容器中
网友评论