如何访问elasticsearch 集群
发布网友
发布时间:2022-04-25 05:10
我来回答
共1个回答
热心网友
时间:2023-10-28 16:01
使用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();
}