java 轮训_java 实现Nginx加权重轮训
代码灵感来源网站,看不懂代码先阅读网站:Nginx 加权轮训
public class WeightedRoundRobinSchedulingDemo {
static class Service {
private String ip;//ip地址
private Integer weight;//权重值
private Integer currentWeight;//当前权重轮训
private Integer effectiveWeight;//偏移权重
private Integer total;//总的权重数
public Service(String ip, Integer weight) {
this.weight = weight;
this.ip = ip;
this.effectiveWeight =this.weight;
this.currentWeight =0;
}
public Service() {
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public Integer getCurrentWeight() {
return currentWeight;
}
public void setCurrentWeight(Integer currentWeight) {
this.currentWeight = currentWeight;
}
public Integer getEffectiveWeight() {
return effectiveWeight;
}
public void setEffectiveWeight(Integer effectiveWeight) {
this.effectiveWeight = effectiveWeight;
}
}
public static Listservices =new ArrayList<>();
static {
services.add(new Service("192.168.1.1",6));
services.add(new Service("192.168.1.2",3));
services.add(new Service("192.168.1.3",1));
}
public static Service getService() {
HashSet set =new HashSet<>();
int sum =services.stream().mapToInt(Service::getEffectiveWeight).sum();//算出应该轮训多少次6+ 3 + 1
OptionalInt max =services.stream().mapToInt(Service::getEffectiveWeight).max();//选出每回权重最大的就是当前该选中的
for (int i =0; i
Service service =services.get(i);
if (service.getEffectiveWeight().equals(max.getAsInt()) && !set.contains(service.getEffectiveWeight())) {
service.setCurrentWeight(service.getEffectiveWeight()-sum);
set.add(service.getEffectiveWeight());
}else {
service.setCurrentWeight(service.getEffectiveWeight());
}
service.setEffectiveWeight(service.getWeight()+service.getCurrentWeight());
services.set(i, service);
}
Optional min =services.stream().min(Comparator.comparing(Service::getCurrentWeight));
return min.get();
}
public static void main(String[] args) {
for (int i =0; i <10; i++) {
Service service =getService();
System.out.println(JSON.toJSONString(service));
}
}
}
java 轮训_java 实现Nginx加权重轮训相关推荐
- java pdf水印排布问题_java 实现 PDF 加水印功能
使用java代码实现给PDF加水印的功能 首先导入所需要的依赖 com.itextpdf itextpdf 5.5.11 com.itextpdf itext-asian 5.2.0 java 代码实 ...
- kubernetes下的Nginx加Tomcat三部曲之三:实战扩容和升级
本章是<kubernetes下的Nginx加Tomcat三部曲系列>的终篇,今天咱们一起在kubernetes环境对下图中tomcat的数量进行调整,再修改tomcat中web工程的源码, ...
- Java 反射将配置文件数据加载到对象属性中
Java 反射将配置文件数据加载到对象属性中 Java 反射 可以根据类名找到相应的类,也可以将配置文件中的值加载到对应属性中. 需要用到的包:spring-core-3.1.2.Release.ja ...
- java pkcs#11读取证书加解密(初学-分享)
java pkcs#11读取证书加解密(初学-分享) http://www.cnblogs.com/sunfb/archive/2013/02/28/2937227.html 插入USB-KEY, 想 ...
- 深入java虚拟机学习 -- 类的加载机制(续)
昨晚写 深入java虚拟机学习 -- 类的加载机制 都到1点半了,由于第二天还要工作,没有将上篇文章中的demo讲解写出来,今天抽时间补上昨晚的例子讲解. 这里我先把昨天的两份代码贴过来,重新看下: ...
- Java show两个整数加减_怎么样用java编写界面实现两个数的加法运算
展开全部 import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ...
- java 根据类名示例化类_如何使用示例从Java中的类路径加载资源
java 根据类名示例化类 Java中的类路径不仅用于加载.class文件,而且还可以用于加载资源,例如属性文件,图像,图标,缩略图或任何二进制内容. Java提供了API来将这些资源读取为Input ...
- 如何使用示例从Java中的类路径加载资源
Java中的类路径不仅用于加载.class文件,而且还可以用于加载资源,例如属性文件,图像,图标,缩略图或任何二进制内容. Java提供了API来将这些资源读取为InputStream或URL. 假设 ...
- java模块_Java 9 揭秘(2. 模块化系统)
文 by / 林本托 Tips 做一个终身学习的人. 在此章节中,主要介绍以下内容: 在JDK 9之前Java源代码用于编写,打包和部署的方式以及该方法的潜在问题 JDK 9中有哪些模块 如何声明模块 ...
最新文章
- STM32串口中断的4种接收数据的实现方式
- 【数据结构与算法】之N个数中有K个数可能的组合算法
- ip数据报首部校验和的计算
- 内核堆栈 用户堆栈_堆栈痕迹从何而来?
- python实现k均值算法_python实现kMeans算法
- Centos 云服务器磁盘占用率90%以上的排查解决
- 动态规划位置hdu 4540 威威猫系列故事——打地鼠(动态规划)
- FPGA设计经验谈 —— 10年FPGA开发经验的工程师肺腑之言
- ps写php,PS是什么
- IPEmotion采集J1939协议信号介绍
- (附源码)springboot电子阅览室app 毕业设计 016514
- Android天气预报项目
- 前端:Tomcat服务器部署Web项目
- C语言学习1——第一、二、三章学习记录
- JAVA一些实例实战
- 如何打jar包和运行jar包
- winlogon 之 WlxLoggedOutSAS
- Shell 加法运算的几种写法
- 《Java Web开发实战》Java工程师推荐的进阶之路
- MACD金叉不绿选股公式