在弹性集群模式下创建Java RestHighLevelClient [英] Create java RestHighLevelClient in elastic cluster mode
本文介绍了在弹性集群模式下创建Java RestHighLevelClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果elasticsearch在单一模式下运行,我可以使用以下代码轻松建立RestHighLevel连接:
If elasticsearch runs on single mode, I can easily establish the RestHighLevel connection with this line of code:
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
但是如果我的弹性集群有3台计算机,例如"host1","host2","host3",如何在集群模式下创建其余的高级客户端?
But if my elastic cluster has 3 machines, e.g., "host1", "host2", "host3", how to create the rest high level client in cluster mode ?
谢谢
推荐答案
要使用多个主机创建高级REST客户端,您可以执行以下操作:
To create High level REST client using multiple hosts, you can do something like following:
String[] esHosts = new String[]{"node1-example.com:9200", "node2-example.com:9200",
"node3-example.com:9200"};
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo(esHosts)
.build();
RestHighLevelClient restClient = RestClients.create(clientConfiguration).rest();
// Hostnames used for building client can be verified as following
List<Node> nodes = restClient.getLowLevelClient().getNodes();
nodes.forEach(node -> System.out.println(node.toString()));
参考文献: