2014-09-10 31 views
0

在我的節點應用程序中,我使用「Elasticsearch」進行數據檢索。當我只連接到1臺主機時,它工作正常。node.js elasticsearch多個主機

爲前:

try { 
     elasticSearchClient = new ElasticSearchClient({ 
     host: 'xxx.xxx.x.xxxx', 
     port: '9200' 
    }); 
} catch (err) { 
    console.log('err=' + err); 
} 

但是當我試圖連接到多臺主機(例如)我得到錯誤:

對於前:

var serverOptions = { 
    hosts:[ 

     { 
      host: 'xxx.xxx.x.xxx', 
      port: 9200 
     },{ 
      host: 'xxx.xxx.x.xxx', 
      port: 9200 
     }] 
}; 

elasticSearchClient = new ElasticSearchClient(serverOptions); 

錯誤:

ECONN REFUSED: socket hang up 

我正在使用「elsaticsearchclient」節點包。幫我解決這個問題。在此先感謝..

編輯:

其實1主機已啓動並在另一臺主機宕機。所以當主機1關閉時,我不得不重定向到另一個主機..我如何實現這一目標?

回答

0

hosts應該是一個完整的URL數組。所以,做到以下幾點:

elasticSearchClient = elasticsearch.Client({ 
    hosts: serverOptions.hosts.map(function (server) { 
     return 'http://' + serverOptions.user + ':' + serverOptions.password + '@' + server.host + ':' + server.port; 
    }) 
}), 
+0

Thannks。但是我們用「elasticsearch」包,但我使用「elasticsearchclient」包..所以,如果你使用上面code.i不必須。對方法.. – Subburaj 2014-09-10 11:20:59

+0

你沒沒有提及你使用過哪個客戶端,所以我假定你使用的是官方模塊,而不是第三方。我建議你這樣做。 – Eye 2014-09-10 11:23:07

+0

K.我需要爲第三方軟件包做些什麼.. – Subburaj 2014-09-10 11:25:03