NoNodeAvailableException[None of the configured nodes are available
Elasticsearch | 作者 ZDS19930211 | 发布于2017年10月23日 | 阅读数:4004package com.ai.slp.product.dubbo;
import java.net.InetSocketAddress;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import com.ai.opt.sdk.appserver.DubboServiceStart;
public class Test {
public static void main(String args) {
try{
//Settings settings = Settings.settingsBuilder().put("client.transport.ping_timeout", "60s").put("node.name", "test").build();
Settings settings = Settings.settingsBuilder().put("client.transport.ping_timeout", "60s").put("client.transport.sniff", "true").put("client.transport.ignore_cluster_name", "true").build();
//Settings settings = Settings.settingsBuilder().put("client.transport.ping_timeout", "60s").put("cluster.name", "testUp").build();
TransportClient client = TransportClient.builder().settings(settings).build();
client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("112.9.116.188",9700)));//云端
// client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("192.168.1.15",9700)));//本地
System.out.println(client);
SearchResponse response = client.prepareSearch().execute().actionGet();//如果连接的是注释的本地ip,该段代码不报错,..反之报错
System.out.println(response);
}catch(Exception e){
e.printStackTrace();
}
}
}
我在本地(192.168.1.15)和云端分别部署了一台服务器,当我执行上面的代码的时候,如果地址是用的本地,可以正确获取到ElasticSearch的索引,但是如果连接云端则获取不到,我用两种方法来解决了这个问题:1.是在云端启动服务器的时候,在Elasticsearch.yml里面添加了node.name=test,之后
Settings settings = Settings.settingsBuilder().put("client.transport.ping_timeout", "60s").put("node.name", "test").build();来获取settings,这样也可以获取到云端对应的索引
2.用该方式Settings settings = Settings.settingsBuilder().put("client.transport.ping_timeout", "60s").put("client.transport.sniff", "true").put("client.transport.ignore_cluster_name", "true").build();获取settings,但是需要去掉put("client.transport.sniff", "true")方可正确执行,参照改的。[/url]
我想问的是,用第2种方式在不修改Settings settings = Settings.settingsBuilder().put("client.transport.ping_timeout", "60s").put("client.transport.sniff", "true").put("client.transport.ignore_cluster_name", "true").build();的情况下如何成功的获取索引?
1 个回复
Cheetah
赞同来自: