2017-05-08 11 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倉庫中。 –

相關問題