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