ElasticSearch Java API:NoNodeAvailableException:没有可用的节点

Joe*_*Joe 22 java elasticsearch

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();
}
Run Code Online (Sandbox Code Playgroud)

我从我的电脑访问服务器

curl -get http://XXX.XXX.XXX.XXX:9200/
Run Code Online (Sandbox Code Playgroud)

得到这个

{
    "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"
}
Run Code Online (Sandbox Code Playgroud)

为什么使用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
Run Code Online (Sandbox Code Playgroud)

hud*_*onb 34

一些建议:

1 - 使用端口9300. [9300-9400]用于节点到节点通信,[9200-9300]用于HTTP流量.

2 - 确保您使用的Java API版本与服务器上运行的elasticsearch版本相匹配.

3 - 确保群集的名称是foxzen(检查服务器上的elasticsearch.yml).

4 - 删除put("node.name", "yu"),您没有加入群集作为节点,因为您正在使用它TransportClient,即使您看起来您的服务器节点已命名,yu因此您在任何情况下都需要不同的节点名称.


Joh*_*one 10

您需要更改代码以使用端口9300 - 正确的行将是:

 Client client = new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress("XXX.XXX.XXX.XXX", 9300));
Run Code Online (Sandbox Code Playgroud)

原因是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();
Run Code Online (Sandbox Code Playgroud)