NoNodeAvailableException[None of the configured nodes are available
这个报错,意思就是你要连接的节点不可用,连不上,一般有以下几个原因:
1、ES服务没启动,肯定连不上,可以访问下9200端口试下;
2、端口使用错了,在Java创建es的client连接,要使用9300端口(TCP端口,客户端连接使用此端口,在配置文件elasticsearch.yml里配置的,默认9300),不是9200(HTTP端口,RESTful api使用此端口);
3、9300端口没开通访问权限,防火墙限制了;
4、没有使用默认的应用名称,导致cluster name不一致;cluster.name 配置的,和代码使用的必须一致。
下面给一个连接实例:
@Bean
public ElasticsearchTemplate elasticsearchTemplate(){
try {
//设置集群名称
Settings settings = Settings.builder()
.put("cluster.name", globalConfig.getEsClusterName())//必须与es配置文件里的cluster name 一致
.put("client.transport.sniff", true)
.build();
//构建TransportAddress
String hosts = globalConfig.getEsClusterHost();
int port = Integer.parseInt(globalConfig.getEsClusterPort());//9300
List<TransportAddress> addressList = new ArrayList<>();
if(!StringUtils.isEmpty(hosts)){
for(String host:hosts.split(",")){
addressList.add(new InetSocketTransportAddress(InetAddress.getByName(host),port));
}
}
//创建client
Client client = new PreBuiltTransportClient(settings).addTransportAddresses(
(TransportAddress[])addressList.toArray(new TransportAddress[addressList.size()]));
return new ElasticsearchTemplate(client);
}catch (Exception e){
e.printStackTrace();
}
return null;
}