Window 搭建完Elasticseach 环境,使用localhost 本机都能够正常访问Elasticsearch 环境,使用Springboot +集成elasticsearch 提示如下错误信息:NoNodeAvailableException[None of the configured nodes are available
大意是:配置文件中没有可以使用的节点。
springboot 通过配置文件封装TransportClient 实体对象源码如下:
package com.zzg.search.config;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import com.zzg.search.exception.ClientException;
import lombok.extern.slf4j.Slf4j;
@Component
@ConfigurationProperties(prefix = "esconfig")
@Slf4j
public class ESConfig {
@Value("${esconfig.host}")
private String hostName;
@Value("${esconfig.port}")
private int port;
@Value("${esconfig.cluster}")
private String clusterName;
@Bean
public TransportClient client() {
// 9300是es的tcp服务端口
TransportAddress node = null;
try{
node = new TransportAddress(
InetAddress.getByName(hostName),
9300);
}catch(UnknownHostException e){
// 打印错误信息,并且抛出错误异常
log.error(e.getMessage());
throw new ClientException(null, e.getMessage());
}
// 设置es节点的配置信息
Settings settings = Settings.builder()
.put("cluster.name", clusterName)
.build();
// 实例化es的客户端对象
TransportClient client = new PreBuiltTransportClient(settings);
client.addTransportAddress(node);
return client;
}
}
application.yml 中的配置文件(错误配置):
### elasticsearch 配置 #######
esconfig:
host: 192.168.1.74
port: 9300
cluster: cluster-name
解决办法:
修改es配置文件(C:\elasticsearch\elasticsearch-6.3.2\config\elasticsearch.yml),
修改network.host
network.host:服务器IP地址
补充说明:
如果你的java代码连接方式为下面的代码,要在配置文件中存在这个集群名称
注意一下,cluster.name在es配置文件(C:\elasticsearch\elasticsearch-6.3.2\config\elasticsearch.yml)存在。如下图所示:
cluster.name的名字要在配置文件中存在,否则还会报上面的错误。