load-balancing - 如何使用c#实现加权循环?

标签 load-balancing scheduler

如果我有一些服务器:192.168.100.1、192.168.100.2、192.168.100.3、192.168.100.4... 它们的权重为:5,1,2,3

我想实现负载均衡,但是如何使用 C# 实现加权循环?

最佳答案

假设您有服务器 abcd。并且您有相应的权重 5123。您可以通过以下方式进行加权循环:

Random rand = new Random(seed);

void processRequest(Request r){

    // assume rand.next() returns a uniformly distributed integer >= 0
    int i = rand.next() % 11; // 11 is sum of weights

    if(i <= 4)      // process r with server a
    else if(i == 5) // process r with server b
    else if(i <= 7) // process r with server c
    else            // process r with server d
}

rand.next() % 11 返回 [0, 10](含)范围内的均匀分布整数。我们使用服务器 a 处理请求,获取五个可能的值 [0, 4]。我们使用服务器 b 处理该请求,仅获取一个可能的值 5,依此类推。

特别注意您使用的特定随机方法和种子值。

关于load-balancing - 如何使用c#实现加权循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8494876/

相关文章:

windows - Windows schtasks 中 xml 文件中 ExecutionTimeLimit 的开关是什么?

operating-system - 什么是调度程序延迟?

ssl - 如何使用haproxy解密和加密HTTPS流量?

c# - WCF 服务在多服务器环境中不起作用

nginx - GCE 上的 Kubernetes : Ingress Timeout Configuration

java - Java 中的计划任务 - 使用哪个工具

azure - 如何通过 RDP 访问 Azure VMSS VM

nginx - Nginx/Web 服务器术语中的 "Reverse Proxy"和 "Load Balancing"是什么?

windows - 在 Windows 上获取当前登录的用户

java - 调度作业在集群上执行两次