2013-12-16 66 views
1

由於應用程序使用ElasticSearch的JavaAPI傳輸客戶端,因此我希望在類路徑中使用本地elasticsearch.yml(由InternalSettingsPreparer解析)配置文件以通過傳輸連接到羣集。配置文件中的ElasticSearch Java API傳輸客戶端地址

現在我有以下內容:

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

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

和客戶端的初始化:

TransportClient client = new TransportClient(); 

這使我異常:

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

但是當我添加以下line

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

事情開始按預期工作。

所以我想知道,是否有一種方法來配置標準配置文件格式的傳輸地址,而不是通過配置文件的配置文件重新發明輪子?

回答

4

使用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} 

而且,我通常使用以下格式我elasticsearch.yml文件

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

不起作用依然:org.elasticsearch.client.transport.NoNodeAvailableException:沒有可用的節點 \t在org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:212) –

+0

我測試了這個本地和有類似的錯誤,直到我修改端口爲一個範圍:9300-9400我檢查了文檔和端口設置應該是一個範圍 - http://www.elasticsearch.org/guide/en/elasticsearch/reference/ current/modules-transport.html它默認爲9300-9400,這對您應該足夠了。因此,您可以將9300-9400的值或刪除它,因爲您已經使用了默認值。 –

+0

仍然沒有成功: // java代碼: TransportClient client = new TransportClient(); ();(); prepareState()。execute()。actionGet(); //配置/ elastisearch.yml cluster.name:elasticsearch network.host:本地主機 network.transport.tcp.port:9300-9400 仍然給出 「無可用節點」 異常 附: es-0.90.7 –

3

簡短的回答是,沒有,Elasticsearch沒有辦法在使用傳輸客戶端時配置默認的網絡地址集來加入。

但是,添加對此的支持相當簡單,同時仍然通過將其添加到配置中的任何位置來搭載默認配置加載等。例如,在elasticsearch.yml

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)); 
     } 

     // ... 
    } 
} 
4

我總稱爲

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)); 

這導致:

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

可以通過下面的代碼,其包括用於可選的端口號一些額外的解析裝in: org.elasticsearch.client.transport.NoNodeAvailableException:無節點可用

在日誌我發現: bound_address {INET [/ 0:0:0:0:0:0:0:0:9210]},{publish_address INET [/172.00.71.128:9200]}

但是,我在傳輸層[[id:0x0fd1380f,/127.0.0.1:53090 => /127.0.0.1:9300]]上捕獲了[transport.netty] [Sabra]異常,並關閉了連接 java.io.IOException異常:EINE vorhandene Verbindung wurde VOM REMOTEHOST geschlossen

尋找一個修復這個相當長的時間自己後,我終於找到了這個工作對我來說:

當你調用http://localhost:9200/_cluster/state我注意到,只有我的本地IP地址是綁定,而不是本地主機回送。

更改我的TransportAddress配置

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

終於解決了我的問題。希望這有助於

相關問題