Joe*_*ann 12 java elasticsearch spring-boot
我有一个工作的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());
}
}
Run Code Online (Sandbox Code Playgroud)
但显然无法解决我的多环境问题.
我还尝试了@Value注释主机和端口变量没有成功.
如何转换上面的内容以从应用程序属性文件中读取其值,或者根据我想要运行的任何配置文件选择不同的@PropertySource文件?
spring.data.elasticsearch.properties.host = 10.10.1.10
spring.data.elasticsearch.properties.port = 9300
Run Code Online (Sandbox Code Playgroud)
谢谢
M. *_*num 26
删除配置类和属性.
添加以下依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
只需将spring.data.elasticsearch 属性添加到application-prod.properties和,application-dev.properties然后更改所需的环境即可.这在Spring Boot指南的ElasticSearch部分中有所描述.
spring.data.elasticsearch.cluster-nodes=10.10.1.10:9300
Run Code Online (Sandbox Code Playgroud)
任何一个文件中的值当然都会有所不同(或者将默认值放在application.properties并且简单地覆盖一个application-dev.properties.
Spring Boot将根据spring.profiles.active 加载所需的属性文件.
没有必要乱搞自己.
| 归档时间: |
|
| 查看次数: |
32897 次 |
| 最近记录: |