From 0a9d3c25a85e0f62cdfa222344c909b63f123b94 Mon Sep 17 00:00:00 2001 From: Saisai Liu <1374434128@qq.com> Date: Thu, 30 May 2024 09:58:00 +0800 Subject: [PATCH] =?UTF-8?q?feat():=E8=8E=B7=E5=8F=96=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E4=BF=A1=E6=81=AF=20version-2():=E8=8E=B7=E5=8F=96ip?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E6=9D=83=E9=87=8D=E5=88=86=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 1 - ...ectInstences.java => SelectInstances.java} | 67 +++++++++---------- .../service/impl/FluxGetInfoServiceImpl.java | 4 +- src/main/resources/application.yml | 5 ++ 4 files changed, 37 insertions(+), 40 deletions(-) rename src/main/java/com/mobai/openApi/{SelectInstences.java => SelectInstances.java} (78%) create mode 100644 src/main/resources/application.yml diff --git a/.idea/misc.xml b/.idea/misc.xml index 82dbec8..c3f3b0a 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/main/java/com/mobai/openApi/SelectInstences.java b/src/main/java/com/mobai/openApi/SelectInstances.java similarity index 78% rename from src/main/java/com/mobai/openApi/SelectInstences.java rename to src/main/java/com/mobai/openApi/SelectInstances.java index f1637ee..478e898 100644 --- a/src/main/java/com/mobai/openApi/SelectInstences.java +++ b/src/main/java/com/mobai/openApi/SelectInstances.java @@ -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 redisTemplate; + private RedisTemplate redisTemplate; + /** * 使用AK&SK初始化账号Client * @@ -57,7 +53,7 @@ public class SelectInstences { @Scheduled(cron = "0/10 * * * * ? ") public void saveIps() throws Exception { List 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 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 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 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 ipSet = arithmet.keySet(); @@ -145,40 +140,38 @@ public class SelectInstences { Map 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 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; } diff --git a/src/main/java/com/mobai/service/impl/FluxGetInfoServiceImpl.java b/src/main/java/com/mobai/service/impl/FluxGetInfoServiceImpl.java index 441aa3f..023f052 100644 --- a/src/main/java/com/mobai/service/impl/FluxGetInfoServiceImpl.java +++ b/src/main/java/com/mobai/service/impl/FluxGetInfoServiceImpl.java @@ -51,8 +51,8 @@ public class FluxGetInfoServiceImpl implements FluxGetInfoService { //封装请求头 HttpEntity> formEntity = new HttpEntity<>(headers); ResponseEntity 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 = JSON.parseArray(exchange.getBody(), ApifoxModel.class); // get 获取具体所有信息 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..06211b3 --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,5 @@ +spring: + redis: + host: 43.142.100.73 + port: 6379 + password: