我有一个工作正常的Spring Boot Elasticsearch Application,它使用以下两个配置文件之一:application.dev.properties或application.prod.properties。那部分工作正常。我在获取来自application.xxx.properties的外部elasticsearch时遇到问题。
这有效:
@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
谢谢
删除您的配置类和属性。
添加以下依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
只需将spring.data.elasticsearch
属性添加到application-prod.properties
和,application-dev.properties
然后更改为所需的环境即可。在Spring Boot指南的ElasticSearch部分中对此进行了描述。
spring.data.elasticsearch.cluster-nodes=10.10.1.10:9300
当然,这两个文件中的值都会有所不同(或将默认值放在中,application.properties
并简单地用覆盖application-dev.properties
。
Spring Boot将基于spring.profiles.active
加载所需的属性文件。
无需乱动自己。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句