Java ElasticSearch没有配置的节点可用

时间:2014-09-18 12:25:46

标签: java elasticsearch

过去一小时刚刚下载并安装了elasticsearch 1.3.2

打开IP表到端口9200和9300:9400

在/ etc / hosts

中设置我的计算机名称和IP

头部模块和护理人员安装并顺利运行

对localhost的卷曲工作完美无缺

将所有jar从下载复制到eclipse中,因此版本客户端

- 爪哇 -

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilders;


public class Test{         
    public static void main(String[] args) {
        Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elastictest").build();
            TransportClient transportClient = new TransportClient(settings);
                Client client = transportClient.addTransportAddress(new InetSocketTransportAddress("143.79.236.xxx",9300));//just masking ip with xxx for SO Question

            try{
                SearchResponse response = client.prepareSearch().setQuery(QueryBuilders.matchQuery("url", "twitter")).setSize(5).execute().actionGet();//bunch of urls indexed
                    String output = response.toString();
                        System.out.println(output);
            }catch(Exception e){
                e.printStackTrace();
            }
                client.close();
        }
}

- 输出 -

log4j:WARN No appenders could be found for logger (org.elasticsearch.plugins).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []
    at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:298)
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:214)
    at org.elasticsearch.client.transport.support.InternalTransportClient.execute(InternalTransportClient.java:105)
    at org.elasticsearch.client.support.AbstractClient.search(AbstractClient.java:330)
    at org.elasticsearch.client.transport.TransportClient.search(TransportClient.java:421)
    at org.elasticsearch.action.search.SearchRequestBuilder.doExecute(SearchRequestBuilder.java:1097)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:91)
    at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:65)
    at Test.main(Test.java:20)

更新:现在我真的很困惑。我只是在日食中按了3次。 2次收到上述错误。 1次搜索工作!??全新的Centos 6.5 vps,全新的jdk安装。然后安装了elasticsearch,没有做任何其他事情。

更新:运行./bin/elasticsearch console

之后
[2014-09-18 08:56:13,694][INFO ][node                     ] [Acrobat] version[1.3.2], pid[2978], build[dee175d/2014-08-13T14:29:30Z]
[2014-09-18 08:56:13,695][INFO ][node                     ] [Acrobat] initializing ...
[2014-09-18 08:56:13,703][INFO ][plugins                  ] [Acrobat] loaded [], sites [head, paramedic]
[2014-09-18 08:56:15,941][WARN ][common.network           ] failed to resolve local host, fallback to loopback
java.net.UnknownHostException: elasticsearchtest: elasticsearchtest: Name or service not known
        at java.net.InetAddress.getLocalHost(InetAddress.java:1473)
        at org.elasticsearch.common.network.NetworkUtils.<clinit>(NetworkUtils.java:54)
        at org.elasticsearch.transport.netty.NettyTransport.<init>(NettyTransport.java:204)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at org.elasticsearch.common.inject.DefaultConstructionProxyFactory$1.newInstance(DefaultConstructionProxyFactory.java:54)
        at org.elasticsearch.common.inject.ConstructorInjector.construct(ConstructorInjector.java:86)
        at org.elasticsearch.common.inject.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:98)
        at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:45)
        at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:837)
        at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:42)
        at org.elasticsearch.common.inject.Scopes$1$1.get(Scopes.java:57)
        at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:45)
        at org.elasticsearch.common.inject.FactoryProxy.get(FactoryProxy.java:52)
        at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:45)
        at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:837)
        at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:42)
        at org.elasticsearch.common.inject.Scopes$1$1.get(Scopes.java:57)
        at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:45)
        at org.elasticsearch.common.inject.SingleParameterInjector.inject(SingleParameterInjector.java:42)
        at org.elasticsearch.common.inject.SingleParameterInjector.getAll(SingleParameterInjector.java:66)
        at org.elasticsearch.common.inject.ConstructorInjector.construct(ConstructorInjector.java:85)
        at org.elasticsearch.common.inject.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:98)
        at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:45)
        at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:837)
        at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:42)
        at org.elasticsearch.common.inject.Scopes$1$1.get(Scopes.java:57)
        at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:45)
        at org.elasticsearch.common.inject.SingleParameterInjector.inject(SingleParameterInjector.java:42)
        at org.elasticsearch.common.inject.SingleParameterInjector.getAll(SingleParameterInjector.java:66)
        at org.elasticsearch.common.inject.ConstructorInjector.construct(ConstructorInjector.java:85)
        at org.elasticsearch.common.inject.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:98)
        at org.elasticsearch.common.inject.FactoryProxy.get(FactoryProxy.java:52)
        at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:45)
        at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:837)
        at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:42)
        at org.elasticsearch.common.inject.Scopes$1$1.get(Scopes.java:57)
        at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:45)
        at org.elasticsearch.common.inject.SingleParameterInjector.inject(SingleParameterInjector.java:42)
        at org.elasticsearch.common.inject.SingleParameterInjector.getAll(SingleParameterInjector.java:66)
        at org.elasticsearch.common.inject.ConstructorInjector.construct(ConstructorInjector.java:85)
        at org.elasticsearch.common.inject.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:98)
        at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:45)
        at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:837)
        at org.elasticsearch.common.inject.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:42)
        at org.elasticsearch.common.inject.Scopes$1$1.get(Scopes.java:57)
        at org.elasticsearch.common.inject.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:45)
        at org.elasticsearch.common.inject.InjectorBuilder$1.call(InjectorBuilder.java:200)
        at org.elasticsearch.common.inject.InjectorBuilder$1.call(InjectorBuilder.java:193)
        at org.elasticsearch.common.inject.InjectorImpl.callInContext(InjectorImpl.java:830)
        at org.elasticsearch.common.inject.InjectorBuilder.loadEagerSingletons(InjectorBuilder.java:193)
        at org.elasticsearch.common.inject.InjectorBuilder.injectDynamically(InjectorBuilder.java:175)
        at org.elasticsearch.common.inject.InjectorBuilder.build(InjectorBuilder.java:110)
        at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:93)
        at org.elasticsearch.common.inject.Guice.createInjector(Guice.java:70)
        at org.elasticsearch.common.inject.ModulesBuilder.createInjector(ModulesBuilder.java:59)
        at org.elasticsearch.node.internal.InternalNode.<init>(InternalNode.java:192)
        at org.elasticsearch.node.NodeBuilder.build(NodeBuilder.java:159)
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:70)
        at org.elasticsearch.bootstrap.Bootstrap.main(Bootstrap.java:203)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:32)
Caused by: java.net.UnknownHostException: elasticsearchtest: Name or service not known
        at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
        at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901)
        at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293)
        at java.net.InetAddress.getLocalHost(InetAddress.java:1469)
        ... 62 more
[2014-09-18 08:56:16,937][INFO ][node                     ] [Acrobat] initialized
[2014-09-18 08:56:16,937][INFO ][node                     ] [Acrobat] starting ...
[2014-09-18 08:56:17,110][INFO ][transport                ] [Acrobat] bound_address {inet[/0:0:0:0:0:0:0:0:9300]}, publish_address {inet[/143.79.236.31:9300]}
[2014-09-18 08:56:17,126][INFO ][discovery                ] [Acrobat] elastictest/QvSNFajjQ9SFjU7WOdjaLw
[2014-09-18 08:56:20,145][INFO ][cluster.service          ] [Acrobat] new_master [Acrobat][QvSNFajjQ9SFjU7WOdjaLw][localhost][inet[/143.79.236.31:9300]], reason: zen-disco-join (elected_as_master)
[2014-09-18 08:56:20,212][INFO ][http                     ] [Acrobat] bound_address {inet[/0:0:0:0:0:0:0:0:9200]}, publish_address {inet[/143.79.236.31:9200]}
[2014-09-18 08:56:20,214][INFO ][node                     ] [Acrobat] started

- elasticsearch.yml中的群集配置 -

################################### Cluster ###################################

# Cluster name identifies your cluster for auto-discovery. If you're running
# multiple clusters on the same network, make sure you're using unique names.
#
cluster.name: elastictest

19 个答案:

答案 0 :(得分:34)

Elasticsearch设置位于$ES_HOME/config/elasticsearch.yml。在那里,如果注释掉cluster.name设置,则意味着ES将采用任何群集名称。因此,在您的代码中,cluster.name"elastictest"可能是问题所在。试试这个:

Client client = new TransportClient()
                .addTransportAddress(new InetSocketTransportAddress(
                        "143.79.236.xxx",
                        9300));

答案 1 :(得分:31)

可能的问题:

  1. 错误的端口,如果您使用Java或Scala客户端,正确的端口是9300,而不是9200
  2. 错误的群集名称,请确保您在代码中设置的群集名称与您在$ES_HOME/config/elasticsearch.yml
  3. 中设置的cluster.name相同
  4. 嗅探选项,将client.transport.sniff设置为true但无法连接到ES群集的所有节点也会导致此问题。 ES doc here解释了原因。

答案 2 :(得分:14)

您应该检查节点的端口,您可以使用head进行检查。 这些端口不相同。例如,

您可以打开的网址是localhost:9200, 但节点的端口为9300,因此如果您使用9200作为端口,则所有已配置的节点都不可用。

答案 3 :(得分:4)

面临类似的问题,这是解决方案

示例:

  1. 在elasticsearch.yml中添加以下属性

    cluster.name: production
    node.name: node1
    network.bind_host: 10.0.1.22
    network.host: 0.0.0.0
    transport.tcp.port: 9300
    
  2. 在批量推送的Java Elastic API中添加以下内容(只是一个代码片段)。 对于IP地址,添加弹性搜索机的公共IP地址

    Client client;
    BulkRequestBuilder requestBuilder;
    try {
        client = TransportClient.builder().settings(Settings.builder().put("cluster.name", "production").put("node.name","node1")).build().addTransportAddress(
        new InetSocketTransportAddress(InetAddress.getByName(""), 9300));
        requestBuilder = (client).prepareBulk();
    } 
    catch (Exception e) {
    }
    
  3. 打开防火墙端口9200,9300

答案 4 :(得分:4)

NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{UfB9geJCR--spyD7ewmoXQ}{192.168.1.245}{192.168.1.245:9300}]]

就我而言,这是版本的差异。如果您查看elasticsearch集群中的日志,将会看到。

Elasticsearch日志

[node1] exception caught on transport layer [NettyTcpChannel{localAddress=/192.168.1.245:9300, remoteAddress=/172.16.1.47:65130}], closing connection
java.lang.IllegalStateException: Received message from unsupported version: [5.0.0] minimal compatible version is: [5.6.0]

我正在使用elasticsearch客户端和传输版本5.1.1。我的Elasticsearch集群版本为6。所以我将库版本更改为5.4.3。

答案 5 :(得分:3)

为了完成起见,这里是使用InetSocketTransportAddress提供的适当静态方法创建传输客户端的代码段:

    Client esClient = TransportClient.builder()
        .settings(settings)
        .build()
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("143.79.236.xxx"), 9300));

答案 6 :(得分:3)

如果上述建议不适合您,请将日志框架配置(log4j,logback ...)的日志级别更改为 INFO 。然后重新检查输出。

记录器可能隐藏了以下消息:

HH

(在上面的示例中,ElasticSearch中有X-Pack插件需要身份验证)

答案 7 :(得分:2)

其他用户遇到此问题。

如果在运行Spring Boot&lt; 2版本时运行较新的ElasticSearch(5.5或更高版本),则可能会出现此错误。

建议使用REST客户端,因为不推荐使用Java客户端。

其他解决方法是升级到Spring Boot 2,因为它应该是兼容的。

有关详细信息,请参阅https://discuss.elastic.co/t/spring-data-elasticsearch-cant-connect-with-elasticsearch-5-5-0/94235

答案 8 :(得分:2)

由于大多数答案似乎都已过时,因此这是对我有用的设置:

  • Elasticsearch版本::在Docker上运行的7.2.0(OSS)
  • Java版本: JDK-11

elasticsearch.yml:

{
"from": 0,
"size": 1000,
"query": {
    "constant_score": {
        "filter": {
            "bool": {
                "must": [
                    {
                        "exists": {
                            "field": "metric_value"
                        }
                    },
                    {
                        "term": {
                            "metric_value_type.keyword": {
                                "value": "Outage",
                                "boost": 1
                            }
                        }
                    },
                    {
                        "terms": {
                            "city.keyword": [
                                "LONDON"
                            ],
                            "boost": 1
                        }
                    },
                    {
                        "range": {
                            "metric_timestamp": {
                                "from": "2019-08-01T08:30:00.000Z",
                                "to": "2019-08-19T14:00:00.000Z",
                                "include_lower": true,
                                "include_upper": true,
                                "boost": 1
                            }
                        }
                    }
                ],
                "adjust_pure_negative": true,
                "boost": 1
            }
        },
        "boost": 1
    }
},
"aggregations": {
    "missing_metric_values_aggregation": {
        "aggregations": {
            "groupby_aggregation": {
                "terms": {
                    "field": "cpe_name.keyword",
                    "size": 20000,
                    "min_doc_count": 1,
                    "shard_min_doc_count": 0,
                    "show_term_doc_count_error": false,
                    "order": [
                        {
                            "_count": "desc"
                        },
                        {
                            "_key": "asc"
                        }
                    ]
                }
            }
        }
    }
}

设置:

cluster.name: production
node.name: node1
network.host: 0.0.0.0
transport.tcp.port: 9300
cluster.initial_master_nodes: node1

由于不赞成使用PreBuiltTransportClient,因此您应将RestHighLevelClient用于Elasticsearch-Version 7.3.0:https://artifacts.elastic.co/javadoc/org/elasticsearch/client/elasticsearch-rest-high-level-client/7.3.0/index.html

答案 9 :(得分:1)

检查你的elasticsearch.yml,“transport.host”属性必须是“0.0.0.0”而不是“127.0.0.1”或“localhost”

答案 10 :(得分:1)

我花了几天时间解决这个问题。我知道已经晚了,但这可能会有所帮助:

我通过更改以下版本的兼容/稳定版本解决了该问题:

Spring boot: 2.1.1
Spring Data Elastic: 2.1.4
Elastic: 6.4.0 (default)

行家:

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</dependency>

您无需提及弹性版本。默认情况下为6.4.0。但是,如果要添加特定版本。在属性标签内使用以下代码片段,并使用兼容版本的Spring Boot和Spring Data(如果需要)

<properties>
<elasticsearch.version>6.8.0</elasticsearch.version>
</properties>

此外,我在ElasticConfiguration中使用了Rest High Level客户端:

@Value("${elasticsearch.host}")
public String host;

@Value("${elasticsearch.port}")
public int port;

@Bean(destroyMethod = "close")
public RestHighLevelClient restClient1() {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
    RestHighLevelClient client = new RestHighLevelClient(builder);
    return client;
    }
}

