Background:
The Spring Cloud project uses the Spring Cloud-config as the distributed configuration, and the configuration parameters are all placed in the config. Different environments have different problems:
Project Local: Boostrap. yml Remote Configuration: application.yml application-local.yml application-dev.yml application-test.yml application-prod.yml
Application-local.yml is the local development environment. Since the configuration is often modified during development, the config will be frequently modified. So you want to place application-local.yml locally in your project, not in your config. Which ultimately becomes:
Boostrap.yml application-local.yml Boostrap.yml application-local.yml application.yml application-dev.yml application-test.yml application-prod.yml
After the adjustment, we found that the project failed to start, the project will not read the local application-local.yml, we need to specify the load.
Adjust the
The original startup code:
SpringApplication.run(Application.class, args);
To:
new SpringApplicationBuilder(Application.class)
.properties("spring.config.location=classpath:application-${spring.profiles.active}.yml,classpath:bootstrap.yml")
.run(args);
Be sure to specify classpath:bootstrap.yml (as well if there are other local files). The project will load classpath:bootstrap.yml by default if Spring.config. location is not configured. If specified, only the specified configuration file will be loaded.
The test case
If you are using spring-test+ JUnit, you can specify the configuration file through properties:
@SpringBootTest(properties = {"spring.config.location=classpath:tscm-service-oem-forecast-${spring.profiles.active}.yml,classpath:bootstrap.yml"})
Which is ultimately:
@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"})
Or use TestPropertySource annotation:
@TestPropertySource(properties = {"spring.config.location=tscm-service-oem-forecast-${spring.profiles.active}.yml,classpath:bootstrap.yml"})