基于百分比分配算法的负载均衡器 [英] Load balancer based on percentage distribution algorithm

查看:75
本文介绍了基于百分比分配算法的负载均衡器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我具有以下实体:

public class Account {
    private String id;
    private List<Host> hosts;
}

public class Host {
    private String name;
    private int percentageLoad;
}

我有一个Web应用程序,其中只有一个端点/check ,其中 accountId 作为请求参数.

I have a web application with only one endpoint /check with an accountId as a request parameter.

请求的示例可以是: localhost:8080/check?accountId = 123456 .

该服务必须验证 accountId 是否存在于嵌入式数据库中,如果存在,它会使用主机名进行回答.这些主机具有负载百分比,应根据其负载百分比来平衡答案.

That service has to validate that accountId exists in an embedded database, and if so, it answers with a host's name. Those hosts have a load percentage and the answers should be balanced depending on their load percentage.

这可能会混淆我的解释,所以我举一个例子:

It may be confusing what I want to explain, so I will put an example:

假设我的数据库中有以下 Account :

Assume that I have in the database the following Account:

Account:
  - id: 123
  - hosts: [{machine1, 60}, {machine2, 40}]

您会看到该帐户的ID为 123 ,并且有两个主机,每个主机的负载百分比为一个.

As you can see that the account has 123 as id and two hosts with one percentage load for each one.

如果我收到以下请求: localhost:8080/check?accountId = 123 ,则由于该帐户ID存在于数据库中,因此它应根据负载百分比平衡主机的名称答案.

If I get the following request: localhost:8080/check?accountId=123, then as that account id exists in the database, it should balance the host's name answer depending on the percentage load.

更清楚一点,假设我发出了10个类似的请求: localhost:8080/check?accountId = 123 ,则主机名 machine1 应该返回6次(因为它的负载百分比为60%),并且 machine2 应该返回4次(因为它的负载百分比为40%).

More clear, let's say I make 10 similar requests: localhost:8080/check?accountId=123, then the hostname machine1 should be returned 6 times (as it has a load percentage of 60%), and machine2 should be returned 4 times (as it has a load percentage of 40%).

我真的不知道该采用哪种策略来平衡它.

I really don't know which strategy to apply in order to balance it.

我想到了一个可以计算每台机器答案数量的计数器.或选择一个介于0到10之间的随机数,然后根据该数字决定一台机器或另一台机器.但是我认为它们不是好的方法,关于平衡算法的任何想法吗?

I thought about a counter which counts the number of answers for each machine. Or pick a random number between 0 and 10 and decide one machine or another depending on that number. But I think they are not good approaches, any idea about an algorithm to balance that?

推荐答案

您将需要 Host 类中的另一个变量:

You'll need another variable in the Host class:

public class Host {
    private String name;
    private int percentageLoad;
    private int percentageAccum;
}

percentageAccum 的初始值是 percentageLoad 的值.

收到请求后:

  • 选择percentageAccum最大的主机
  • percentageAccum 中为所选主机减去100
  • 为所有主机(包括所选主机)
  • percentageLoad 添加到 percentageAccum
  • choose the host with the largest percentageAccum
  • subtract 100 from the percentageAccum for the chosen host
  • add percentageLoad to percentageAccum for all hosts, including the chosen host

略微优化是在更新所有主机的 percentageAccum 时确定下一步选择.

A slight optimization is to determine the next choice while updating the percentageAccum for all of the hosts.

这篇关于基于百分比分配算法的负载均衡器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