重要说明: 弹性使用9300端口在节点之间进行通信,并使用9200作为HTTP客户端。在应用程序属性中:

elasticsearch.host=10.40.43.111
elasticsearch.port=9200

spring.data.elasticsearch.cluster-nodes=10.40.43.111:9300 (customized Elastic server)
spring.data.elasticsearch.cluster-name=any-cluster-name (customized cluster name)

在Postman中,您可以使用:http://10.40.43.111:9200/[indexname]/_search

快乐编码:)

答案 11 :(得分:1)

检查ES服务器日志

sudo tail -f /var/log/elasticsearch/elasticsearch.log

我正在使用过时的客户端

Received message from unsupported version: [5.0.0] minimal compatible version is: [6.8.0]

答案 12 :(得分:0)

这意味着我们无法实例化ES transportClient并抛出此异常。有几种可能性会导致此问题。

  • 群集名称不正确。因此打开ES_HOME_DIR/config/elasticserach.yml文件并检查群集名称值或使用此命令:curl -XGET 'http://localhost:9200/_nodes'
  • 验证端口9200是http端口,但elasticsearch服务正在使用tcp端口9300 [默认]。因此,请验证端口是否未被阻止。
  • 身份验证问题:在transportClient的上下文中设置标头以进行身份​​验证:

    client.threadPool().getThreadContext()
      .putHeader("Authorization", "Basic " + encodeBase64String(basicHeader.getBytes()));
    

如果您仍然遇到此问题,请添加以下属性:

put("client.transport.ignore_cluster_name", true)

以下基本代码对我来说很好:

Settings settings = Settings.builder()
            .put("cluster.name", "my-application").put("client.transport.sniff", true).put("client.transport.ignore_cluster_name", false).build();
    TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));

答案 13 :(得分:0)

如果您正在使用Java Transport Client 1.检查9300是否可以访问/打开。 2.检查节点和群集名称,这应该是正确的,您可以通过在浏览器中键入ip:port来检查节点和群集名称。 3.检查您的jar版本和Es安装版本。

答案 14 :(得分:0)

更改版本的ElasticSearch对我有用。

答案 15 :(得分:0)

这个在ES 1.7.5中对我有用:

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;

public static void main(String[] args) throws IOException  {
    Settings settings = ImmutableSettings.settingsBuilder()
            .put("client.transport.sniff",true)
            .put("cluster.name","elasticcluster").build();

    Client client = new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress("[ipaddress]",9300));
    XContentBuilder builder = null;
    try {
        builder = jsonBuilder().startObject().field("user", "testdata").field("postdata",new Date()).field("message","testmessage")
                .endObject();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println(builder.string());

    IndexResponse response = client.prepareIndex("twitter","tweet","1").setSource(builder).execute().actionGet();

    client.close();

}

答案 16 :(得分:0)

如果上面的答案不起作用,我知道我来晚了,我建议在elasticsearch终端中检查日志。我发现错误消息说我需要将版本从5.0.0-rc1更新为6.8.0,我通过将maven依赖项更新为以下内容来解决了该问题:

<dependency>
  <groupId>org.elasticsearch</groupId>
  <artifactId>elasticsearch</artifactId>
  <version>6.8.0</version>
</dependency>
  <dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>transport</artifactId>
  <version>6.8.0</version>
</dependency>

这也更改了我的代码,因为InetSocketTransportAddress已过时。我必须将其更改为TransportAddress

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
               .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));

您还需要将此文件添加到您的 config / elasticsearch.yml 文件中(使用您的主机地址)

transport.host: localhost

答案 17 :(得分:0)

我有同样的问题。我的问题是依赖项的版本与elasticsearch版本冲突。检查ip:9200中的版本并使用与其匹配的依赖项版本

答案 18 :(得分:-1)

您应该检查日志 如果您看到如下图 “ stacktrace”:[“ java.lang.IllegalStateException:从不支持的版本收到的消息:[6.4.3]最小兼容版本是:[6.8.0]”

您可以检查此链接 https://discuss.elastic.co/t/java-client-or-spring-boot-for-elasticsearch-7-3-1/199778 您必须显式声明es版本。