Automatization Spring Cloud Profile

和自甴很熟 提交于 2021-01-29 17:10:49

问题


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.


回答1:


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;
    }
}



回答2:


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



来源:https://stackoverflow.com/questions/60226678/automatization-spring-cloud-profile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!