Spring Cloud配置中心
- Environment 抽象
- 基于git实现配置中心
Spring Cloud配置中心
版本信息
Spring Cloud : Hoxton.SR1
Spring Boot : 2.2.2.RELEASE
Environment 抽象
- org.springframework.core.env.Environment关注于读取profile- org.springframework.core.env.PropertyResolver关注于读取配置
 
- org.springframework.core.env.ConfigurableEnvironment关注于存储profile- org.springframework.core.env.ConfigurablePropertyResolver关注于类型转换
 
默认实现:
org.springframework.core.env.StandardEnvironment
配置资源
- org.springframework.core.env.PropertySource单个读资源的抽象,对应注解- @org.springframework.context.annotation.PropertySource
- org.springframework.core.env.PropertySources多个读资源的抽象,对应注解- @org.springframework.context.annotation.PropertySource
- org.springframework.core.env.MutablePropertySources关注于存储资源
默认实现:
org.springframework.core.env.CompositePropertySource
Spring Boot 外部化配置
Devtools global settings properties on your home directory (~/.spring-bootdevtools.
properties when devtools is active).
@TestPropertySource annotations on your tests.
properties attribute on your tests. Available on @SpringBootTest and the test annotations for
testing a particular slice of your application.
Command line arguments.
commandLineArgs–>org.springframework.core.env.CommandLinePropertySource
Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable
or system property).
spring.application.json–>org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor.JsonPropertySource
ServletConfig init parameters.
servletConfigInitParams–>org.springframework.core.env.PropertySource.StubPropertySource占位–>
org.springframework.web.context.support.ServletConfigPropertySource
ServletContext init parameters.
servletContextInitParams–>org.springframework.core.env.PropertySource.StubPropertySource占位–>
org.springframework.web.context.support.ServletContextPropertySource
JNDI attributes from java:comp/env.
jndiProperties–>org.springframework.jndi.JndiPropertySource
Java System properties (System.getProperties()).
systemProperties–>org.springframework.core.env.PropertiesPropertySource
- OS environment variables.
systemEnvironment–>org.springframework.core.env.SystemEnvironmentPropertySource
- A RandomValuePropertySource that has properties only in random.*.
random–>org.springframework.boot.env.RandomValuePropertySource
Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
Application properties outside of your packaged jar (application.properties and YAML variants).
Application properties packaged inside your jar (application.properties and YAML variants).
12-15类型
applicationConfig: [${location"]–>org.springframework.boot.env.OriginTrackedMapPropertySource
@PropertySource annotations on your @Configuration classes.
Default properties (specified by setting SpringApplication.setDefaultProperties).
类型转换
- org.springframework.core.convert.ConversionService类型转换服务
- org.springframework.core.convert.converter.Converter类型转换接口
- org.springframework.core.convert.converter.GenericConverter普通的类型转换接口
- org.springframework.core.convert.converter.ConditionalConverter适配(条件)的类型转换
- org.springframework.core.convert.converter.ConverterRegistry类型转换的注册中心
- org.springframework.core.convert.converter.ConverterFactory类型转的注册工厂
- org.springframework.core.convert.support.ConfigurableConversionService可配置的类型转换服务
默认实现:
org.springframework.core.convert.support.DefaultConversionService
配置文件加载
- org.springframework.boot.env.PropertySourceLoader配置资源加载- org.springframework.boot.env.PropertiesPropertySourceLoaderproperties(xml)配置文件加载
- org.springframework.boot.env.YamlPropertySourceLoaderyml(yaml)配置文件加载
 
- org.springframework.cloud.bootstrap.config.PropertySourceLocatorSpring Cloud中配置资源加载- org.springframework.cloud.config.client.ConfigServicePropertySourceLocator配置服务资源实现
- org.springframework.cloud.config.server.environment.EnvironmentRepositoryPropertySourceLocator环境仓库配置服务资源实现
 
Environment与PropertySource的关系
Spring
- ``org.springframework.core.env.Environment`- org.springframework.core.env.ConfigurableEnvironment- org.springframework.core.env.MutablePropertySources- org.springframework.core.env.PropertySource
 
 
 
Spring Cloud
- org.springframework.cloud.config.environment.Environment- org.springframework.cloud.config.environment.PropertySource
 
基于git实现配置中心
服务端
应用
- 创建git的配置目录文件,以E:/GitHub/properties文件夹为例,使用的是git客户端 - 1 
 2- cd /e/GitHub/properties
 git init
- 创建配置文件configserver.yml以及configserver-dev.yml,配置文件内容如下 - 1 
 2
 3
 4
 5
 6
 7
 8- management:
 endpoints:
 web:
 exposure:
 include: health,info,beans,env
 spring:
 application:
 name: application-name- 配置文件命名规则: - {application}/{profile}[/{label}] 
 {application}-{profile}.yml
 {label}/{application}-{profile}.yml
 {application}-{profile}.properties
 {label}/{application}-{profile}.properties- {application} 为配置文件名称,对应spring.cloud.config.name
- {profile} 为配置文件激活环境,对应spring.cloud.config.profile,没有默认为default
- {label} 为配置文件标签(git中为分支版本),对应spring.cloud.config.label
 
- {application} 为配置文件名称,对应
- 添加pom的依赖 - 1 
 2
 3
 4- <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-config-server</artifactId>
 </dependency>
- 启动类添加 - @EnableConfigServer- 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14- /**
 * 配置服务启动类
 *
 * @author FelixFly <chenglinxu@yeah.net>
 * @date 2020/1/29
 */
 @SpringBootApplication
 @EnableConfigServer
 public class SpringCloudConfigServerApplication {
 public static void main(String[] args) {
 SpringApplication.run(SpringCloudConfigServerApplication.class, args);
 }
 }
- 配置文件bootstrap.yml - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13- management:
 endpoints:
 web:
 exposure:
 include: health,info,beans,env
 server:
 port: 8090
 spring:
 cloud:
 config:
 server:
 git:
 uri: file:///E:/GitHub/properties
- 启动 - SpringCloudConfigServerApplication,验证配置文件访问路径为- http://127.0.0.1:8090/{application}/{profile},若是有{label},后面再加上/{label}- {label}若是存在”/“,使用“(_)”进行替换,实现类 - org.springframework.cloud.config.server.environment.EnvironmentController- 示例访问地址: - http://127.0.0.1:8090/configserver/default- 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17- {
 "name": "configserver",
 "profiles": [
 "default"
 ],
 "label": null,
 "version": "f6cb3fcb36ae0ea085d0342ebe5e68811b9098a5",
 "state": null,
 "propertySources": [{
 "name": "file:///E:/GitHub/properties/configserver.yml",
 "source": {
 "management.endpoints.web.exposure.include": "health,info,beans,env",
 "spring.application.name": "application-name"
 }
 }
 ]
 }
- 示例访问地址: - http://127.0.0.1:8090/configserver/dev- 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23- {
 "name": "configserver",
 "profiles": [
 "dev"
 ],
 "label": null,
 "version": "f6cb3fcb36ae0ea085d0342ebe5e68811b9098a5",
 "state": null,
 "propertySources": [{
 "name": "file:///E:/GitHub/properties/configserver-dev.yml",
 "source": {
 "management.endpoints.web.exposure.include": "health,info,beans,env",
 "spring.application.name": "application-name"
 }
 }, {
 "name": "file:///E:/GitHub/properties/configserver.yml",
 "source": {
 "management.endpoints.web.exposure.include": "health,info,beans,env",
 "spring.application.name": "application-name"
 }
 }
 ]
 }
 
源码分析
- @EnableConfigServer注解- 1 
 2
 3
 4
 5
 6
 7- @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Import(ConfigServerConfiguration.class)
 public @interface EnableConfigServer {
 }- 就是导入了 - ConfigServerConfiguration的配置Bean- 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12- @Configuration(proxyBeanMethods = false)
 public class ConfigServerConfiguration {
 @Bean
 public Marker enableConfigServerMarker() {
 return new Marker();
 }
 
 class Marker {
 }
 }- 这个 - ConfigServerConfiguration配置Bean就配置了一个Marker的Bean,猜想这个Marker的Bean是不是某个配置的自动装配条件
- ConfigServerAutoConfiguration服务配置自动装配- 1 
 2
 3
 4
 5
 6
 7
 8
 9- @Configuration(proxyBeanMethods = false)
 @ConditionalOnBean(ConfigServerConfiguration.Marker.class)
 @EnableConfigurationProperties(ConfigServerProperties.class)
 @Import({ EnvironmentRepositoryConfiguration.class, CompositeConfiguration.class,
 ResourceRepositoryConfiguration.class, ConfigServerEncryptionConfiguration.class,
 ConfigServerMvcConfiguration.class, ResourceEncryptorConfiguration.class })
 public class ConfigServerAutoConfiguration {
 }- 这个 - ConfigServerAutoConfiguration自动装配条件是- ConfigServerConfiguration.Marker.class这个类型的Bean存在- 启用了ConfigServerProperties配置信息
- 导入了一些配置信息,如EnvironmentRepositoryConfiguration
 
- 启用了
- 默认启用git的实现代码 
| 1 |  | 
   DefaultRepositoryConfiguration在不存在EnvironmentRepository.class类型的Bean下自动装配,MultipleJGitEnvironmentRepository–>JGitEnvironmentRepository git仓储的实现
- 自定义环境仓储的实现 - 从上面可得知,默认的git环境仓储实现只有在不存在 - EnvironmentRepository.class类型的Bean下自动装配,我们只需要自定义一个- EnvironmentRepository的Bean就行了- 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12- @Bean
 public EnvironmentRepository defaultEnvironmentRepository(){
 return (application, profile, label) -> {
 Environment environment = new Environment("custom-default");
 List<PropertySource> propertySources = environment.getPropertySources();
 Map<String, String> sourceMap = new HashMap<>();
 sourceMap.put("application", "custom-application");
 PropertySource propertySource = new PropertySource("map", sourceMap);
 propertySources.add(propertySource);
 return environment;
 };
 }- 访问地址: - http://127.0.0.1:8090/{application}/{profile},若是有{label},后面再加上/{label}- 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16- {
 "name": "config",// {application}
 "profiles": [
 "default" // {profile}
 ],
 "label": null, // {label}
 "version": null,
 "state": null,
 "propertySources": [{
 "name": "map",
 "source": {
 "application": "custom-application"
 }
 }
 ]
 }
客户端
应用
- 添加pom的依赖 - 1 
 2
 3
 4- <dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-config</artifactId>
 </dependency>
- 配置文件bootstrap.yml - 1 
 2
 3
 4
 5
 6
 7- spring:
 cloud:
 config:
 uri: http://127.0.0.1:8090
 name: configserver
 application:
 name: config-client
- 服务启动类 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13- /**
 * 配置客户端启动类
 *
 * @author FelixFly <chenglinxu@yeah.net>
 * @date 2020/1/29
 */
 @SpringBootApplication
 public class SpringCloudConfigClientApplication {
 public static void main(String[] args) {
 SpringApplication.run(SpringCloudConfigClientApplication.class, args);
 }
 }
- 启动服务,查看/actuator/env - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32- {
 "activeProfiles": [],
 "propertySources": [{
 "name": "server.ports",
 "properties": {}
 }, {
 "name": "bootstrapProperties-configClient",
 "properties": {}
 }, {
 "name": "bootstrapProperties-file:///E:/GitHub/properties/configserver.yml",
 "properties": {}
 }, {
 "name": "servletContextInitParams",
 "properties": {}
 }, {
 "name": "systemProperties",
 "properties": {}
 }, {
 "name": "systemEnvironment",
 "properties": {}
 }, {
 "name": "springCloudClientHostInfo",
 "properties": {}
 }, {
 "name": "applicationConfig: [classpath:/bootstrap.yml]",
 "properties": {}
 }, {
 "name": "springCloudDefaultProperties",
 "properties": {}
 }
 ]
 }- 从上得知配置信息 - bootstrapProperties-file:///E:/GitHub/properties/configserver.yml具有高优先级,从先前的知识得知,自定义的bootstrap具有高优先级,实现类实现了- PropertySourceLocator- 自定义配置加载类 - org.springframework.cloud.config.client.ConfigServicePropertySourceLocator
源码分析
ConfigServicePropertySourceLocator加载配置资源
| 1 |  |