Springcloud项目使用Springcloud-config做为分布式配置,配置参数都放在config里,不一样的环境有不一样的问题:web
项目本地: boostrap.yml 远程配置: application.yml application-local.yml application-dev.yml application-test.yml application-prod.yml
其中application-local.yml
是本地开发环境,因为开发时,常常修改配置,就会频繁去修改config
。
因此想将application-local.yml
放在项目本地,而不是在config
里。
也就是最终变成:spring
项目本地: boostrap.yml application-local.yml 远程配置: application.yml application-dev.yml application-test.yml application-prod.yml
调整以后,发现项目启动失败,项目并不会去读取本地的application-local.yml
,须要咱们来指定加载。bootstrap
原先的启动代码:app
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
指定配置文件:ui
@SpringBootTest(properties = {"spring.config.location=classpath:tscm-service-oem-forecast-${spring.profiles.active}.yml,classpath:bootstrap.yml"})
也就是最终是:spa
@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"})