高版本的客户端不能调用低版本的服务端。
异常:org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available
出现以上异常原因:
1、端口或IP或集群名称设置错误
2、客户端版本与服务器版本不一致
elasticsearch2.X调用方式
public static Client getClient() throws UnknownHostException {
String clusterName = "elasticsearch";
List<String> clusterNodes = Arrays.asList("http://172.16.0.29:9300");
Settings settings = Settings.settingsBuilder().put("cluster.name", clusterName).build();
TransportClient client = TransportClient.builder().settings(settings).build();
for (String node : clusterNodes) {
URI host = URI.create(node);
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host.getHost()), host.getPort()));
}
return client;
}
elasticsearch5.X调用方式
public static Client getClient() throws UnknownHostException {
String clusterName = "shopmall-es";
List<String> clusterNodes = Arrays.asList("http://172.16.32.69:9300","http://172.16.32.48:9300");
Settings settings = Settings.builder().put("cluster.name", clusterName).build();
TransportClient client = new PreBuiltTransportClient(settings);
for (String node : clusterNodes) {
URI host = URI.create(node);
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host.getHost()), host.getPort()));
}
return client;
}