Spring Cloud Config:以编程方式定义服务器配置属性

时间:2018-02-16 17:08:16

标签: java spring spring-cloud spring-cloud-config

我有自配置的Spring Cloud Config服务器(它自己用于自己的配置)。

所以基本上我有一个文件bootstrap.properties,内容为:

spring.cloud.config.server.bootstrap=true
spring.cloud.config.server.git.uri=<my GitHub repo>
spring.application.name=config
spring.profiles.active=development

它运行良好,但我想使用Java代码定义上面的属性。事实上,我可以在那里保留这些属性,我只想以编程方式添加spring.cloud.config.server.git.usernamespring.cloud.config.server.git.password。有可能以某种方式做到这一点吗?

我尝试使用常用方法来添加/覆盖application.properties中定义的Spring属性,但是没有成功:看起来像bootstrap.properties应该以其他方式以编程方式声明(如果它甚至可能)

3 个答案:

答案 0 :(得分:1)

嗯,在深入挖掘Spring资源后,我设法找到了解决方案。我不喜欢它,因为它看起来像一个肮脏的黑客,但至少它是有效的。如果有人提出更清洁的解决方案,我将不胜感激。

资源/ META-INF / spring.factories:

# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
my.awesome.package.ConfigClientBootstrapConfiguration

ConfigClientBootstrapConfiguration:

@Configuration
public class ConfigClientBootstrapConfiguration {

    @Autowired
    private ConfigurableEnvironment environment;
    @Autowired
    private ConfigServerProperties server;

    @Bean
    public MultipleJGitEnvironmentRepository environmentRepository() {
        Map<String, Object> properties = new HashMap<>();
        properties.put("spring.cloud.config.server.bootstrap", "true");
        properties.put("spring.cloud.config.server.git.uri", "<GitHub repo URI>");
        properties.put("spring.cloud.config.server.git.username", "username");
        properties.put("spring.cloud.config.server.git.password", "password");
        properties.put("spring.application.name", "config");
        properties.put("spring.profiles.active", "development");
        MapPropertySource customPropertySource = new MapPropertySource("applicationConfig: [classpath:/bootstrap.properties]", properties);
        environment.getPropertySources().replace("applicationConfig: [classpath:/bootstrap.properties]", customPropertySource);
        MultipleJGitEnvironmentRepository repository = new MultipleJGitEnvironmentRepository(this.environment);
        if (this.server.getDefaultLabel() != null) {
            repository.setDefaultLabel(this.server.getDefaultLabel());
        }
        return repository;
    }

}

P.S。也可以在此处对现有属性添加迭代,以将它们保留在文件中(并可能覆盖)。

P.P.S。我不知道为什么,但它只适用于班级名ConfigClientBootstrapConfiguration。当我重命名它时,它停止了工作。我不太关心这里的命名......

答案 1 :(得分:0)

你为什么喜欢:

properties.put("spring.cloud.config.server.git.uri", "<GitHub repo URI>");
properties.put("spring.cloud.config.server.git.username", "username");
properties.put("spring.cloud.config.server.git.password", "password");

将它们添加到bootstrap.yml

或通过VM args(.... -Dspring.cloud.config.server.git.uri=xxxxxx ....)或env变量(... SPRING_CLOUD_CONFIG_SERVER_GIT_URI=xxxxx .... java -jar application.jar

设置它们

答案 2 :(得分:0)

在bootstrap.yml中尝试以下

    username: ${SPRING_CLOUD_CONFIG_USERNAME:defaultuser}
    password: ${SPRING_CLOUD_CONFIG_PASSWORD:defaultpass}

并将值添加为 -

的环境变量
SPRING_CLOUD_CONFIG_USERNAME
SPRING_CLOUD_CONFIG_PASSWORD