2015-06-10 47 views
3

我要實現以下目標:使用配置文件禁用Spring Cloud Server配置?

  • 當「開發」模式,embbed春雲配置在當前的webapp
  • 在「開發」模式不,連接到春季雲配置服務器實例,它已經運行

因此我對當前Web應用程序的classpath包含依賴於配置服務器客戶:

<dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-config</artifactId> 
</dependency> 
<dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-config-server</artifactId> 
</dependency> 

在開發者模式,並與在bootstrap.yml以下性質,它的行(嵌入式配置服務器被配置和啓動)

spring: 
    application: 
    name: app1 
    profiles: 
    active: dev 
    cloud: 
    config: 
     uri: ${SPRING_CONFIG_URI:http://localhost:8888} 

--- 

spring: 
    profiles: dev 
    cloud: 
    config: 
     server: 
     bootstrap: true 
     git: 
      uri: https://github.com/nocquidant/my-config-repo.git 
      basedir: target/config 

當不在「dev的」模式(spring.profiles.active例如= prod),當前webapp不會啓動:它不能自動調用我的屬性(我猜嵌入式服務器是以錯誤配置啓動的)

如果我評論POM中的spring-cloud-config-server依賴項,那麼它的工作。

我我吼聲可以達到我想要使用以下,但沒有(實際使用EnableConfigServer與否似乎沒有任何改變):

@Configuration 
@Profile("dev") 
@EnableConfigServer 
public static class EmbeddedConfigServer { } 

我怎麼能這樣做(不使用maven配置文件) ? 有沒有簡單的方法來禁用春天的雲配置服務器? 有沒有更好的方法來實現我的目標?

任何幫助表示讚賞。

P.S.使用的版本:

<parent> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-parent</artifactId> 
    <version>1.0.3.BUILD-SNAPSHOT</version> 
</parent> 

//尼克

// ---編輯#1

請參閱在github上一個簡單的項目:https://github.com/nocquidant/exchange/tree/master/poc-spring-cloud/

它包含了2個簡單模塊:config-服務器和客戶端應用程序。

首先,如果您啓動從客戶應用模塊的客戶端類,一切正常(嵌入模式是=>看到bootstrap.yml dev的配置文件)

然後,啓動配置服務器(ConfigServer類)。然後在客戶端應用程序中更改'prod'(或其他)的'spring.profiles.active'並重新啓動它:屬性注入不再工作(並且webapp拒絕啓動)。

Caused by: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors 
Field error in object 'server' on field 'port': rejected value [${taap.bak.config.port}]; 

因此它無法連接到配置服務器模塊。如果您從客戶端應用程序模塊註釋maven依賴項:spring-cloud-config-server,它將再次運行。

希望現在更清楚。 謝謝,

+0

我想到了'@profile(「開發」)'會工作。你看到什麼錯誤? – spencergibb

+0

我剛剛編輯我的職務,以使其更清晰。謝謝。 –

回答

5

根據春季雲配置服務器this class:配置服務器明確禁用配置客戶端,除非你從系統屬性(-D),或通過SpringApplicationBuilder設置spring.cloud.config.enabled=true。這是目前實現這一目標的唯一方法。歡迎您提交解釋您的使用案例的改進請求。

+0

我看過並嘗試在我的bootstrap.yml(for profile!dev)中設置此屬性,但沒有成功。我只是試圖從系統屬性('-D')settting它和它的作品如預期。謝謝!附:它似乎沒有使用'SpringApplicationBuilder'工作... –

相關問題