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;
|
||||
|
||||
/**
|
||||
* 使用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");
|
||||
|
@ -129,7 +125,6 @@ public class SelectInstences {
|
|||
sum = sum.add(value);
|
||||
arithmet.put(node.getIp(), value);
|
||||
}// 根据权重总和计算每个节点的特定比例
|
||||
System.out.println (arithmet );
|
||||
log.info("总可负载量:{}", arithmet);
|
||||
List<String> ips = new ArrayList<>();
|
||||
//获取每个ip的分配率
|
||||
|
@ -137,7 +132,7 @@ public class SelectInstences {
|
|||
//概率
|
||||
BigDecimal probability = arithmet.get(ip).divide(sum, 4, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100));
|
||||
arithmet.put(ip, probability);
|
||||
log.info(ip+"可负载率(权重值):{}",probability);
|
||||
log.info("{}可负载率(权重值):{}",ip, probability);
|
||||
}
|
||||
Set<String> ipSet = arithmet.keySet();
|
||||
|
||||
|
@ -148,13 +143,13 @@ public class SelectInstences {
|
|||
map.put(ip, arithmet.get(ip).multiply(finalSum).intValue() / 100)
|
||||
);
|
||||
Long i = 0L;
|
||||
System.out.println(map);
|
||||
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++;
|
||||
String ip = iterator.next();
|
||||
ips.add(ip);
|
||||
int i1 = map.get(ip) - 1;
|
||||
|
@ -163,15 +158,13 @@ public class SelectInstences {
|
|||
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);
|
||||
}
|
||||
|
||||
|
|
@ -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