Java Es报错 NoNodeAvailableException[None of the configured nodes are available:{127.0.0.1:9300}

Java Elasticsearch 报错 NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{lq_UlumMQwicWXO8-2hUdQ}{127.0.0.1}{127.0.0.1:9300}]]

环境

  • Win10
  • Elasticsearch 6.7.0

问题

Java编写Elasticsearch测试用例:

package com.itheima.es;

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.junit.Test;

import java.net.InetAddress;

public class ElasticSearchClientTest {

    @Test
    public void createIndex() throws Exception {
        //1) 创建一个Settings对象,相当于配置信息,主要配置集群名称。
        Settings settings = Settings.builder()
                .put("cluster.name", "my-elasticsearch")
                .build();
        //2) 创建一个客户端client对象
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        //3) 使用client对象创建一个索引库
        client.admin().indices().prepareCreate("index_hello")
                //执行操作
                .get();
        //4) 关闭client对象
        client.close();
    }
}

程序执行报错:

NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{lq_UlumMQwicWXO8-2hUdQ}{127.0.0.1}{127.0.0.1:9300}]]

错误原因

  1. 端口错误

仔细观察ES启动信息发现端口是9301es启动信息截图
2. 未修改Elasticsearch配置文件

观察es配置文件 elasticsearch.yml 发现 cluster.name 被注释掉,需要手动启动配置
Java Es报错 NoNodeAvailableException[None of the configured nodes are available:{127.0.0.1:9300}_第1张图片

解决方法

修改代码中9300端口为9301

client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9301));

错误变化为

[main] WARN org.elasticsearch.client.transport.TransportClientNodesService - node {#transport#-1}{ZJkXvfFCRgG_RwubwxtEug}{127.0.0.1}{127.0.0.1:9301} not part of the cluster Cluster [my-elasticsearch], ignoring...

NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{ZJkXvfFCRgG_RwubwxtEug}{127.0.0.1}{127.0.0.1:9301}]
]

修改es配置文件 elasticsearch.yml
增加“cluster.name: my-elasticsearch”
Java Es报错 NoNodeAvailableException[None of the configured nodes are available:{127.0.0.1:9300}_第2张图片
执行代码

public class ElasticSearchClientTest {

    @Test
    public void createIndex() throws Exception {
        //1) 创建一个Settings对象,相当于配置信息,主要配置集群名称。
        Settings settings = Settings.builder()
                .put("cluster.name", "my-elasticsearch")
                .build();
        //2) 创建一个客户端client对象
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9301));
        //3) 使用client对象创建一个索引库
        client.admin().indices().prepareCreate("index_hello")
                //执行操作
                .get();
        //4) 关闭client对象
        client.close();
    }
}

成功
在这里插入图片描述

你可能感兴趣的:(Java,Elasticsearch)