当前位置: 首页 > news >正文

Java LoadBalanceUtil 负载均衡、轮询加权

负载均衡、轮询加权
LoadBalanceUtil

@Component
public class LoadBalanceUtil {private List<Node> nodes;/*** 初始化  约定的invoker和权重的键值对*/public void init(Map<Invoker, Integer> invokersWeight){if (invokersWeight != null && !invokersWeight.isEmpty()) {nodes = new ArrayList<>(invokersWeight.size());invokersWeight.forEach((invoker, weight) -> nodes.add(new Node(invoker, weight)));} else {nodes = null;}}/*** 算法逻辑:* 1. 对于每个请求,遍历集群中的所有可用后端,对于每个后端peer执行:*  peer->current_weight += peer->effecitve_weight。*  同时累加所有peer的effective_weight,保存为total。* 2. 从集群中选出current_weight最大的peer,作为本次选定的后端。* 3. 对于本次选定的后端,执行:peer->current_weight -= total。** @Return ivoker*/public Invoker select() {if (!checkNodes()) {return null;} else if (nodes.size() == 1) {if (nodes.get(0).invoker.isAvalable()) {return nodes.get(0).invoker;} else {return null;}}Integer total = 0;Node nodeOfMaxWeight = null;for (Node node : nodes) {total += node.effectiveWeight;node.currentWeight += node.effectiveWeight;if (nodeOfMaxWeight == null) {nodeOfMaxWeight = node;} else {nodeOfMaxWeight = nodeOfMaxWeight.compareTo(node) > 0 ? nodeOfMaxWeight : node;}}nodeOfMaxWeight.currentWeight -= total;return nodeOfMaxWeight.invoker;}private boolean checkNodes() {return (nodes != null && nodes.size() > 0);}public interface Invoker {Boolean isAvalable();String nodeName();}private static class Node implements Comparable<Node> {final Invoker invoker;final Integer weight;Integer effectiveWeight;Integer currentWeight;Node(Invoker invoker, Integer weight) {this.invoker = invoker;this.weight = weight;this.effectiveWeight = weight;this.currentWeight = 0;}@Overridepublic int compareTo(Node o) {return currentWeight > o.currentWeight ? 1 : (currentWeight.equals(o.currentWeight) ? 0 : -1);}}}

invoker

if (totalCount > 0) {//根据计算结果修改权重for (LoadBalanceUtil.Invoker key : invokersWeight.keySet()) {if (weightMap.containsKey(key.nodeName())) {invokersWeight.put(key, weightMap.get(key.nodeName()));}}//初始化权重loadBalanceUtil.init(invokersWeight);//根据AI空闲数量,获取待分配任务List<TaskInfo> waitAssignList = scheduleService.listWaitAllocate(totalCount);if (ObjectUtil.isEmpty(waitAssignList)) {//没数据,停30秒。Thread.sleep(cronInterval * 1000);return ReturnT.SUCCESS;}for (TaskInfo item : waitAssignList) {LoadBalanceUtil.Invoker invoker = loadBalanceUtil.select();item.setAiServerName(invoker.nodeName());item.setStatus(ReportStatus.ALLOCATED.getStatus());item.setAllocationTime(new Date());scheduleService.allocationServer(item); String logMsg = StrUtil.format("{} 分配给 {}", item.getCustodyNo(), invoker.nodeName());logger.info(logMsg);}
}
http://www.jsqmd.com/news/490408/

相关文章:

  • 墨语灵犀辅助C语言学习:代码解释与调试实践指南
  • SecGPT-14B实操手册:利用Gradio历史消息功能构建持续进化的安全知识库
  • 【实战】驾驭n8n:构建企业级自动化中枢,解锁AI工作流新范式
  • LenovoLegionToolkit技术攻关:Legion 9风扇控制功能异常的创新解决路径
  • 优质血液净化设备推荐—健帆生物DX-10与Future F20详解 - 品牌2026
  • Youtu-Parsing镜像免配置:预装supervisor+webui.py+模型缓存,启动即服务
  • 2026年3月国内八大土工布厂家解析推荐:土工膜、排水板、雨水收集系统 - 深度智识库
  • Flink项目踩坑记:如何快速解决Scala版本不兼容导致的NoSuchMethodError
  • Qwen3-14B开源可部署方案:完全离线运行的int4 AWQ文本生成服务
  • 2026年流量传感器技术解析与市场主流品牌定位分析 - 品牌推荐大师
  • Terraform之locales模块
  • Qwen3-14B开源大模型部署教程:int4 AWQ量化版vLLM服务搭建与日志排查
  • 2026年国军标钛锻件权威评测报告 - 优质品牌商家
  • 重新定义Lenovo Legion Toolkit的价值:从核心痛点到场景化解决方案
  • 【IEEE会议】2026年IEEE第八届软件工程和计算机科学国际会议(CSECS 2026)
  • org.springframework.security.access.AccessDeniedException 不允许访问
  • Phi-3-vision-128k-instruct快速上手:图文问答模型安全护栏测试与绕过分析
  • Excel导入批量创建多格式文件,这5个工具亲测实用!
  • Legion 9笔记本风扇控制功能异常问题深度解析与解决
  • iReport 5.6.0组件实战:从基础到高级报表设计全解析
  • 5个实战项目推荐:如何用微表情数据集训练你的第一个AI模型(附完整代码)
  • 新手必看:如何用F12在5分钟内破解SWPUCTF签到题(附完整步骤)
  • 代账公司票据多、效率慢?一套接口全面提速
  • 【2026最新】nexus3.90.x安装文件说明
  • MCP Sampling配置失效的终极元凶:不是代码,是这1个被忽略的TLS 1.3 ALPN协商参数
  • 保姆级教程:如何为你的Android项目选择正确的AGP版本(2024最新)
  • [agent memory] Diagnosing Retrieval vs. Utilization Bottlenecks in LLM Agent Memory
  • Speech Seaco Paraformer案例分享:如何用热词定制提升识别准确率
  • GTE中文向量模型部署指南:防火墙开放5000端口+SELinux配置实操
  • Endoscapes2024最新评测:YOLOv8在腹腔镜关键安全视图检测中的表现