[英]Automatization Spring Cloud Profile
其实有点小问题。
我想切换我的 bootstrap.yml 的 url
它看起来如下:
spring:
application:
name: <project-name>
profiles:
active: dev
cloud:
config:
uri: http://<git-repository>:8080
fail-fast: false
这有效,但我想要一个属性或任何可以在本地或其他环境中切换的东西。
我尝试查看此文档,但没有看到任何适合我的工作。
我认为 Spring Cloud 与任何 Spring 应用程序没有任何不同,因此您可以使用 Spring 配置文件。
在这个答案中提出了类似的建议: https : //stackoverflow.com/a/22759706/6908551 。
您可以为您的云配置 uri 定义一个单独的.yml
文件,例如cloud-config-dev.yml
、 cloud-config-prod.yml
。 然后,对于 Java 配置,您可以使用以下内容:
@Configuration
public class MyApplicationConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
String activeProfile = System.getProperty("spring.profiles.active", "production");
String ymlFilename = "cloud-config-" + activeProfile + ".yml";
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource(ymlFilename));
return configurer;
}
}
我会按环境定义一个 bootstrap.yml 文件。
在src/main/resources
定义一个默认的 bootstrap.yml 并为每个环境定义一个特定的 bootstrap.yml 文件。
然后有多种方式。
不详尽:
1)对于配置文件不同的每个环境,通过使用预期值指定系统属性spring.cloud.bootstrap.location
来运行你的 spring boot jar,例如:
java -jar ... -Dspring.cloud.bootstrap.location=bootstrap-dev.yml ...
.
这会覆盖该文件的当前位置。
2) 利用 Spring Boot 配置文件功能:与bootstrap.yml
兼容。 例如,如果启用了开发配置文件,则将使用类路径中的bootstrap-dev.properties
。
我倾向于使用第一种方式,因为这对于非 Spring Boot 用户来说更为明确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.