【问题标题】:Spring Cloud Config: define server config properties programmaticallySpring Cloud Config:以编程方式定义服务器配置属性
【发布时间】:2018-02-16 17:08:15
【问题描述】:

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

【问题讨论】:

  • bootstrap.properties 是 Spring 框架首先读取的内容之一。我认为您不能以编程方式添加它。但我相信你可以从命令行传递那个值(如果你不想把它放在属性文件中)
  • @pvpkiran 这是我的第一个想法。但后来我想出了一个“hackish”解决方案,请在下面找到它。

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


【解决方案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;
    }

}

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

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

【讨论】:

  • 你为什么称它为肮脏的黑客?
  • 首先,我认为它很麻烦:通常,Spring 提供了更流畅的配置方式。为什么我应该知道一些spring.factories?我以前从未使用过它,理想情况下我宁愿不知道这个文件:) 其次,我找不到一种简单的方法来向现有的属性添加属性:我必须迭代现有的属性并手动将它们复制到新地图,然后添加我自己的几个,然后重新设置此地图。我也觉得有点麻烦,一点也不优雅。
  • 没有可用于 ConfigServerProperties 的 bean。 MultipleJGitEnvironmentRepository() 有 2 个参数。
【解决方案2】:

你为什么喜欢:

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?

【讨论】:

    【解决方案3】:

    在您的 bootstrap.yml 中尝试以下操作

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

    并将值添加为环境变量-

    SPRING_CLOUD_CONFIG_USERNAME
    SPRING_CLOUD_CONFIG_PASSWORD
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 2016-06-26
      • 2019-03-26
      • 2016-02-07
      • 2020-07-10
      • 2015-10-28
      • 2015-07-13
      相关资源
      最近更新 更多