parent
ab68985bd4
commit
0a9d3c25a8
|
@ -1,4 +1,3 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
package com.mobai.openApi;
|
||||
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||
import com.aliyun.tea.TeaException;
|
||||
|
@ -9,7 +10,6 @@ import com.mobai.domain.ApifoxModel;
|
|||
import com.mobai.domain.Result;
|
||||
import com.mobai.service.FluxGetInfoService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
@ -17,24 +17,20 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.stereotype.Indexed;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Log4j2
|
||||
@Component
|
||||
public class SelectInstences {
|
||||
public class SelectInstances {
|
||||
|
||||
@Autowired
|
||||
private FluxGetInfoService fluxGetInfoService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String,String> redisTemplate;
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
/**
|
||||
* 使用AK&SK初始化账号Client
|
||||
*
|
||||
|
@ -57,7 +53,7 @@ public class SelectInstences {
|
|||
@Scheduled(cron = "0/10 * * * * ? ")
|
||||
public void saveIps() throws Exception {
|
||||
List<String> ips = new ArrayList<>();
|
||||
com.aliyun.ecs20140526.Client client = SelectInstences.createClient();
|
||||
com.aliyun.ecs20140526.Client client = SelectInstances.createClient();
|
||||
com.aliyun.ecs20140526.models.DescribeInstancesRequest describeInstancesRequest = new com.aliyun.ecs20140526.models.DescribeInstancesRequest()
|
||||
// .setImageId("m-8vb8qnidv34yj3nbirhc")
|
||||
.setRegionId("cn-zhangjiakou");
|
||||
|
@ -72,9 +68,9 @@ public class SelectInstences {
|
|||
List<String> ipAddress = inst
|
||||
.getPublicIpAddress()
|
||||
.getIpAddress();
|
||||
if (ipAddress.isEmpty()){
|
||||
if (ipAddress.isEmpty()) {
|
||||
continue;
|
||||
}else {
|
||||
} else {
|
||||
ipAddress.forEach(ip -> ips.add(ip));
|
||||
}
|
||||
}
|
||||
|
@ -82,20 +78,20 @@ public class SelectInstences {
|
|||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
log.error("错误信息::{}",error.getMessage());
|
||||
log.error("错误信息::{}", error.getMessage());
|
||||
// 诊断地址
|
||||
log.warn("诊断地址::{}",error.getData().get("Recommend"));
|
||||
log.warn("诊断地址::{}", error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
log.error("错误信息::{}",error.getMessage());
|
||||
log.error("错误信息::{}", error.getMessage());
|
||||
// 诊断地址
|
||||
if (error.getData() == null) {
|
||||
log.error("错误信息::{}","error.getData()为空");
|
||||
log.error("错误信息::{}", "error.getData()为空");
|
||||
} else {
|
||||
log.error("诊断地址::{}",error.getData().get("Recommend"));
|
||||
log.error("诊断地址::{}", error.getData().get("Recommend"));
|
||||
}
|
||||
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
|
@ -105,9 +101,9 @@ public class SelectInstences {
|
|||
Result<ApifoxModel> info = fluxGetInfoService.getInfo(ip);
|
||||
//连接总数
|
||||
long connectSize = info.getData().getMqttInfo().getConnectSize();
|
||||
log.info("{}::{}",ip,connectSize);
|
||||
log.info("{}::{}", ip, connectSize);
|
||||
//添加到一个容器
|
||||
nodes.add(new SmallNode(ip,connectSize));
|
||||
nodes.add(new SmallNode(ip, connectSize));
|
||||
}
|
||||
//负载均横方法
|
||||
this.getArithmetic(nodes);
|
||||
|
@ -127,17 +123,16 @@ public class SelectInstences {
|
|||
BigDecimal value = BigDecimal.valueOf(80 - node.getNum());
|
||||
//获取总量
|
||||
sum = sum.add(value);
|
||||
arithmet.put(node.getIp(),value);
|
||||
arithmet.put(node.getIp(), value);
|
||||
}// 根据权重总和计算每个节点的特定比例
|
||||
System.out.println (arithmet );
|
||||
log.info("总可负载量:{}",arithmet);
|
||||
log.info("总可负载量:{}", arithmet);
|
||||
List<String> ips = new ArrayList<>();
|
||||
//获取每个ip的分配率
|
||||
for (String ip : arithmet.keySet()) {
|
||||
//概率
|
||||
BigDecimal probability = arithmet.get(ip).divide(sum,4,BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100));
|
||||
arithmet.put(ip,probability);
|
||||
log.info(ip+"可负载率(权重值):{}",probability);
|
||||
BigDecimal probability = arithmet.get(ip).divide(sum, 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100));
|
||||
arithmet.put(ip, probability);
|
||||
log.info("{}可负载率(权重值):{}",ip, probability);
|
||||
}
|
||||
Set<String> ipSet = arithmet.keySet();
|
||||
|
||||
|
@ -145,40 +140,38 @@ public class SelectInstences {
|
|||
Map<String, Integer> map = new HashMap<>();
|
||||
// 转换成数量
|
||||
ipSet.forEach(ip ->
|
||||
map.put(ip,arithmet.get(ip).multiply(finalSum).intValue()/100)
|
||||
map.put(ip, arithmet.get(ip).multiply(finalSum).intValue() / 100)
|
||||
);
|
||||
Long i = 0L;
|
||||
System.out.println(map);
|
||||
while (true){
|
||||
log.info("ip对应可分配车辆:{}",map);
|
||||
int sumInit = map.values().stream().mapToInt(num -> num).sum();
|
||||
while (true) {
|
||||
ipSet = map.keySet();
|
||||
Iterator<String> iterator = ipSet.iterator();
|
||||
i++;
|
||||
while (iterator.hasNext()){
|
||||
i++;
|
||||
while (iterator.hasNext()) {
|
||||
String ip = iterator.next();
|
||||
ips.add(ip);
|
||||
int i1 = map.get(ip) - 1;
|
||||
map.put(ip,i1);
|
||||
if (map.get(ip).equals(0)){
|
||||
map.put(ip, i1);
|
||||
if (map.get(ip).equals(0)) {
|
||||
map.remove(ip);
|
||||
}
|
||||
}
|
||||
if (i.equals(finalSum)){
|
||||
if (ips.size() == sumInit) {
|
||||
break;
|
||||
}
|
||||
System.out.println(i);
|
||||
}
|
||||
|
||||
redisTemplate.opsForValue().set("fluxMq", JSON.toJSONString(ips));
|
||||
// 可负载IP轮询排列
|
||||
System.out.println(arithmet);
|
||||
System.out.println(ips);
|
||||
log.info("排列ip,{}", ips);
|
||||
}
|
||||
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
class SmallNode{
|
||||
class SmallNode {
|
||||
private String ip;
|
||||
private long num;
|
||||
}
|
|
@ -51,8 +51,8 @@ public class FluxGetInfoServiceImpl implements FluxGetInfoService {
|
|||
//封装请求头
|
||||
HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(headers);
|
||||
ResponseEntity<String> exchange = restTemplate.exchange(url + "cluster", HttpMethod.GET, formEntity, String.class);
|
||||
System.out.println(exchange);
|
||||
System.out.println(exchange.getBody());
|
||||
// System.out.println(exchange);
|
||||
// System.out.println(exchange.getBody());
|
||||
List<ApifoxModel> apifoxModel = JSON.parseArray(exchange.getBody(), ApifoxModel.class);
|
||||
// get 获取具体所有信息
|
||||
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
spring:
|
||||
redis:
|
||||
host: 43.142.100.73
|
||||
port: 6379
|
||||
password:
|
Loading…
Reference in New Issue