2017-03-02 13 views
2

私は春のブートアプリケーションを持っており、私は領事エージェントに持っているプロパティを取得したいと思います。 スプリングブート領事サーバからプロパティを取得

@EnableDiscoveryClient 
@SpringBootApplication(scanBasePackages={"com.commons"}) 
public class MainAppProxy implements CommandLineRunner {  
    @Value("${proxy.endpoint}") 
    private String endpointAddress; 

私application.properties

は、私は、次の設定(ショートバージョン)が

  <artifactId>spring-cloud-dependencies</artifactId> 
      <version>Camden.SR5</version> 
     <groupId>org.springframework.cloud</groupId> 
     <artifactId>spring-cloud-starter-config</artifactId> 

プロパティが上に保存されているのsrc /メイン/リソースのpom.xmlで

spring.application.name=SOAPProxy 
spring.cloud.consul.host=http://10.0.1.241 
spring.cloud.consul.port=8500 
spring.cloud.config.discovery.enabled=false 

の下にありますこの形式の領事: ビジネス/ SOAPProxy/proxy.endpoint

アプリケーションが起動すると、領事を見つけたようですが、領事@Value( "$ {proxy.endpoint}")を試す前に値を取得できません 領事でどのようにプロパティを取得できますか?

+0

にすべての構成負荷あなたは領事から設定を取得する必要がありますか? – wthamira

答えて

1

あなたは領事からのコンフィギュレーションをロードするために3つの方法を使用することができ

  1. キー/値
  2. YAML
  3. ファイル

私はコンフィギュレーションをロードするためにYAMLで使用します。

これは私のブートアプリは

@EnableDiscoveryClient 
@EnableAutoConfiguration 
@SpringBootApplication 
public class SpringBootConsulApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(SpringBootConsulApplication.class, args); 
    } 
} 

以下のMaven依存のような注釈私bootstrap.ymlファイル(あなたも.propertyファイルを使用することができます)

spring: 
    application: 
    name: SOAPProxy 

--- 

spring: 
    profiles: default 
    cloud: 
    consul: 
     config: 
     data-key: data 
     prefix: config 
     format: yaml 
     host: localhost 
     port: 8500 
     discovery: 
     prefer-ip-address: true 

である。このよう

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

<dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-consul-discovery</artifactId> 
</dependency> 
を追加

これは、領事代理人の鍵/値の設定です。

スタートアップで今

enter image description here

アプリケーション

関連する問題