简体   繁体   English

自动化 Spring Cloud Profile

[英]Automatization Spring Cloud Profile

Actually have a little problem.其实有点小问题。

I want switch the url of my bootstrap.yml我想切换我的 bootstrap.yml 的 url

It looks as follows:它看起来如下:

spring:
  application:
    name: <project-name>
  profiles:
    active: dev
  cloud:
    config:
      uri: http://<git-repository>:8080
      fail-fast: false

This works, but i want have an propertie or anything what can switch if are in local or another enviroment.这有效,但我想要一个属性或任何可以在本地或其他环境中切换的东西。

I try to see this documentation but dont see any work for me.我尝试查看此文档,但没有看到任何适合我的工作。

I don't think Spring Cloud is any different from any Spring application, so you could use the Spring profiles.我认为 Spring Cloud 与任何 Spring 应用程序没有任何不同,因此您可以使用 Spring 配置文件。

Something similar is suggested on this answer: https://stackoverflow.com/a/22759706/6908551 .在这个答案中提出了类似的建议: https : //stackoverflow.com/a/22759706/6908551

You could define a separate .yml file just for your cloud config uri, like cloud-config-dev.yml , cloud-config-prod.yml .您可以为您的云配置 uri 定义一个单独的.yml文件,例如cloud-config-dev.ymlcloud-config-prod.yml Then, for a Java config, you could have something like:然后,对于 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;
    }
}

I would define a bootstrap.yml file by environment.我会按环境定义一个 bootstrap.yml 文件。
Define a default bootstrap.yml in src/main/resources and define a specific bootstrap.yml file for each environment.src/main/resources定义一个默认的 bootstrap.yml 并为每个环境定义一个特定的 bootstrap.yml 文件。

Then there are multiple ways.然后有多种方式。
Not exhaustive :不详尽:

1) For each environment where the configuration file differs, run your spring boot jar by specifying the system property spring.cloud.bootstrap.location with the expected value such as : 1)对于配置文件不同的每个环境,通过使用预期值指定系统属性spring.cloud.bootstrap.location来运行你的 spring boot jar,例如:
java -jar ... -Dspring.cloud.bootstrap.location=bootstrap-dev.yml ... . java -jar ... -Dspring.cloud.bootstrap.location=bootstrap-dev.yml ... .
That overrides the current location of that file.这会覆盖该文件的当前位置。

2) Take advantage of Spring Boot profile feature : bootstrap.yml is compatible with. 2) 利用 Spring Boot 配置文件功能:与bootstrap.yml兼容。 For example if the dev profile is enabled, the bootstrap-dev.properties in the classpath will be used.例如,如果启用了开发配置文件,则将使用类路径中的bootstrap-dev.properties

I tend to use the first way because that is more explicit for non Spring Boot users.我倾向于使用第一种方式,因为这对于非 Spring Boot 用户来说更为明确。

Source : 1.3 Changing the Location of Bootstrap Properties来源: 1.3 更改 Bootstrap 属性的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM