1. 程式人生 > >Spring Cloud Config原始碼解析(一)

Spring Cloud Config原始碼解析(一)

   這是第一次寫原始碼的解析,主要是給自己留一個印象,有很多不充足的地方以後自己慢慢補充。這個spring cloud config原始碼的解析是以2.1.0.BUILD-SNAPSHOT為基礎,時間是2018-10-05以前的程式碼來分析。spring cloud config主要有3個模組,分別是spring-cloud-config-server、spring-cloud-config-client、spring-cloud-config-monitor。

   首先我們看下spring-cloud-config-server模組,通過該模組的目錄resources/META-INF/spring.factories配置檔案可以看到一些啟動配置如下

# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.config.server.bootstrap.ConfigServerBootstrapConfiguration,\
org.springframework.cloud.config.server.config.EncryptionAutoConfiguration

# Application listeners
org.springframework.context.ApplicationListener=\
org.springframework.cloud.config.server.bootstrap.ConfigServerBootstrapApplicationListener

# Autoconfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.config.server.config.ConfigServerAutoConfiguration,\
org.springframework.cloud.config.server.config.EncryptionAutoConfiguration

  我們首先看下org.springframework.boot.autoconfigure.EnableAutoConfiguration路徑下的ConfigServerAutoConfiguration配置類,它用@Import來載入配置其他相關的配置類。

@Configuration
@ConditionalOnBean(ConfigServerConfiguration.Marker.class)
@EnableConfigurationProperties(ConfigServerProperties.class)
@Import({ EnvironmentRepositoryConfiguration.class, CompositeConfiguration.class, ResourceRepositoryConfiguration.class,
      ConfigServerEncryptionConfiguration.class, ConfigServerMvcConfiguration.class })
public class ConfigServerAutoConfiguration {

}

  首先來看一下EnvironmentRepositoryConfiguration這個類的實現

@Configuration
@EnableConfigurationProperties({ SvnKitEnvironmentProperties.class,
      JdbcEnvironmentProperties.class, NativeEnvironmentProperties.class, VaultEnvironmentProperties.class })
@Import({ CompositeRepositoryConfiguration.class, JdbcRepositoryConfiguration.class, VaultRepositoryConfiguration.class,
      SvnRepositoryConfiguration.class, NativeRepositoryConfiguration.class, GitRepositoryConfiguration.class,
      DefaultRepositoryConfiguration.class })
public class EnvironmentRepositoryConfiguration {
**
}

1.首先通過@Configuration註解說明它是一個配置檔案屬性,用於注入Spring中;

2.通過@EnableConfigurationProperties註解引入了properties檔案,分別是

SvnKitEnvironmentProperties(spring.cloud.config.server.svn)、

JdbcEnvironmentProperties(spring.cloud.config.server.jdbc)、

NativeEnvironmentProperties(spring.cloud.config.server.native)、

VaultEnvironmentProperties(spring.cloud.config.server.vault),通過檢視具體的properties檔案我們可以看到它們對應的是其名字相應的perfix屬性配置在*.yml或*.properties中的配置,但是在這裡可以看到明顯少了JGitEnvironmentProperties

配置檔案的引入,這裡沒有關係,我們在後面可以看到在GitRepositoryConfiguration類中通過引數形式傳入了;

3.通過@Import註解引入CompositeRepositoryConfiguration、JdbcRepositoryConfiguration、VaultRepositoryConfiguration、SvnRepositoryConfiguration、NativeRepositoryConfiguration、GitRepositoryConfiguration、DefaultRepositoryConfiguration

相關的倉庫配置類為預設bean,這些bean就是我們常用的配置公共引入屬性的來源地方;

4.在類中同時新增了以下bean:

1).ConfigServerHealthIndicator configServerHealthIndicator()健康端點

@Bean
@ConditionalOnProperty(value = "spring.cloud.config.server.health.enabled", matchIfMissing = true)
public ConfigServerHealthIndicator configServerHealthIndicator(
      EnvironmentRepository repository) {
   return new ConfigServerHealthIndicator(repository);
}

2).MultipleJGitEnvironmentProperties multipleJGitEnvironmentProperties()無引數的git bean

@Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public MultipleJGitEnvironmentProperties multipleJGitEnvironmentProperties() {
   return new MultipleJGitEnvironmentProperties();
}

3).EnvironmentWatch environmentWatch()的consule watch的prefix為spring.cloud.config.server.consul.watch.enabled的是否啟動的 bean

@Configuration
@ConditionalOnProperty(value = "spring.cloud.config.server.consul.watch.enabled")
protected static class ConsulEnvironmentWatchConfiguration {
   @Bean
   public EnvironmentWatch environmentWatch() {
      return new ConsulEnvironmentWatch();
   }
}

