2015-09-29 3 views
7

私は、2つのプロファイル(application.dev.propertiesまたはapplication.prod.properties)のいずれかを使用して動作するSpring Boot Elasticsearchアプリケーションがあります。その部分は正常に動作します。私は、external elasticsearchをapplication.xxx.propertiesから読み込むことに問題があります。スプリングブートのエラスティックサーチコンフィギュレーション

これは動作します:

@Configuration 
@PropertySource(value = "classpath:config/elasticsearch.properties") 
public class ElasticsearchConfiguration { 

    @Resource 
    private Environment environment; 

    @Bean 
    public Client client() { 
     TransportClient client = new TransportClient(); 
     TransportAddress address = new InetSocketTransportAddress(
       environment.getProperty("elasticsearch.host"), 
       Integer.parseInt(environment.getProperty("elasticsearch.port")) 
     ); 
     client.addTransportAddress(address);   
     return client; 
    } 

    @Bean 
    public ElasticsearchOperations elasticsearchTemplate() { 
     return new ElasticsearchTemplate(client()); 
    } 
} 

をしかし、明らかに私のマルチ環境の問題を解決していません。

ホスト変数とポート変数の@Valueアノテーションも成功していませんでした。

私は上記の値をアプリケーションプロパティファイルから読み取るか、実行するプロファイルに基づいて別の@PropertySourceファイルを選択するように変換できますか?

spring.data.elasticsearch.properties.host = 10.10.1.10 
spring.data.elasticsearch.properties.port = 9300 

おかげ

+0

あなただけの春ブーツを使用して、それを回避しようとしていないのはなぜ。スプリングブートは、選択したプロファイルに基づいてプロパティファイルを読み込みます。あなたは基本的にそれを難し​​くしています...また、Spring BootはElasticSearchを既に設定しています。だから、なぜあなた自身が再びそれをやろうとしていますか? –

答えて

13

コンフィギュレーションクラスとプロパティを削除します。

ちょうどapplication-prod.propertiesapplication-dev.propertiesspring.data.elasticsearchプロパティを追加して、所望の環境のために変更するには、次の依存関係

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId> 
</dependency> 

を追加します。これはSpring BootガイドのElasticSearch sectionに記載されています。

spring.data.elasticsearch.cluster-nodes=10.10.1.10:9300 

いずれかのファイルの値は、もちろん異なります(またはapplication.propertiesでデフォルトを入れて、単にapplication-dev.propertiesで上書きされます。

春ブーツspring.profiles.activeload the desired propertiesファイルに基づいてます。

+0

フィードバックありがとうございます。私がカスタム設定なしでプロパティだけを使用しようとすると、常にElasticsearch内の内部サーバーが実行され、外部サーバーは実行されません。 –

+1

クラスタノードのプロパティを指定した場合はそうではありません(これについては、リファレンスガイドでも説明しています)。 –

+0

私はESのリファレンスガイドを読んだ。私がやっていることは、アプリケーションプロパティのspring.data.elasticsearch.cluster-nodes行と競合している必要があります。そのため、手動で構成を作成しようとしました。 –

1

私はDeinumに同意します。あなたがスプリングブートを使用している場合、アクティブなプロファイルのプロパティをアクティブにします。

私は私のプロジェクトで異なるプロファイルを持っており、これが私のelasticsearchの設定です:

@Configuration 
public class ElasticSearchConfiguration { 
    @Value("${spring.data.elasticsearch.cluster-name}") 
    private String clusterName; 
    @Value("${spring.data.elasticsearch.cluster-nodes}") 
    private String clusterNodes; 
    @Bean 
    public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException { 
      String server = clusterNodes.split(":")[0]; 
      Integer port = Integer.parseInt(clusterNodes.split(":")[1]); 
      Settings settings = Settings.settingsBuilder() 
       .put("cluster.name", clusterName).build(); 
      client = TransportClient.builder().settings(settings).build() 
       .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(server), port)); 
      return new ElasticsearchTemplate(client); 

    } 
関連する問題