云配置服务器与外部文件和弹簧启动应用程序

时间:2017-05-08 19:36:49

标签: git spring-boot spring-cloud-config

我在Git上创建了一个个人存储库,我保存了我的属性文件。 我创建了一个云配置服务器(' my-config-server')并使用了git存储库url。 我绑定了我的spring-boot应用程序,该应用程序应该使用Git存储库访问外部属性文件。

问题:在使用外部属性文件之前,我的内部属性文件位于:src / main / resources,我使用

在我的应用程序中访问它
@PropertySource("classpath:myproperties.properties)

但是在使用云配置服务器之后,为了使我的spring-boot应用程序理解现在它必须从git存储库中获取属性,我应该做出哪些更改?

我添加了

services
   - my-config-server
manifest.yml中的

我添加了@EnableConfigServer和@RefreshScope

还有什么需要做的? 该怎么办

 @PropertySource("classpath:myproperties.properties)

1 个答案:

答案 0 :(得分:1)

您应该用myproperties.properties文件替换文件bootstrap.yml,该文件提供您的git存储库的URL:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/spring-cloud-samples/config-repo

然后在存储库中,您应该使用myproperties.properties重命名application.properties。要访问您的属性,请使用@Value注释,例如:

@Value("${my.color}")
private String myColor;

阅读Spring Cloud Config reference了解更多详情。

相关问题