我尝试在Spring MVC应用程序中配置ElasticSearch存储库。 我使用的是Spring Data ElasticSearch版本:2.0.7和ElasticSearch Server 2.4.4。
我确定ElasticNode正常工作,这里是示例输出
$ curl -X GET http://127.0.0.1:9200/
{
"name" : "Tattoo",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "dX0lPfNnSA6vxGqhzVEuSg",
"version" : {
"number" : "2.4.4",
"build_hash" : "fcbb46dfd45562a9cf00c604b30849a6dec6b017",
"build_timestamp" : "2017-01-03T11:33:16Z",
"build_snapshot" : false,
"lucene_version" : "5.5.2"
},
"tagline" : "You Know, for Search"
}
这是我的测试配置
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.somepackage.repo.elastic")
public class ElasticSearchConfig {
@Bean
public ElasticsearchTemplate elasticsearchTemplate() throws UnknownHostException {
return new ElasticsearchTemplate(nodeClient());
}
@Bean
public TransportClient nodeClient() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
TransportClient client = TransportClient.builder()
.settings(settings)
.build();
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9200));
return client;
}
}
我收到应用程序无法连接到Elastic node,stacktrace
的错误2017-02-17 23:34:53 INFO transport:383 - [Impulse] failed to get node info for {#transport#-1}{127.0.0.1}{localhost/127.0.0.1:9200}, disconnecting...
ReceiveTimeoutTransportException[[][localhost/127.0.0.1:9200][cluster:monitor/nodes/liveness] request_id [1] timed out after [5002ms]]
at org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:645)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
我尝试从1.7.1,2.4.4和5.2.1更改弹性搜索节点的版本。什么都行不通。 使用Java 8的Spring MVC 4.3.6.RELEASE
答案 0 :(得分:2)
Transport client通过端口9300与elasticsearch进行对话。请尝试
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
答案 1 :(得分:2)
简而言之:ElasticSearch节点和TransportClient应该具有相同的版本。
Spring Data ElasticSearch在2.2.0版中提供TransportClient。我在版本2.4.4中使用ElasticSearch节点。我将ES节点降级到2.2.0并在配置中将端口从9200更改为9300。