Spring Cloud-Config扩展原理与自定义

目录

目标

config扩展点

制定了config获取流程,通过PropertySourceLocator bean进行扩展。

spring-cloud-config扩展实现

自定义config扩展

添加配置文件

添加配置

自定义PropertySourceLocator


目标

通过源码简述Spring Cloud提供的config扩展的原理,spring-cloud-config-client如何实现扩展,如何自定义config扩展。

config扩展点

制定了config获取流程,通过PropertySourceLocator bean进行扩展。

spring-cloud-context-xxxx.jar!/META-INF/

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration
@Configuration
@EnableConfigurationProperties(PropertySourceBootstrapProperties.class)
public class PropertySourceBootstrapConfiguration implements
		ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered{}
org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration#initialize

遍历PropertySourceLocator beans

生成的propertySource存储于

org.springframework.core.env.AbstractEnvironment#propertySources

org.springframework.core.env.MutablePropertySources#propertySourceList 写拷贝List

这里列举下该field的使用

env提供获取属性的接口

//解析器
org.springframework.core.env.AbstractEnvironment#propertyResolver

//优先读取list中靠前source的属性
org.springframework.core.env.PropertySourcesPropertyResolver#getProperty 

org.springframework.core.env.AbstractEnvironment#getRequiredProperty

org.springframework.core.env.AbstractEnvironment#getProperty

ConfigurationProperties数据赋值

// 存储,来自env
org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor#propertySources
   
// BeanPostProcessor扩展点
org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor#postProcessBeforeInitialization

// 属性绑定
org.springframework.boot.bind.PropertiesConfigurationFactory#bindPropertiesToTarget

// 绑定
org.springframework.validation.DataBinder#bind

// 验证
org.springframework.validation.DataBinder#validate

 

 

spring-cloud-config扩展实现

spring-cloud-config-client-xxxxx.jar!/META-INF/spring.factories

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration

其中通过@Bean提供了PropertySourceLocator的实现 

    org.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration
	
    // 提供ConfigServicePropertySourceLocator bean
	@Bean
	@ConditionalOnMissingBean(ConfigServicePropertySourceLocator.class)
	@ConditionalOnProperty(value = "spring.cloud.config.enabled", matchIfMissing = true)
	public ConfigServicePropertySourceLocator configServicePropertySource(xx){xxx}

其locate实现, 从configserver获取配置数据。

org.springframework.cloud.config.client.ConfigServicePropertySourceLocator#locate

// 聚合propertySource,其中使用linkedHashSet存储了多个source,可以利用linkedHashSet的sorted特性实现k/v优先级目标
org.springframework.core.env.CompositePropertySource 

// 从map中读取k/v属性,可作为CompositePropertySource内部存储的类型
org.springframework.core.env.MapPropertySource 

// 代表k/v属性对的数据源抽象
org.springframework.core.env.PropertySource 

//提供可枚举k/v属性对的抽象能力
org.springframework.core.env.EnumerablePropertySource 

 

 

自定义config扩展

添加配置文件

resources中添加META-INF/spring.factories

添加配置

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
packagePath.xxxxBootstrapConfiguration
自定义xxxxBootstrapConfiguration,模仿ConfigServiceBootstrapConfiguration

  • 类上@Configuration @EnableConfigurationProperties
  • 定义生成PropertySourceLocator的方法

        @Bean
        @条件

自定义PropertySourceLocator

public class XxxxSourceLocator implements PropertySourceLocator

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值