简体   繁体   English

配置文件中的ElasticSearch Java API TransportClient地址

[英]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. 给定使用ElasticSearch的JavaAPI TransportClient的应用程序,我想在类路径中使用本机elasticsearch.yml (由InternalSettingsPreparer解析)配置文件通过传输连接到集群。

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: 使用transport.tcp.port应该如下所示:

 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 另外,我通常对我的elasticsearch.yml文件使用以下格式

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 结果是:org.elasticsearch.client.transport.NoNodeAvailableException:没有可用的节点

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]} 在我发现的日志中:bound_address {inet [/ 0: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 但是我在传输层上捕获了一个[transport.netty] [Sabra]异常[[id:0x0fd1380f,/127.0.0.1:53090 => /127.0.0.1:9300]],关闭了连接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. 当您调用http://localhost:9200/_cluster/state我注意到仅绑定了我的本地IP地址,而不绑定了本地主机回送。

Changing my TransportAddress configuration to 将我的TransportAddress配置更改为

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. 简短的答案是,不,Elasticsearch没有配置使用传输客户端时要加入的默认网络地址集的方法。

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. 但是,添加对此的支持非常简单,同时仍可以通过将默认配置加载等添加到配置中的任何位置来anywhere带默认配置加载。 For example, in elasticsearch.yml : 例如,在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));
        }

        // ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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