通道长连接分配问题

netty服务端给了N个连接,我是客户端,有M个服务器,想要让N个连接平均分配在M个服务器上。例如,如果N为5,M为3,则最好是两台台服务器2个连接、一台服务器1个连接。N为1,M为3的话,1台服务器1个连接,其他服务器0个连接。算法要稳定,即如果N和M固定的话每次计算的结果要是一样的。可以借助服务器内网ip作为服务器标识。

请给出java算法,和不同的N和M值,对应的不同的结果。

java 复制代码
import java.util.*;

public class ConnectionDistributor {

    public static Map<String, List<Integer>> distributeConnections(List<String> serverIps, int connectionCount) {
        Map<String, List<Integer>> distribution = new HashMap<>();
        int serverCount = serverIps.size();
        
        // Initialize the map with empty lists for each server IP
        for (String ip : serverIps) {
            distribution.put(ip, new ArrayList<>());
        }

        for (int i = 0; i < connectionCount; i++) {
            // Use the modulo operation to ensure even distribution
            String server = serverIps.get(i % serverCount);
            distribution.get(server).add(i + 1); // Connection IDs are 1-based
        }

        return distribution;
    }

    public static void main(String[] args) {
        List<String> serverIps = Arrays.asList("192.168.0.1", "192.168.0.2", "192.168.0.3");

        int[] connectionCounts = {1, 5, 10, 15, 20};
        for (int connectionCount : connectionCounts) {
            Map<String, List<Integer>> result = distributeConnections(serverIps, connectionCount);
            System.out.println("For " + connectionCount + " connections:");
            for (Map.Entry<String, List<Integer>> entry : result.entrySet()) {
                System.out.println("Server " + entry.getKey() + " has connections: " + entry.getValue());
            }
            System.out.println();
        }
    }
}

执行结果

java 复制代码
For 1 connections:
Server 192.168.0.2 has connections: []
Server 192.168.0.1 has connections: [1]
Server 192.168.0.3 has connections: []

For 5 connections:
Server 192.168.0.2 has connections: [2, 5]
Server 192.168.0.1 has connections: [1, 4]
Server 192.168.0.3 has connections: [3]

For 10 connections:
Server 192.168.0.2 has connections: [2, 5, 8]
Server 192.168.0.1 has connections: [1, 4, 7, 10]
Server 192.168.0.3 has connections: [3, 6, 9]

For 15 connections:
Server 192.168.0.2 has connections: [2, 5, 8, 11, 14]
Server 192.168.0.1 has connections: [1, 4, 7, 10, 13]
Server 192.168.0.3 has connections: [3, 6, 9, 12, 15]

For 20 connections:
Server 192.168.0.2 has connections: [2, 5, 8, 11, 14, 17, 20]
Server 192.168.0.1 has connections: [1, 4, 7, 10, 13, 16, 19]
Server 192.168.0.3 has connections: [3, 6, 9, 12, 15, 18]
相关推荐
·薯条大王6 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
ZJH__GO9 小时前
网络编程v4pro--实现聊天室文件传输功能
java·服务器·网络·计算机网络
程序员黑豆9 小时前
Windows 系统 Java 环境变量配置全攻略:解决“不是内部或外部命令”
java·前端·ai编程
命运之光9 小时前
【C语言完整代码】就诊信息管理系统
java·c语言·开发语言
凤山老林9 小时前
Spring Boot @Async 线上实战:从默认配置到生产级线程池治理
java·spring boot·后端
marvelyu10 小时前
每天10分钟学会OceanBase系列(Day 20):跨机房容灾实战——构建多数据中心高可用架构
java·大数据·数据库
AI人工智能+电脑小能手11 小时前
大白话说Java设计模式-04-工厂方法模式(业务实战篇)
java·设计模式·工厂方法模式·架构设计·代码解耦
BUG指挥官11 小时前
Sa-Token和Spring Security对比
java·后端·spring
小当家.10511 小时前
深入理解 ReAct Agent:从原理到 Java 实战
java·react.js·agent·react·架构设计·agent设计
geminigoth11 小时前
Spring AI Alibaba 入门开发一(备份)
java·人工智能·spring