访问Elasticsearch集群方式
2016-05-08 本文已影响997人
jacksu在简书
访问集群方式.png
使用TransportClient的代码示例:
try {
int port = 9300;
Map<String, String> m = new HashMap<String, String>();
// 设置client.transport.sniff为true来使客户端去嗅探整个集群的状态,
//把集群中其它机器的ip地址加到客户端中,这样本地节点挂了,程序还可以正常工作
Settings settings = Settings.settingsBuilder().put(m).put("cluster.name", "elasticsearch").put("client.transport.sniff", true).build();
client = TransportClient.builder().settings(settings).build();
String ip = "";
InetAddress host = InetAddress.getLocalHost();
client.addTransportAddress(new InetSocketTransportAddress(host, port));
} catch (Exception e) {
e.printStackTrace();
}