Nginx 是否尊重具有一致性哈希的权重属性?

Does Nginx respect the weight attribute with consistent hashing?

更具体一点,这行得通吗?

upstream backend {
    hash $request_uri consistent;

    server backend1.example.com weight=1;
    server backend2.example.com weight=2;
}

backend2.example.com 会收到两倍的流量吗?

此外,如果更改权重或将另一台服务器添加到组合中,会发生什么情况。 "only few keys will be remapped" 还会成立吗?

The optional consistent parameter of the hash directive enables ketama consistent hash load balancing. Requests will be evenly distributed across all upstream servers based on the user-defined hashed key value. If an upstream server is added to or removed from an upstream group, only few keys will be remapped which will minimize cache misses in case of load balancing cache servers and other applications that accumulate state.

来自 https://www.nginx.com/resources/admin-guide/load-balancer/

在此配置中,一致性哈希权重更重要。

换句话说,如果一个上游同时提供权重和一致性哈希,那么主要是一致性哈希

并根据权重将哈希分发到服务器。

upstream consistent_test {
    server consistent_test.example.ru:80 weight=90;
    server consistent_test2.example.ru:80 weight=10;
    hash $arg_consistent consistent;
}

实验

1) 默认状态

上游 balancer_test {

hash $arg_someid consistent;
server server1.example.ru:8080;
server server2.example.ru:8080;
server server3.example.ru:8080 down;

}

请求哈希固定到主机:

server1.example.ru ==> 535

server2.example.ru ==> 462

server3.example.ru ==> 0

2)第一步:启用节点并设置权重

上游 balancer_test {

hash $api_sessionid consistent;
server server1.example.ru:8080 weight=250;
server server2.example.ru:8080 weight=500;
server server3.example.ru:8080 weight=250;

}

请求哈希固定到主机:

server1.example.ru:8080 ==> 263

server2.example.ru:8080 ==> 473

server3.example.ru:8080 ==> 254

3) 第二步:完成流量转换,禁用旧节点

上游 balancer_test {

hash $api_sessionid consistent;
server1.example.ru:8080 down;
server2.example.ru:8080;
server3.example.ru:8080;

}

请求哈希固定到主机:

server1.example.ru:8080 ==> 0

server2.example.ru:8080 ==> 533

server3.example.ru:8080 ==> 464

server1.example.ru:

1) 之前 = 463

2) 在 step_2 = 533

3) 哈希命中数 = 306

server2.example.ru:

1) 之前 = 536

2) 在 step_1 = 263

3) 哈希命中 = 148

server3.example.ru:

1) 之前 = 255

2) 第 1 步 = 464

3) 哈希命中 = 115