繁体   English   中英

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

[英]Spring Cloud Config: define server config properties programmatically

我有自我配置的 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应该以其他方式以编程方式声明(如果可能的话)。

好吧,在深入研究 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;
    }

}

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

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

你为什么更喜欢:

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 参数( .... -Dspring.cloud.config.server.git.uri=xxxxxx .... )或通过环境变量( ... SPRING_CLOUD_CONFIG_SERVER_GIT_URI=xxxxx .... java -jar application.jar

在 bootstrap.yml 中尝试以下操作

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

并将值添加为环境变量 -

SPRING_CLOUD_CONFIG_USERNAME
SPRING_CLOUD_CONFIG_PASSWORD

暂无
暂无

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

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