SpringCloud starter-consul-discovery自动注册

一、概述

springcloud引入consul的依赖后,做了些什么呢,这里简单跟踪下。

二、怎么找到入口

1 引入依赖


  org.springframework.cloud
  spring-cloud-starter-consul-discovery

2 查看spring.factories文件

springboot的使用了类似java SPI的方式引入依赖,我们引入了上面的maven依赖后,进入jar包,查看spring.factories文件,这个文件定义了consul启动时候加载的配置及初始化方法。

  • 文件位置


    SpringCloud starter-consul-discovery自动注册_第1张图片
    image.png
  • 文件内容
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.consul.discovery.RibbonConsulAutoConfiguration,\
org.springframework.cloud.consul.discovery.configclient.ConsulConfigServerAutoConfiguration,\
org.springframework.cloud.consul.serviceregistry.ConsulAutoServiceRegistrationAutoConfiguration,\
org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistryAutoConfiguration,\
org.springframework.cloud.consul.discovery.ConsulDiscoveryClientConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.consul.discovery.configclient.ConsulDiscoveryClientConfigServiceBootstrapConfiguration

其中 org.springframework.cloud.bootstrap.BootstrapConfiguration下面的配置等同于bootstrap.yml(或者bootstrap.properties),优先于application.yml(或者application.properties)
org.springframework.boot.autoconfigure.EnableAutoConfiguration下面的配置等同于普通的config
这里的配置大部分都是加载application.yml中的consul相关的配置,并实例化成bean,这里不做过多解释,返回本文的目的:系统启动后,这个starter怎么向consul进行注册!

3 向consul发起注册请求

直入主题,我们看下spring.factoties中的 org.springframework.cloud.consul.serviceregistry.ConsulAutoServiceRegistrationAutoConfiguration,import啥的都省略了,注释直接写到源码里了

  • ConsulAutoServiceRegistrationAutoConfiguration.java
@Configuration
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
@ConditionalOnMissingBean(type = "org.springframework.cloud.consul.discovery.ConsulLifecycle")
@ConditionalOnConsulEnabled
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
// 在注册这个bean之前,需要下面两个配置文件预先加载,主要是装载了consul的配置
@AutoConfigureAfter({ AutoServiceRegistrationConfiguration.class,
        ConsulServiceRegistryAutoConfiguration.class })
public class ConsulAutoServiceRegistrationAutoConfiguration {

    @Autowired
    AutoServiceRegistrationProperties autoServiceRegistrationProperties;
    // 将加载的配置汇总成一个bean :ConsulAutoServiceRegistration
    @Bean
    @ConditionalOnMissingBean
    public ConsulAutoServiceRegistration consulAutoServiceRegistration(
            ConsulServiceRegistry registry,
            AutoServiceRegistrationProperties autoServiceRegistrationProperties,
            ConsulDiscoveryProperties properties,
            ConsulAutoRegistration consulRegistration) {
        return new ConsulAutoServiceRegistration(registry,
                autoServiceRegistrationProperties, properties, consulRegistration);
    }
/*
  这里是向consul发起注册请求的入口!!!!
  ConsulAutoServiceRegistrationListener继承了SmartApplicationListener
  会在系统启动后执行
*/
    @Bean
    public ConsulAutoServiceRegistrationListener consulAutoServiceRegistrationListener(
            ConsulAutoServiceRegistration registration) {
        return new ConsulAutoServiceRegistrationListener(registration);
    }

    @Bean
    @ConditionalOnMissingBean
    public ConsulAutoRegistration consulRegistration(
            AutoServiceRegistrationProperties autoServiceRegistrationProperties,
            ConsulDiscoveryProperties properties, ApplicationContext applicationContext,
            ObjectProvider> registrationCustomizers,
            ObjectProvider> managementRegistrationCustomizers,
            HeartbeatProperties heartbeatProperties) {
        return ConsulAutoRegistration.registration(autoServiceRegistrationProperties,
                properties, applicationContext, registrationCustomizers.getIfAvailable(),
                managementRegistrationCustomizers.getIfAvailable(), heartbeatProperties);
    }

    @Configuration
    @ConditionalOnClass(ServletContext.class)
    protected static class ConsulServletConfiguration {

        @Bean
        public ConsulRegistrationCustomizer servletConsulCustomizer(
                ObjectProvider servletContext) {
            return new ConsulServletRegistrationCustomizer(servletContext);
        }

    }

}

  • UML图例
    SpringCloud starter-consul-discovery自动注册_第2张图片
    注册流程

你可能感兴趣的:(SpringCloud starter-consul-discovery自动注册)