Springboot整合Elasticsearch 提示:NoNodeAvailableException[None of the configured nodes are available

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的名字要在配置文件中存在,否则还会报上面的错误。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值