简体   繁体   中英

ElasticSearch Java API TransportClient address in configuration file

Given the application using ElasticSearch's JavaAPI TransportClient, i want to use native elasticsearch.yml (resolved by InternalSettingsPreparer ) config file in class path to connect to cluster via transport.

For now i have the following contents:

cluster:
  name: elasticsearch # would be ${es.cluster.name}

network:
  host: localhost 
  transport_address: localhost:9301 # would be ${es.network.trasport}

and the initialisation of client:

TransportClient client = new TransportClient();

Which gives me exception:

13/12/16 14:04:40 INFO elasticsearch.plugins: [Hanna Levy] loaded [], sites []
org.elasticsearch.client.transport.NoNodeAvailableException: No node available

But when I add the following line

client.addTransportAddress(new InetSocketTransportAddress("localhost", 9301));

things begin to work as expected.

So i wonder, whether is there a way to configure transport addresses from standard configuration file format and not re-invent the wheel by having a config file for config file?

Use transport.tcp.port Should look like the following:

 cluster:
   name: elasticsearch # would be ${es.cluster.name}

 network:
   host: localhost 
   transport: 
     tcp:
       port: 9301 # would be ${es.network.transport.tcp.port}

Also, I typically use the following format for my elasticsearch.yml file

cluster.name=elasticsearch
network.host=localhost
network.transport.tcp.port=9301

I always called

Settings settings = ImmutableSettings.settingsBuilder()
                .put("cluster.name", "mycluster")
                .put("client.transport.sniff", true).build();

        // now try to connect with the TransportClient
        Client client = new TransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(
                        "localhost", httpPort));

which resulted in: org.elasticsearch.client.transport.NoNodeAvailableException: No node available

In the logs I found: bound_address {inet[/0:0:0:0:0:0:0:0:9210]}, publish_address {inet[/172.00.71.128:9200]}

But I got a [transport.netty] [Sabra] exception caught on transport layer [[id: 0x0fd1380f, /127.0.0.1:53090 => /127.0.0.1:9300]], closing connection java.io.IOException: Eine vorhandene Verbindung wurde vom Remotehost geschlossen

After searching for a fix to this quite a long time myself I finally found this working for me:

When you call http://localhost:9200/_cluster/state I noticed that only my local IP address was bound, not the localhost loopback.

Changing my TransportAddress configuration to

InetAddress IP = InetAddress.getLocalHost();
client = new TransportClient(settings)
    .addTransportAddress(new InetSocketTransportAddress(IP.getHostAddress(), 9300));

finally solved my issue. Hope this helps

The short answer is, no, Elasticsearch does not have a way to configure a default set of network addresses to join when using the transport client.

However, it's rather simple to add support for this while still piggybacking on the default configuration loading etc by adding it to anywhere you'd like in the configuration. For example, in elasticsearch.yml :

transport.client.initial_nodes: ["localhost:9301"]

can be loaded by the following code which includes some extra parsing for the optional port number:

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

public class Test {
    public static void main(String[] args) {
        TransportClient client = new TransportClient();

        for(String host: client.settings().getAsArray("transport.client.initial_nodes")) {
            int port = 9300;

            // or parse it from the host string...
            String[] splitHost = host.split(":", 2);
            if(splitHost.length == 2) {
                host = splitHost[0];
                port = Integer.parseInt(splitHost[1]);
            }

            client.addTransportAddress(new InetSocketTransportAddress(host, port));
        }

        // ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM