Springcloud Config支持本地配置文件的方法示例
2020-04-19 12:05
239 查看
背景:
Springcloud项目使用Springcloud-config作为分布式配置,配置参数都放在config里,不同的环境有不同的问题:
项目本地:
- boostrap.yml
远程配置:
- application.yml
- application-local.yml
- application-dev.yml
- application-test.yml
- application-prod.yml
其中application-local.yml是本地开发环境,由于开发时,经常修改配置,就会频繁去修改config。
所以想将application-local.yml放在项目本地,而不是在config里。
也就是最终变成:
项目本地:
- boostrap.yml
- application-local.yml
远程配置:
- application.yml
- application-dev.yml
- application-test.yml
- application-prod.yml
调整之后,发现项目启动失败,项目并不会去读取本地的application-local.yml,需要我们来指定加载。
调整
原先的启动代码:
SpringApplication.run(Application.class, args);
改成:
new SpringApplicationBuilder(Application.class) .properties("spring.config.location=classpath:application-${spring.profiles.active}.yml,classpath:bootstrap.yml") .run(args);
一定要指定classpath:bootstrap.yml(如果还有其他本地文件,也是一样),如果在没有配置spring.config.location的情况下,项目会默认加载classpath:bootstrap.yml,如果指定了就只会加载指定的配置文件。
测试用例
如果用了spring-test+junit,可以通过properties指定配置文件:
@SpringBootTest(properties = {"spring.config.location=classpath:tscm-service-oem-forecast-${spring.profiles.active}.yml,classpath:bootstrap.yml"})
也就是最终是:
@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {"spring.config.location=classpath:tscm-service-oem-forecast-${spring.profiles.active}.yml,classpath:bootstrap.yml"})
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- Spring Cloud Config 使用本地配置文件
- Spring Cloud Config配置文件使用对称加密的方法
- spring cloud config server 使用本地配置文件报错
- Spring Cloud Config RSA简介及使用RSA加密配置文件的方法
- SpringCloud之分布式配置中心-本地配置文件(Spring Cloud Config) |第十章 -yellowcong
- spring cloud config 统一配置中心 读取Git/SVN/本地文件配置及动态刷新
- spring cloud config 用svn做配置文件仓库
- Spring Cloud Config 的坑:以ssh方式从自己搭建的 git repository 取配置文件
- Spring cloud config配置文件加密解密
- SpringCloud-动态配置变化监控-获取变化(支持Config、Nacos)
- Java中spring读取配置文件的几种方法示例
- Spring Cloud Config - RSA简介以及使用RSA加密配置文件
- SpringCloud系列九:SpringCloudConfig 基础配置(SpringCloudConfig 的基本概念、配置 SpringCloudConfig 服务端、抓取配置文件信息、客户端使用 SpringCloudConfig 进行配置、单仓库目录匹配、应用仓库自动选择、仓库匹配模式)
- springcloud项目配置拓展从本地config目录加载
- 服务调用RMI远程方法调用示例【基于Spring配置文件】
- Spring Cloud Config配置文件加解密
- springcloud config 访问配置文件
- Spring Cloud Config - RSA简介以及使用RSA加密配置文件
- Spring Cloud Config + Spring Cloud Bus 实现配置文件动态刷新(版本 Finchley.RC1)
- 详解使用spring cloud config来统一管理配置文件