概要:
-
客户端通过ConfigServicePropertySourceLocator 拉取配置的
-
服务端是通过EnvironmentRepository 存放配置的
- 客户端加载配置
org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration#initialize
PropertySourceBootstrapConfiguration 类继承了ApplicationContextInitializer ,应用初始化时就会调用initialize 方法
而initialize 方法内的org.springframework.cloud.config.client.ConfigServicePropertySourceLocator#locate 方法会通过resttemplate 请求服务端
用户也可以自定义ConfigServicePropertySourceLocator 来加载配置,见官网文档
public void initialize(ConfigurableApplicationContext applicationContext) {
CompositePropertySource composite = new CompositePropertySource(
BOOTSTRAP_PROPERTY_SOURCE_NAME);
AnnotationAwareOrderComparator.sort(this.propertySourceLocators);
boolean empty = true;
ConfigurableEnvironment environment = applicationContext.getEnvironment();
// 遍历 propertySourceLocators加载配置, 用户也可以自定义PropertySourceLocator
// 见官网链接 https://cloud.spring.io/spring-cloud-static/Hoxton.RELEASE/reference/htmlsingle/#custom-rest-template
for (PropertySourceLocator locator : this.propertySourceLocators) {
PropertySource<?> source = null;
// 加载配置
source = locator.locate(environment);
if (source == null) {
continue;
}
logger.info("Located property source: " + source);
composite.addPropertySource(source);
empty = false;
}
if (!empty) {
MutablePropertySources propertySources = environment.getPropertySources();
String logConfig = environment.resolvePlaceholders("${logging.config:}");
LogFile logFile = LogFile.get(environment);
if (propertySources.contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
propertySources.remove(BOOTSTRAP_PROPERTY_SOURCE_NAME);
}
insertPropertySources(propertySources, composite);
reinitializeLoggingSystem(environment, logConfig, logFile);
setLogLevels(applicationContext, environment);
handleIncludedProfiles(environment);
}
}
- 配置中心服务端,服务端提供配置是通过org.springframework.cloud.config.server.environment.EnvironmentController 类提供http接口
http接口会从EnvironmentRepository拿配置的, 用户要自定义存放配置,只需要实现该接口
public interface EnvironmentRepository {
Environment findOne(String application, String profile, String label);
}
服务端自动配置
@Configuration
@ConditionalOnBean(ConfigServerConfiguration.Marker.class)
@EnableConfigurationProperties(ConfigServerProperties.class)
@Import({ EnvironmentRepositoryConfiguration.class, CompositeConfiguration.class, ResourceRepositoryConfiguration.class,
ConfigServerEncryptionConfiguration.class, ConfigServerMvcConfiguration.class })
public class ConfigServerAutoConfiguration {
}