我是Elastic Search Java Api的新手[5.0]。我正在使用elasticsearch-5.0.0。 我尝试使用Spring Boot创建Java应用程序(Maven)。运行Application后,显示
2016-11-04 23:32:19.339 INFO 8280 --- [][generic][T#2]] org.elasticsearch.client.transport : [X-Ray] failed to get node info for [#transport#-1][DESKTOP-8SIPHSN][inet[localhost/127.0.0.1:9300]], disconnecting...
org.elasticsearch.transport.NodeDisconnectedException: [][inet[localhost/127.0.0.1:9300]][cluster:monitor/nodes/info] disconnected
我的配置文件
@Configuration
public class ElasticsearchConfiguration {
@Bean
public Client client() {
TransportClient client = new TransportClient();
TransportAddress address = new InetSocketTransportAddress("localhost",9300);
client.addTransportAddress(address);
return client;
}
}
我正在使用默认群集“elasticsearch”。通过正确检测原因,我需要帮助来解决我的问题。
答案 0 :(得分:5)
尝试使用5.0文档中提到的PreBuiltTransportClient
:
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html
另请注意,ES版本2.x的TransportClient
与5.x:
客户端必须具有相同的主要版本(例如2.x或5.x) 集群中的节点。客户端可以连接到具有的群集 不同的次要版本(例如2.3.x)但有可能是新的 可能不支持功能。理想情况下,客户应该有 与集群版本相同。
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/client.html
<强>更新强>
作为连接测试,请尝试执行以下简单程序:
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class App {
public static void main(String[] args) throws UnknownHostException {
// The following settings aren't strictly necessary, because the default cluster name is "elasticsearch".
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
System.out.println(client.connectedNodes());
}
}
它应该打印到stdout,如下所示:
[{luhcORJ}{luhcORJOSzSLPBeXocDsuQ}{mkTJpwIAQGuNYTHfRLqUIw}{127.0.0.1}{127.0.0.1:9300}]