簡體   English   中英

ElasticSearch Java API:NoNodeAvailableException:沒有可用的節點

[英]ElasticSearch Java API:NoNodeAvailableException: No node available

public static void main(String[] args) throws IOException {
    Settings settings = ImmutableSettings.settingsBuilder()
            .put("cluster.name", "foxzen")
            .put("node.name", "yu").build();
    Client client = new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress("XXX.XXX.XXX.XXX", 9200));
            // XXX is my server's ip address
    IndexResponse response = client.prepareIndex("twitter", "tweet")
            .setSource(XContentFactory.jsonBuilder()
                    .startObject()
                    .field("productId", "1")
                    .field("productName", "XXX").endObject()).execute().actionGet();
    System.out.println(response.getIndex());
    System.out.println(response.getType());
    System.out.println(response.getVersion());
    client.close();
}

我從我的電腦訪問服務器

curl -get http://XXX.XXX.XXX.XXX:9200/

得到這個

{
    "status" : 200,
    "name" : "yu",
    "version" : {
        "number" : "1.1.0",
        "build_hash" : "2181e113dea80b4a9e31e58e9686658a2d46e363",
        "build_timestamp" : "2014-03-25T15:59:51Z",
        "build_snapshot" : false,
        "lucene_version" : "4.7"
    },
    "tagline" : "You Know, for Search"
}

為什么使用Java API會出錯?

編輯

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: foxzen


#################################### Node #####################################

# Node names are generated dynamically on startup, so you're relieved
# from configuring them manually. You can tie this node to a specific name:
#
node.name: yu

一些建議:

1 - 使用端口9300. [9300-9400]用於節點到節點通信,[9200-9300]用於HTTP流量。

2 - 確保您使用的Java API版本與服務器上運行的elasticsearch版本相匹配。

3 - 確保群集名稱為foxzen (檢查服務器上的foxzen )。

4 - 刪除put("node.name", "yu") ,因為您正在使用TransportClient ,所以您沒有將群集作為節點加入,即使您出現了,您的服務器節點也會被命名為yu因此您可能需要在任何情況下都是不同的節點名稱。

您需要更改代碼以使用端口9300 - 正確的行將是:

 Client client = new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress("XXX.XXX.XXX.XXX", 9300));

原因是Java API使用用於節點間通信的內部傳輸,默認為端口9300.端口92​​00是REST API接口的默認端口。 遇到的常見問題 - 請在此處查看此示例代碼,位於頁面底部的Transport Client下:

http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/client.html

// on startup

Client client = new TransportClient()
        .addTransportAddress(new InetSocketTransportAddress("host1", 9300))
        .addTransportAddress(new InetSocketTransportAddress("host2", 9300));

// on shutdown

client.close();

我也遇到了這個錯誤。 我使用ElasticSearch 2.4.1作為docker中的獨立服務器(單節點),使用Grails 3 / spring-data-elasticsearch進行編程。 我的修復是將client.transport.sniff設置為false 這是我的核心內容:

application.yml

spring.data.elasticsearch:
    cluster-name: "my-es"
    cluster-nodes: "localhost:9300"
    properties:
        "client.transport.ignore_cluster_name": true
        "client.transport.nodes_sampler_interval": "5s"
        "client.transport.ping_timeout": "5s"
        "client.transport.sniff": false      # XXX : notice here
    repositories.enabled: false

看到這個

我假設您在遠程主機上設置ES服務器? 在這種情況下,您需要將發布地址綁定到主機的公共IP地址。

在您的ES主機中編輯/etc/elasticsearch/elasticsearch.yml並在network.publish_host之后添加其公共IP:

# Set the address other nodes will use to communicate with this node. If not
# set, it is automatically derived. It must point to an actual IP address.
#
network.publish_host: 192.168.0.1

並在您的代碼中連接到端口9300上的此主機。請注意,您需要IP而不是域名(至少根據我在Amazon EC2上的經驗)

如果您仍然遇到問題,即使使用端口9300,並且其他所有內容似乎都配置正確,請嘗試使用舊版本的elasticsearch。

我在使用elasticsearch 2.2.0時遇到了同樣的錯誤,但是當我回滾到版本1.7.5時,我的問題神奇地消失了。 這是與其他人有這個問題的鏈接: 舊版本解決了問題

對於有類似問題的人,我收到了這個,因為我沒有在TransportClient構建器中設置cluster.name 添加了屬性,一切正常。

其他原因可能是,您的Elasticsearch Java客戶端Elasticsearch服務器的版本不同。

Elasticsearch Java客戶端版本只是您代碼庫中的彈性搜索 jar版本。

例如:在我的代碼中,它是elasticsearch-2.4.0.jar

要驗證Elasticsearch服務器版本,

$ /Users/kkolipaka/elasticsearch/bin/elasticsearch -version
Version: 5.2.2, Build: f9d9b74/2017-02-24T17:26:45.835Z, JVM: 1.8.0_111

如您所見,我已下載最新版本的Elastic server 5.2.2,但忘記更新ES Java API客戶端版本2.4.0 https://www.elastic.co/guide/en/elasticsearch/client/java- API /電流/ client.html

另一種解決方案可能是將io.netty.netty-all明確地包含在項目依賴項中。

addTransportAddresses上,正在執行一個方法nodesSampler.sample() ,並且正在檢查添加的地址的可用性。 在我的情況下, try-catch塊吞下ConnectTransportException因為io.netty.channel.DefaultChannelId.newInstance()方法io.netty.channel.DefaultChannelId.newInstance() 因此添加的節點不被視為可用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM