简体   繁体   中英

Automatization Spring Cloud Profile

Actually have a little problem.

I want switch the url of my bootstrap.yml

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.

Something similar is suggested on this answer: 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 . Then, for a Java config, you could have something like:

@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.
Define a default bootstrap.yml in src/main/resources and define a specific bootstrap.yml file for each environment.

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 :
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. For example if the dev profile is enabled, the bootstrap-dev.properties in the classpath will be used.

I tend to use the first way because that is more explicit for non Spring Boot users.

Source : 1.3 Changing the Location of Bootstrap Properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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