负载均衡算法--最小连接数法(Least Connections) 旧城等待, 2022-01-29 23:25 866阅读 0赞 接上一篇博文:[负载均衡算法–源地址哈希法(Hash)][Hash]。 最小连接数法是根据服务器当前的连接情况进行负载均衡的,当请求到来时,会选取当前连接数最少的一台服务器来处理请求。由此也可以延伸出,根据服务器 CPU 占用最少,根据单位时间内处理请求的效率高低等进行服务器选择。最小连接数法只是动态分配服务器的一种算法,通过各种维度的参数计算,可以找到适合不同场景的更均衡的动态分配服务器的方案。 ### 算法描述 ### 假设有 N 台服务器 S = \{S0, S1, S2, …, Sn\},算法可以描述为: 1、实时记录每台服务器的连接数 2、查找当前连接数最小的服务器 假定我们现在有如下四台服务器: <table> <thead> <tr> <th>服务器地址</th> <th>连接数</th> </tr> </thead> <tbody> <tr> <td>192.168.1.1</td> <td>1</td> </tr> <tr> <td>192.168.1.2</td> <td>2</td> </tr> <tr> <td>192.168.1.3</td> <td>3</td> </tr> <tr> <td>192.168.1.4</td> <td>4</td> </tr> </tbody> </table> 进行 10 次访问后, 被分配的服务器如下: <table> <thead> <tr> <th>服务器地址</th> <th>序号</th> <th>连接数</th> </tr> </thead> <tbody> <tr> <td>192.168.1.1</td> <td>1</td> <td>2</td> </tr> <tr> <td>192.168.1.1</td> <td>1</td> <td>3</td> </tr> <tr> <td>192.168.1.2</td> <td>2</td> <td>3</td> </tr> <tr> <td>192.168.1.1</td> <td>1</td> <td>4</td> </tr> <tr> <td>192.168.1.2</td> <td>2</td> <td>4</td> </tr> <tr> <td>192.168.1.2</td> <td>3</td> <td>4</td> </tr> <tr> <td>192.168.1.1</td> <td>1</td> <td>5</td> </tr> <tr> <td>192.168.1.2</td> <td>2</td> <td>5</td> </tr> <tr> <td>192.168.1.2</td> <td>3</td> <td>5</td> </tr> <tr> <td>192.168.1.3</td> <td>4</td> <td>5</td> </tr> </tbody> </table> ### 代码实现 ### 1、服务器连接数 bean 类 package org.learn.loadbalance; import java.io.Serializable; public class ConnectionsServer implements Serializable { private static final long serialVersionUID = 7246747589293111189L; private String server; private Integer connnections; public ConnectionsServer(String server, Integer connnections){ this.server = server; this.connnections = connnections; } public String getServer() { return server; } public void setServer(String server) { this.server = server; } public Integer getConnnections() { return connnections; } public void setConnnections(Integer connnections) { this.connnections = connnections; } } 1、服务器管理类 package org.learn.loadbalance; import java.util.Map; import java.util.TreeMap; /** * @author zhibo * @date 2019/5/16 16:25 */ public class ConnectionsServerManager { public volatile static Map<String, ConnectionsServer> serverMap = new TreeMap<>(); static { serverMap.put("192.168.1.1", new ConnectionsServer("192.168.1.1",1)); serverMap.put("192.168.1.2", new ConnectionsServer("192.168.1.2",2)); serverMap.put("192.168.1.3", new ConnectionsServer("192.168.1.3",3)); serverMap.put("192.168.1.4", new ConnectionsServer("192.168.1.4",4)); } } 2、最小连接数类 package org.learn.loadbalance; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; /** * @author zhibo * @date 2019/5/16 16:28 */ public class LeastBalance { public static String getServer() { Map<String, ConnectionsServer> serverMap = new TreeMap<>(ConnectionsServerManager.serverMap); Iterator<String> iterator = serverMap.keySet().iterator(); ConnectionsServer minConnectionsServer = null; while (iterator.hasNext()){ ConnectionsServer server = serverMap.get(iterator.next()); if(minConnectionsServer == null){ minConnectionsServer = server; } if(minConnectionsServer.getConnnections() > server.getConnnections()){ minConnectionsServer = server; } } minConnectionsServer.setConnnections(minConnectionsServer.getConnnections() + 1); ConnectionsServerManager.serverMap.put(minConnectionsServer.getServer(), minConnectionsServer); System.out.println(String.format("ip=%s, connections=%s",minConnectionsServer.getServer(), minConnectionsServer.getConnnections())); return minConnectionsServer.getServer(); } public static void main(String[] args) { for (int i = 0; i < 10; i++) { String server = getServer(); } } } 执行 main 方法输出结果如下: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly96aGliby5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70] 最小连接数法是一个简单的动态分配服务器的方案,它提供了一个站在服务器角度思考动态分配服务器的简单方案。在实际的使用过程中,通过单一的连接数进行服务器运行状态的判定可能不太科学的,还需要针对使用场景进行分析。 文章内容仅代表个人观点,如有不正之处,欢迎批评指正,谢谢大家。 [Hash]: https://zhibo.blog.csdn.net/article/details/90288865 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly96aGliby5ibG9nLmNzZG4ubmV0_size_16_color_FFFFFF_t_70]: /images/20220130/13de1f03b7344b4d8983650ced20dd3a.png
还没有评论,来说两句吧...