节点选择
client通过轮训方式发送给被配置的节点,节点能够通过client初始化时候提供的节点过滤器进行过滤,当嗅探可用时候这是有用的,万一没有指定的master节点配http请求击中。对于每个请求,client将会运行最终配置节点过滤器去过滤节点候选者,然后再剩下的节点中选择下一个
RestClientBuilder builder = RestClient.builder(
new HttpHost("localhost", 9200, "http"));
builder.setNodeSelector(new NodeSelector() {
@Override
public void select(Iterable<Node> nodes) {
/*
* Prefer any node that belongs to rack_one. If none is around
* we will go to another rack till it's time to try and revive
* some of the nodes that belong to rack_one.
*/
boolean foundOne = false;
for (Node node : nodes) {
String rackId = node.getAttributes().get("rack_id").get(0);
if ("rack_one".equals(rackId)) {
foundOne = true;
break;
}
}
if (foundOne) {
Iterator<Node> nodesIt = nodes.iterator();
while (nodesIt.hasNext()) {
Node node = nodesIt.next();
String rackId = node.getAttributes().get("rack_id").get(0);
if ("rack_one".equals(rackId) == false) {
nodesIt.remove();
}
}
}
}
});
不会一直选择相同的节点的节点选择器将会进行不可预知和节能不公平的轮询的操作,这个上边的偏好实例是好的因为它关于节点可用性(它已经影响了可预测的轮询),节点选择不应该依靠额外的因素,否则轮询将不会合理的工作
嗅探
允许从一个运行着的Elasticsearh集群自动发现节点并设置他们到一个已经存在的rest client 实例的最小的库,它默认重试这些归属于集群的节点,这个集群使用节点信息api 并使用jackson去转化获得的json结果
与Elasticsearch 2.x及以上版本兼容

本文探讨了如何在Elasticsearch中通过节点选择器实现负载均衡,特别关注了轮询策略的改进,如偏好的节点选择和自动嗅探机制。节点过滤器确保了高可用性和可预测性,避免了不合理的轮询。
956

被折叠的 条评论
为什么被折叠?