4).未生成EnvironmentWatch environmentWatch()的consule watch 的情況下生成bean

@Configuration
@ConditionalOnMissingBean(EnvironmentWatch.class)
protected static class DefaultEnvironmentWatch {
   @Bean
   public EnvironmentWatch environmentWatch() {
      return new EnvironmentWatch.Default();
   }
}

5).有回撥類TransportConfigCallback的情況下的MultipleJGitEnvironmentRepositoryFactory gitEnvironmentRepositoryFactory()的帶參bean

@Configuration
@ConditionalOnClass(TransportConfigCallback.class)
static class JGitFactoryConfig {

    @Bean
    public MultipleJGitEnvironmentRepositoryFactory gitEnvironmentRepositoryFactory(
            ConfigurableEnvironment environment, ConfigServerProperties server,
            Optional<ConfigurableHttpConnectionFactory> jgitHttpConnectionFactory,
            Optional<TransportConfigCallback> customTransportConfigCallback) {
        return new MultipleJGitEnvironmentRepositoryFactory(environment, server, jgitHttpConnectionFactory,
   customTransportConfigCallback);
    }
}

6).有回撥類TransportConfigCallback和客戶端類HttpClient的情況下的ConfigurableHttpConnectionFactory httpClientConnectionFactory()的bean

@Configuration
@ConditionalOnClass({ HttpClient.class, TransportConfigCallback.class })
static class JGitHttpClientConfig {
    @Bean
    public ConfigurableHttpConnectionFactory httpClientConnectionFactory() {
        return new HttpClientConfigurableHttpConnectionFactory();
    }
}

7).有異常類SVNException情況下的SvnEnvironmentRepositoryFactory svnEnvironmentRepositoryFactory() 的bean

@Configuration
@ConditionalOnClass(SVNException.class)
static class SvnFactoryConfig {
    @Bean
    public SvnEnvironmentRepositoryFactory svnEnvironmentRepositoryFactory(ConfigurableEnvironment environment,
                                                                           ConfigServerProperties server) {
        return new SvnEnvironmentRepositoryFactory(environment, server);
    }
}

8).帶參的VaultEnvironmentRepositoryFactory vaultEnvironmentRepositoryFactory()的bean

@Configuration
static class VaultFactoryConfig {
    @Bean
    public VaultEnvironmentRepositoryFactory vaultEnvironmentRepositoryFactory(
            ObjectProvider<HttpServletRequest> request, EnvironmentWatch watch,
            Optional<VaultEnvironmentRepositoryFactory.VaultRestTemplateFactory> vaultRestTemplateFactory) {
        return new VaultEnvironmentRepositoryFactory(request, watch, vaultRestTemplateFactory);
    }
}

9).有客戶端類HttpClient的情況下的VaultEnvironmentRepositoryFactory.VaultRestTemplateFactory vaultRestTemplateFactory()的bean

@Configuration
@ConditionalOnClass(HttpClient.class)
static class VaultHttpClientConfig {
    @Bean
    public VaultEnvironmentRepositoryFactory.VaultRestTemplateFactory vaultRestTemplateFactory() {
        return new HttpClientVaultRestTemplateFactory();
    }
}

10).有資料庫模板類JdbcTemplat情況下的JdbcEnvironmentRepositoryFactory jdbcEnvironmentRepositoryFactory()帶參bean

@Configuration
@ConditionalOnClass(JdbcTemplate.class)
static class JdbcFactoryConfig {
 @Bean
    @ConditionalOnBean(JdbcTemplate.class)
    public JdbcEnvironmentRepositoryFactory jdbcEnvironmentRepositoryFactory(JdbcTemplate jdbc) {
        return new JdbcEnvironmentRepositoryFactory(jdbc);
    }
}

11).NativeEnvironmentRepositoryFactory nativeEnvironmentRepositoryFactory()帶參bean

@Configuration
static class NativeFactoryConfig {
    @Bean
    public NativeEnvironmentRepositoryFactory nativeEnvironmentRepositoryFactory(ConfigurableEnvironment environment,
                                                                                 ConfigServerProperties properties) {
        return new NativeEnvironmentRepositoryFactory(environment, properties);
    }
}

12).定義在不存在bean為EnvironmentRepository時擁有引數MultipleJGitEnvironmentRepositoryFactory和MultipleJGitEnvironmentProperties

的MultipleJGitEnvironmentRepository defaultEnvironmentRepository()帶參bean,其中MultipleJGitEnvironmentProperties定義了prefix為spring.cloud.config.server.git

的git配置

@Configuration
@ConditionalOnMissingBean(value = EnvironmentRepository.class)//, search = SearchStrategy.CURRENT)
class DefaultRepositoryConfiguration {
   @Autowired
   private ConfigurableEnvironment environment;

   @Autowired
   private ConfigServerProperties server;

   @Autowired(required = false)
   private TransportConfigCallback customTransportConfigCallback;

   @Bean
   public MultipleJGitEnvironmentRepository defaultEnvironmentRepository(
           MultipleJGitEnvironmentRepositoryFactory gitEnvironmentRepositoryFactory,
         MultipleJGitEnvironmentProperties environmentProperties) throws Exception {
      return gitEnvironmentRepositoryFactory.build(environmentProperties);
   }
}

 13).定義在不存在bean為EnvironmentRepository且當前資料來源配置環境為native時,擁有引數NativeEnvironmentRepositoryFactory和NativeEnvironmentProperties

的NativeEnvironmentRepository nativeEnvironmentRepository()帶參的bean

@Configuration
@ConditionalOnMissingBean(EnvironmentRepository.class)
@Profile("native")
class NativeRepositoryConfiguration {
   @Bean
   public NativeEnvironmentRepository nativeEnvironmentRepository(NativeEnvironmentRepositoryFactory factory,
         NativeEnvironmentProperties environmentProperties) {
        return factory.build(environmentProperties);
   }
}

 14).資料來源配置環境為git的MultipleJGitEnvironmentRepository bean

@Configuration
@Profile("git")
class GitRepositoryConfiguration extends DefaultRepositoryConfiguration {
}

15).資料來源配置環境為subversion擁有引數SvnKitEnvironmentProperties和SvnEnvironmentRepositoryFactory的SvnKitEnvironmentRepository svnKitEnvironmentRepository()帶參bean

@Configuration
@Profile("subversion")
class SvnRepositoryConfiguration {
   @Bean
   public SvnKitEnvironmentRepository svnKitEnvironmentRepository(SvnKitEnvironmentProperties environmentProperties,
                                                                   SvnEnvironmentRepositoryFactory factory) {
      return factory.build(environmentProperties);
   }
}

  16).資料來源配置環境為vault擁有引數VaultEnvironmentRepositoryFactory

和VaultEnvironmentProperties的VaultEnvironmentRepository vaultEnvironmentRepository帶參bean

@Configuration
@Profile("vault")
class VaultRepositoryConfiguration {
   @Bean
   public VaultEnvironmentRepository vaultEnvironmentRepository(VaultEnvironmentRepositoryFactory factory,
                                                                 VaultEnvironmentProperties environmentProperties)
            throws Exception {
      return factory.build(environmentProperties);
   }
}

  17).有資料庫模板類JdbcTemplate且資料來源配置環境為jdbc,擁有引數JdbcEnvironmentRepositoryFactory

和JdbcEnvironmentProperties的JdbcEnvironmentRepository jdbcEnvironmentRepository()帶參bean

@Configuration
@Profile("jdbc")
@ConditionalOnClass(JdbcTemplate.class)
class JdbcRepositoryConfiguration {

   @Bean
   @ConditionalOnBean(JdbcTemplate.class)
   public JdbcEnvironmentRepository jdbcEnvironmentRepository(JdbcEnvironmentRepositoryFactory factory,
                                                JdbcEnvironmentProperties environmentProperties) {
      return factory.build(environmentProperties);
   }
}

  18).資料來源配置環境為composite擁有引數Environment的CompositeEnvironmentBeanFactoryPostProcessor compositeEnvironmentRepositoryBeanFactoryPostProcessor()帶參bean

@Bean
public static CompositeEnvironmentBeanFactoryPostProcessor compositeEnvironmentRepositoryBeanFactoryPostProcessor(
      Environment environment) {
   return new CompositeEnvironmentBeanFactoryPostProcessor(environment);
}

  19).資料來源配置環境為composite本地多環境倉庫下優先使用SearchPathCompositeEnvironmentRepository searchPathCompositeEnvironmentRepository()的bean

@Primary
@Bean
@ConditionalOnSearchPathLocator
public SearchPathCompositeEnvironmentRepository searchPathCompositeEnvironmentRepository(
      List<EnvironmentRepository> environmentRepositories) throws Exception {
   return new SearchPathCompositeEnvironmentRepository(environmentRepositories);
}

  20).資料來源配置環境為composite非本地多環境下優先使用CompositeEnvironmentRepository compositeEnvironmentRepository()的bean

@Primary
@Bean
@ConditionalOnMissingSearchPathLocator
public CompositeEnvironmentRepository compositeEnvironmentRepository(
      List<EnvironmentRepository> environmentRepositories) throws Exception {
   return new CompositeEnvironmentRepository(environmentRepositories);
}

 在下一篇我們主要了解下git相關的實現程式碼的相關實現。