通道长连接分配问题

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]
相关推荐
xiaoming001819 分钟前
JAVA项目打包部署运维全流程(多服务、批量)
java·linux·运维
拾-光37 分钟前
【Git】命令大全:从入门到高手,100 个最常用命令速查(2026 版)
java·大数据·人工智能·git·python·elasticsearch·设计模式
无人不xiao1 小时前
springBoot 实现 接口进度条
java·spring boot·后端
pkowner1 小时前
若依分页问题及解决方法
java·前端·算法
largecode2 小时前
如何让电话显示店名?来电显示店铺名称,提升有效接通率
java·开发语言·spring·百度·学习方法·业界资讯·twitter
xuhaoyu_cpp_java2 小时前
SpringMVC学习(五)
java·开发语言·经验分享·笔记·学习·spring
计算机安禾2 小时前
【c++面向对象编程】第22篇:输入输出运算符重载:<< 与 >> 的友元实现
java·前端·c++
旷世奇才李先生2 小时前
Java虚拟线程原理与实践
java
heimeiyingwang2 小时前
【架构实战】RPC框架Dubbo3.0:高性能Java通信之道
java·rpc·架构
i220818 Faiz Ul2 小时前
宠物猫之猫咖管理系统|基于java + vue宠物猫之猫咖管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·宠物猫之猫咖管理系统