通道长连接分配问题

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]
相关推荐
能摆一天是一天2 分钟前
JAVA Function
java
The Sheep 20232 分钟前
Dotnet-Dapper的用法
java·开发语言
SimonKing18 分钟前
百度统计、Google Analytics平替开源网站分析工具:Umami
java·后端·程序员
Juchecar23 分钟前
设计模式不是Java专属,其他语言的使用方法
java·python·设计模式
马克学长30 分钟前
SSM基于Java的医疗器械销售系统oy281(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·开发语言·用户管理·ssm 框架·医疗器械销售系统
Seven971 小时前
MyBatis 常见面试题
java·mybatis
我命由我123451 小时前
Android WebView - loadUrl 方法的长度限制
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
前端架构师-老李1 小时前
Maven安装以及环境变量配置(macOS)
java·macos·maven
带刺的坐椅1 小时前
(对标 Spring IA 和 LangChain4j)Solon AI & MCP v3.7.0, v3.6.4, v3.5.8 发布(支持 LTS)
java·spring·ai·solon·mcp·langchain4j
7澄11 小时前
深入解析 LeetCode 1572:矩阵对角线元素的和 —— 从问题本质到高效实现
java·算法·leetcode·矩阵·intellij-idea