2017-05-08 10 views
2

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

问题:使用外部属性文件之前,我的内部属性文件位于:SRC /主/资源,我用

@PropertySource("classpath:myproperties.properties) 

访问它在我的应用程序,但使用云配置的服务器,什么都后为了使我的spring-boot应用程序明白现在它必须从git存储库中获取属性,我应该做些什么改变?

我在manifest.yml 添加

services 
    - my-config-server 

我加入@EnableConfigServer和@RefreshScope

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

@PropertySource("classpath:myproperties.properties) 

回答

1

您应该由bootstrap.yml文件,让您的git仓库的URL替换文件myproperties.properties

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

然后在仓库,您应该重命名myproperties.propertiesapplication.properties。要访问属性,使用@Value注释,如:

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

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

+0

谢谢。 几个问题: 1.为了访问属性,我使用'import org.springframework.core.env.Environment;' @Autowired public环境env; env.getProperty( “foo.bar”);因为我动态地需要传递一个属性并获取它的值。你的逻辑是否可以使用env.getProperty(“”)? – Tiya

+0

2.在bootstrap.yml中,值为: spring:cloud:config:server:git:uri:ssh://[email protected]:7999/user/gaia.git \t \t work ?它是一个.git库uri。 3.如果我用application.properties替换myproperties,那么应该是我的外部属性文件的名称? application.properties或myproperties? – Tiya

+0

1.是2.是3.'bootstrap.yml'将替换classpath上的文件,'application.properties'将被存储在git仓库中。 –

相关问题