springcloud 配置中心原理

概要:

  • 客户端通过ConfigServicePropertySourceLocator 拉取配置的

  • 服务端是通过EnvironmentRepository 存放配置的

  1. 客户端加载配置
    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);
		}
	}
  1. 配置中心服务端,服务端提供配置是通过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 {

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值