From 4c0155d1ccc4c2c0dfbdceeefb18f55f7099ce4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=A4=A7=E4=B8=BE?= <13970129+huandgaju@user.noreply.gitee.com> Date: Wed, 17 Apr 2024 09:40:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 46 ++ pom.xml | 260 ++++++++++ .../loadCenter/LoadCenterApplication.java | 22 + .../loadCenter/aliyun/config/AliConfig.java | 54 +++ .../aliyun/config/InstanceConfig.java | 62 +++ .../aliyun/config/RestClientConfig.java | 23 + .../aliyun/service/AliYunEcsService.java | 197 ++++++++ .../loadCenter/aliyun/utils/UserUtil.java | 53 +++ .../loadCenter/aliyun/utils/uuid/IdUtils.java | 44 ++ .../loadCenter/aliyun/utils/uuid/UUID.java | 449 ++++++++++++++++++ .../controller/LoadCenterController.java | 124 +++++ .../controller/WorkGatewayNodeController.java | 165 +++++++ .../muyu/loadCenter/domain/ApifoxModel.java | 55 +++ .../com/muyu/loadCenter/domain/CPUInfo.java | 30 ++ .../loadCenter/domain/EcsInstanceInfo.java | 46 ++ .../com/muyu/loadCenter/domain/FlowInfo.java | 28 ++ .../com/muyu/loadCenter/domain/JVMInfo.java | 58 +++ .../com/muyu/loadCenter/domain/MqttInfo.java | 64 +++ .../com/muyu/loadCenter/domain/Result.java | 117 +++++ .../loadCenter/domain/WorkGatewayNode.java | 23 + .../FastJson2JsonRedisSerializer.java | 56 +++ .../redis/configure/RedisConfig.java | 43 ++ .../redis/service/RedisService.java | 362 ++++++++++++++ src/main/resources/application.yml | 30 ++ src/test/java/Test1.java | 61 +++ 25 files changed, 2472 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/muyu/loadCenter/LoadCenterApplication.java create mode 100644 src/main/java/com/muyu/loadCenter/aliyun/config/AliConfig.java create mode 100644 src/main/java/com/muyu/loadCenter/aliyun/config/InstanceConfig.java create mode 100644 src/main/java/com/muyu/loadCenter/aliyun/config/RestClientConfig.java create mode 100644 src/main/java/com/muyu/loadCenter/aliyun/service/AliYunEcsService.java create mode 100644 src/main/java/com/muyu/loadCenter/aliyun/utils/UserUtil.java create mode 100644 src/main/java/com/muyu/loadCenter/aliyun/utils/uuid/IdUtils.java create mode 100644 src/main/java/com/muyu/loadCenter/aliyun/utils/uuid/UUID.java create mode 100644 src/main/java/com/muyu/loadCenter/controller/LoadCenterController.java create mode 100644 src/main/java/com/muyu/loadCenter/controller/WorkGatewayNodeController.java create mode 100644 src/main/java/com/muyu/loadCenter/domain/ApifoxModel.java create mode 100644 src/main/java/com/muyu/loadCenter/domain/CPUInfo.java create mode 100644 src/main/java/com/muyu/loadCenter/domain/EcsInstanceInfo.java create mode 100644 src/main/java/com/muyu/loadCenter/domain/FlowInfo.java create mode 100644 src/main/java/com/muyu/loadCenter/domain/JVMInfo.java create mode 100644 src/main/java/com/muyu/loadCenter/domain/MqttInfo.java create mode 100644 src/main/java/com/muyu/loadCenter/domain/Result.java create mode 100644 src/main/java/com/muyu/loadCenter/domain/WorkGatewayNode.java create mode 100644 src/main/java/com/muyu/loadCenter/redis/configure/FastJson2JsonRedisSerializer.java create mode 100644 src/main/java/com/muyu/loadCenter/redis/configure/RedisConfig.java create mode 100644 src/main/java/com/muyu/loadCenter/redis/service/RedisService.java create mode 100644 src/main/resources/application.yml create mode 100644 src/test/java/Test1.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..09bdfea --- /dev/null +++ b/.gitignore @@ -0,0 +1,46 @@ +###################################################################### +# Build Tools + +.gradle +/build/ +!gradle/wrapper/gradle-wrapper.jar + +target/ +!.mvn/wrapper/maven-wrapper.jar + +###################################################################### +# IDE + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### JRebel ### +rebel.xml +### NetBeans ### +nbproject/private/ +build/* +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ + +###################################################################### +# Others +*.log +*.xml.versionsBackup +*.swp + +!*/build/*.java +!*/build/*.html +!*/build/*.xml \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..750bc4d --- /dev/null +++ b/pom.xml @@ -0,0 +1,260 @@ + + + 4.0.0 + + com.muyu + LoadCenter + 1.0-SNAPSHOT + + 20 + 20 + 2.7.18 + UTF-8 + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + jar + sample + Alibaba Cloud SDK Code Sample for Java + + https://github.com/aliyun/alibabacloud-code-sample + + + The Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + + aliyundeveloper + Aliyun SDK + aliyunsdk@aliyun.com + + + + + sonatype-nexus-snapshots + https://s01.oss.sonatype.org/content/repositories/snapshots + + + sonatype-nexus-staging + https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + org.projectlombok + lombok + + + + com.alibaba.fastjson2 + fastjson2 + 2.0.47 + + + + com.aliyun + ecs20140526 + 3.1.2 + + + com.aliyun + tea-openapi + 0.3.2 + + + com.aliyun + tea-util + 0.2.21 + + + com.aliyun + darabonba-number + 0.0.3 + + + com.aliyun + tea-console + 0.0.1 + + + com.aliyun + darabonba-env + 0.1.1 + + + com.aliyun + tea + 1.1.14 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + com.aliyun + darabonba-string + 0.0.3 + + + + + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + 16 + 16 + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + + sign-artifacts + verify + + sign + + + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.3 + true + + sonatype-nexus-staging + https://s01.oss.sonatype.org/ + true + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.1.1 + + UTF-8 + none + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.1 + + + org.apache.maven.plugins + maven-assembly-plugin + 2.4.1 + + + jar-with-dependencies + + + + true + com.aliyun.sample.Sample1 + + + + + + make-assembly + package + + single + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + ${project.artifactId} + + + + + + + + diff --git a/src/main/java/com/muyu/loadCenter/LoadCenterApplication.java b/src/main/java/com/muyu/loadCenter/LoadCenterApplication.java new file mode 100644 index 0000000..ed7ce08 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/LoadCenterApplication.java @@ -0,0 +1,22 @@ +package com.muyu.loadCenter; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; + +/** + * @ProjectName: LoadCenter + * @PackageName: com.muyu.loadCenter + * @Description TODO + * @Author HuangDaJu + * @Date 2024/4/13 08:32 + * @Version 1.0 + */ + +@EnableScheduling +@SpringBootApplication +public class LoadCenterApplication { + public static void main(String[] args) { + SpringApplication.run(LoadCenterApplication.class, args); + } +} diff --git a/src/main/java/com/muyu/loadCenter/aliyun/config/AliConfig.java b/src/main/java/com/muyu/loadCenter/aliyun/config/AliConfig.java new file mode 100644 index 0000000..ef87722 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/aliyun/config/AliConfig.java @@ -0,0 +1,54 @@ +package com.muyu.loadCenter.aliyun.config; + +import com.aliyun.ecs20140526.Client; +import com.aliyun.teaopenapi.models.Config; +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @ProjectName: LoadCenter + * @PackageName: com.muyu.loadCenter.aliyun.config + * @Description 阿里云配置类 + * @Author HuangDaJu + * @Date 2024/4/16 14:48 + * @Version 1.0 + */ + +@Data +@Configuration +@ConfigurationProperties(prefix = "config.ali") +public class AliConfig { + + + /** + * access-key-id + */ + private String accessKeyId; + /** + * access-key-secret + */ + private String accessKeySecret; + /** + * 地域id + */ + private String regionId; + + + + @Bean + public Client createEcsClient(AliConfig aliConfig) throws Exception { + Config config = new Config() + // 您的AccessKey ID + .setAccessKeyId(aliConfig.getAccessKeyId()) + // 您的AccessKey Secret + .setAccessKeySecret(aliConfig.getAccessKeySecret()) + // 您的可用区ID + .setRegionId(aliConfig.getRegionId()); + return new Client(config); + } + + + +} diff --git a/src/main/java/com/muyu/loadCenter/aliyun/config/InstanceConfig.java b/src/main/java/com/muyu/loadCenter/aliyun/config/InstanceConfig.java new file mode 100644 index 0000000..2e747a2 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/aliyun/config/InstanceConfig.java @@ -0,0 +1,62 @@ +package com.muyu.loadCenter.aliyun.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + *创建实例的参数映射yml + */ +@Data +@Configuration +@ConfigurationProperties(prefix = "config.instance") +public class InstanceConfig { + + /** + * 地域Id + */ + private String regionId; + /** + * 镜像 ID,启动实例时选择的镜像资源。 + */ + private String imageId; + /** + * 实例规格 + */ + private String instanceType; + /** + * 安全组id + */ + private String securityGroupId; + /** + * 虚拟交换机 + */ + private String vSwitchId; + /** + * 公网出带宽最大值,单位为 Mbit/s。取值范围:0~100。 默认值:0。 + */ + private String internetMaxBandwidthOut; + /** + * 系统盘大小 + */ + private String size; + /** + * 系统盘的云盘种类 + */ + private String category; + /** + * ECS实例的计费方式 + */ + private String instanceChargeType; + /** + * 网络计费类型。取值范围: + */ + private String internetChargeType; + + + + + + + +} diff --git a/src/main/java/com/muyu/loadCenter/aliyun/config/RestClientConfig.java b/src/main/java/com/muyu/loadCenter/aliyun/config/RestClientConfig.java new file mode 100644 index 0000000..ad34322 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/aliyun/config/RestClientConfig.java @@ -0,0 +1,23 @@ +package com.muyu.loadCenter.aliyun.config;//package com.muyu.business.domain.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +/** + * @ProjectName: cloud-vehicles + * @PackageName: com.muyu.system.common.domain.config + * @Description TODO + * @Author XiaoFan + * @Date 2024/4/2 19:37 + * @Version 1.0 + */ +@Configuration +public class RestClientConfig { + @Bean + public RestTemplate restTemplate(){ + return new RestTemplate(); + } + + +} diff --git a/src/main/java/com/muyu/loadCenter/aliyun/service/AliYunEcsService.java b/src/main/java/com/muyu/loadCenter/aliyun/service/AliYunEcsService.java new file mode 100644 index 0000000..89c51b0 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/aliyun/service/AliYunEcsService.java @@ -0,0 +1,197 @@ +package com.muyu.loadCenter.aliyun.service; + +import com.aliyun.ecs20140526.Client; +import com.aliyun.ecs20140526.models.*; +import com.aliyun.tea.TeaException; +import com.aliyun.teautil.models.RuntimeOptions; +import com.muyu.loadCenter.aliyun.config.AliConfig; + +import com.muyu.loadCenter.aliyun.utils.UserUtil; +import com.muyu.loadCenter.domain.EcsInstanceInfo; +import com.muyu.loadCenter.aliyun.config.InstanceConfig; +import lombok.extern.log4j.Log4j2; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; + + + +/** + * @ProjectName: LoadCenter + * @PackageName: com.muyu.loadCenter.aliyun + * @Description 阿里云实现类OpenApi调用 + * @Author HuangDaJu + * @Date 2024/4/16 14:57 + * @Version 1.0 + */ +@Log4j2 +@Service +public class AliYunEcsService { + + private final AliConfig aliConfig; + private final InstanceConfig instanceConfig; + private final Client client; + + public AliYunEcsService(AliConfig aliConfig, InstanceConfig instanceConfig, Client client) { + this.aliConfig = aliConfig; + this.instanceConfig = instanceConfig; + this.client = client; + } + + + /** + * RunInstances 通过备选实例规格创建ECS实例最佳实践 + */ + public String runInstances() throws Exception { + + RunInstancesRequest request = new RunInstancesRequest() + .setRegionId(aliConfig.getRegionId()) + .setImageId(instanceConfig.getImageId()) + .setInstanceType(instanceConfig.getInstanceType()) + .setSecurityGroupId(instanceConfig.getSecurityGroupId()) + .setVSwitchId(instanceConfig.getVSwitchId()) + .setInstanceName("gateway:node:HuangDaJu") + .setDescription(new Date().toLocaleString() + " 创建的实例节点") + .setInternetMaxBandwidthOut(Integer.valueOf(instanceConfig.getInternetMaxBandwidthOut())) + .setInternetChargeType(instanceConfig.getInternetChargeType()) + .setInstanceChargeType(instanceConfig.getInstanceChargeType()) + // 批量创建五台ECS实例,如果不设置该参数,默认创建一台ECS实例。 + // amount = 5, + // 如果缺少库存可以接受的最低创建数量。 + // minAmount = 2, + // 打开预检参数功能,不会实际创建ECS实例,只检查参数正确性、用户权限或者ECS库存等问题。 + // 实际情况下,设置了DryRun参数后,Amount必须为1,MinAmount必须为空,您可以根据实际需求修改代码。 + // .setDryRun(true) + .setDryRun(false) + .setSystemDisk(new RunInstancesRequest.RunInstancesRequestSystemDisk() + .setSize(instanceConfig.getSize()) + .setCategory(instanceConfig.getCategory())); + + + String result = ""; + try { + com.aliyun.teaconsole.Client.log("--------------------批量创建实例开始--------------------"); + RunInstancesResponse responces = client.runInstances(request); + com.aliyun.teaconsole.Client.log("--------------------创建实例成功,实例ID:" + com.aliyun.teautil.Common.toJSONString(responces.body.instanceIdSets.instanceIdSet) + "--------------------"); + //返回实例ID + result = responces.body.instanceIdSets.instanceIdSet + "";//前后带 [] + result = UserUtil.removeBrackets(result);//前后不带[] + } catch (TeaException error) { + com.aliyun.teaconsole.Client.log("--------------------创建实例失败:" + com.aliyun.teautil.Common.toJSONString(error.code) + "--------------------" + error.message); + } catch (Exception _error) { + TeaException error = new TeaException(_error.getMessage(), _error); + com.aliyun.teaconsole.Client.log("--------------------创建实例失败:" + com.aliyun.teautil.Common.toJSONString(error.code) + "--------------------" + error.message); + } + return result; + } + + + /** + * 传入实例ID + */ + public EcsInstanceInfo selectList(String instanceId) throws Exception { + + DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest() + .setRegionId(aliConfig.getRegionId()) + .setInstanceName("*") + .setInstanceIds(com.aliyun.teautil.Common.toJSONString(com.aliyun.darabonbastring.Client.split(instanceId, ",", 50))) + .setPageSize(10); + + + //初始化返回值 + List instanceList = null; + + + try { + + // 复制代码运行请自行打印 API 的返回值 + DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, new RuntimeOptions()); + DescribeInstancesResponseBody body = describeInstancesResponse.getBody(); + DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstances instances = body.getInstances(); + + instanceList = instances.getInstance(); + + if (instanceList == null || instanceList.isEmpty()){ + return new EcsInstanceInfo(); + } + + + EcsInstanceInfo ecsInstanceInfo = new EcsInstanceInfo(); + instanceList.forEach(item -> { + + ecsInstanceInfo.setInstanceName(item.getInstanceName()); + + ecsInstanceInfo.setInstanceId(item.getInstanceId()); + + ecsInstanceInfo.setStatus(item.status); + + ecsInstanceInfo.setPublicIpAddress(UserUtil.removeBrackets(item.getPublicIpAddress().getIpAddress().toString())); + + ecsInstanceInfo.setRecyclable(item.getRecyclable()); + + ecsInstanceInfo.setPrivateIpAddress(item.getVpcAttributes().getPrivateIpAddress().ipAddress.toString()); + + }); + return ecsInstanceInfo; + + } catch (TeaException error) { + log.error("code:[{}], message: [{}],data: [{}]",error.getCode(),error.getMessage(),error.getData()); + // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 + // 错误 message + System.out.println(error.getMessage()); + // 诊断地址 + System.out.println(error.getData().get("Recommend")); + com.aliyun.teautil.Common.assertAsString(error.message); + } catch (Exception _error) { + TeaException error = new TeaException(_error.getMessage(), _error); + + log.error("message: [{}]",_error.getMessage(),_error); + // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 + // 错误 message + System.out.println(error.getMessage()); + // 诊断地址 + System.out.println(error.getData().get("Recommend")); + com.aliyun.teautil.Common.assertAsString(error.message); + } + + return new EcsInstanceInfo(); + } + + /** + * 删除(释放)实例 + */ + + public void releaseECS(String instanceId) throws Exception { + + DeleteInstancesRequest deleteInstancesRequest = new DeleteInstancesRequest() + .setRegionId(aliConfig.getRegionId()) + .setDryRun(false) + .setForce(true) + .setTerminateSubscription(false) + .setInstanceId(java.util.Arrays.asList( instanceId)); + + try { + // 复制代码运行请自行打印 API 的返回值 + client.deleteInstancesWithOptions(deleteInstancesRequest, new RuntimeOptions()); + } catch (TeaException error) { + // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 + // 错误 message + log.error("code:[{}], message: [{}],data: [{}]",error.getCode(),error.getMessage(),error.getData()); + System.out.println(error); + // 诊断地址 + System.out.println(error.getData().get("Recommend")); + com.aliyun.teautil.Common.assertAsString(error); + } catch (Exception _error) { + TeaException error = new TeaException(_error.getMessage(), _error); + // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 + // 错误 message + log.error("message: [{}]",_error.getMessage()); + + + } + } + + + +} diff --git a/src/main/java/com/muyu/loadCenter/aliyun/utils/UserUtil.java b/src/main/java/com/muyu/loadCenter/aliyun/utils/UserUtil.java new file mode 100644 index 0000000..c09191e --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/aliyun/utils/UserUtil.java @@ -0,0 +1,53 @@ +package com.muyu.loadCenter.aliyun.utils; + + +import com.muyu.loadCenter.aliyun.utils.uuid.IdUtils; +import com.muyu.loadCenter.redis.service.RedisService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.concurrent.TimeUnit; + +/** + * @ProjectName: LoadCenter + * @Author: LiuYunHu + * @CreateTime: 2024/4/13 + * @Description: 自定义工具类 + */ +@Component +public class UserUtil { + @Autowired + private RedisService redisService; + + /* + * @Author: LiuYunHu + * @Date: 2024/4/13 9:25 + * @Description: 将字符串前后的[]进行删除 + * @Param: [input] + * @Return: java.lang.String + **/ + public static String removeBrackets(String input) { + // 使用正则表达式匹配并移除括号 + String pattern = "\\[|\\]"; // 匹配 [ 或 ] + return input.replaceAll(pattern, ""); + } + + + /* + * @Author: LiuYunHu + * @Date: 2024/4/13 11:42 + * @Description: 获取实例注册token令牌 + * @Param: + * @Return: + **/ + public String getToken(String instanceIp) { + //生成令牌 + String randomUUID = IdUtils.randomUUID(); + + //缓存两分钟 + redisService.setCacheObject("lyhToken:" + instanceIp, randomUUID, 2L, TimeUnit.MINUTES); + + //令牌返回 + return randomUUID; + } +} diff --git a/src/main/java/com/muyu/loadCenter/aliyun/utils/uuid/IdUtils.java b/src/main/java/com/muyu/loadCenter/aliyun/utils/uuid/IdUtils.java new file mode 100644 index 0000000..c91a04c --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/aliyun/utils/uuid/IdUtils.java @@ -0,0 +1,44 @@ +package com.muyu.loadCenter.aliyun.utils.uuid; + +/** + * ID生成器工具类 + * + * @author couplet + */ +public class IdUtils { + /** + * 获取随机UUID + * + * @return 随机UUID + */ + public static String randomUUID () { + return UUID.randomUUID().toString(); + } + + /** + * 简化的UUID,去掉了横线 + * + * @return 简化的UUID,去掉了横线 + */ + public static String simpleUUID () { + return UUID.randomUUID().toString(true); + } + + /** + * 获取随机UUID,使用性能更好的ThreadLocalRandom生成UUID + * + * @return 随机UUID + */ + public static String fastUUID () { + return UUID.fastUUID().toString(); + } + + /** + * 简化的UUID,去掉了横线,使用性能更好的ThreadLocalRandom生成UUID + * + * @return 简化的UUID,去掉了横线 + */ + public static String fastSimpleUUID () { + return UUID.fastUUID().toString(true); + } +} diff --git a/src/main/java/com/muyu/loadCenter/aliyun/utils/uuid/UUID.java b/src/main/java/com/muyu/loadCenter/aliyun/utils/uuid/UUID.java new file mode 100644 index 0000000..8381017 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/aliyun/utils/uuid/UUID.java @@ -0,0 +1,449 @@ +package com.muyu.loadCenter.aliyun.utils.uuid; + + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; + +/** + * 提供通用唯一识别码(universally unique identifier)(UUID)实现 + * + * @author couplet + */ +public final class UUID implements java.io.Serializable, Comparable { + private static final long serialVersionUID = -1185015143654744140L; + /** + * 此UUID的最高64有效位 + */ + private final long mostSigBits; + /** + * 此UUID的最低64有效位 + */ + private final long leastSigBits; + + /** + * 私有构造 + * + * @param data 数据 + */ + private UUID (byte[] data) { + long msb = 0; + long lsb = 0; + assert data.length == 16 : "data must be 16 bytes in length"; + for (int i = 0 ; i < 8 ; i++) { + msb = (msb << 8) | (data[i] & 0xff); + } + for (int i = 8 ; i < 16 ; i++) { + lsb = (lsb << 8) | (data[i] & 0xff); + } + this.mostSigBits = msb; + this.leastSigBits = lsb; + } + + /** + * 使用指定的数据构造新的 UUID。 + * + * @param mostSigBits 用于 {@code UUID} 的最高有效 64 位 + * @param leastSigBits 用于 {@code UUID} 的最低有效 64 位 + */ + public UUID (long mostSigBits, long leastSigBits) { + this.mostSigBits = mostSigBits; + this.leastSigBits = leastSigBits; + } + + /** + * 获取类型 4(伪随机生成的)UUID 的静态工厂。 + * + * @return 随机生成的 {@code UUID} + */ + public static UUID fastUUID () { + return randomUUID(false); + } + + /** + * 获取类型 4(伪随机生成的)UUID 的静态工厂。 使用加密的强伪随机数生成器生成该 UUID。 + * + * @return 随机生成的 {@code UUID} + */ + public static UUID randomUUID () { + return randomUUID(true); + } + + /** + * 获取类型 4(伪随机生成的)UUID 的静态工厂。 使用加密的强伪随机数生成器生成该 UUID。 + * + * @param isSecure 是否使用{@link SecureRandom}如果是可以获得更安全的随机码,否则可以得到更好的性能 + * + * @return 随机生成的 {@code UUID} + */ + public static UUID randomUUID (boolean isSecure) { + final Random ng = isSecure ? Holder.numberGenerator : getRandom(); + + byte[] randomBytes = new byte[16]; + ng.nextBytes(randomBytes); + randomBytes[6] &= 0x0f; /* clear version */ + randomBytes[6] |= 0x40; /* set to version 4 */ + randomBytes[8] &= 0x3f; /* clear variant */ + randomBytes[8] |= 0x80; /* set to IETF variant */ + return new UUID(randomBytes); + } + + /** + * 根据指定的字节数组获取类型 3(基于名称的)UUID 的静态工厂。 + * + * @param name 用于构造 UUID 的字节数组。 + * + * @return 根据指定数组生成的 {@code UUID} + */ + public static UUID nameUUIDFromBytes (byte[] name) { + MessageDigest md; + try { + md = MessageDigest.getInstance("MD5"); + } catch (NoSuchAlgorithmException nsae) { + throw new InternalError("MD5 not supported"); + } + byte[] md5Bytes = md.digest(name); + md5Bytes[6] &= 0x0f; /* clear version */ + md5Bytes[6] |= 0x30; /* set to version 3 */ + md5Bytes[8] &= 0x3f; /* clear variant */ + md5Bytes[8] |= 0x80; /* set to IETF variant */ + return new UUID(md5Bytes); + } + + /** + * 根据 {@link #toString()} 方法中描述的字符串标准表示形式创建{@code UUID}。 + * + * @param name 指定 {@code UUID} 字符串 + * + * @return 具有指定值的 {@code UUID} + * + * @throws IllegalArgumentException 如果 name 与 {@link #toString} 中描述的字符串表示形式不符抛出此异常 + */ + public static UUID fromString (String name) { + String[] components = name.split("-"); + if (components.length != 5) { + throw new IllegalArgumentException("Invalid UUID string: " + name); + } + for (int i = 0 ; i < 5 ; i++) { + components[i] = "0x" + components[i]; + } + + long mostSigBits = Long.decode(components[0]).longValue(); + mostSigBits <<= 16; + mostSigBits |= Long.decode(components[1]).longValue(); + mostSigBits <<= 16; + mostSigBits |= Long.decode(components[2]).longValue(); + + long leastSigBits = Long.decode(components[3]).longValue(); + leastSigBits <<= 48; + leastSigBits |= Long.decode(components[4]).longValue(); + + return new UUID(mostSigBits, leastSigBits); + } + + /** + * 返回指定数字对应的hex值 + * + * @param val 值 + * @param digits 位 + * + * @return 值 + */ + private static String digits (long val, int digits) { + long hi = 1L << (digits * 4); + return Long.toHexString(hi | (val & (hi - 1))).substring(1); + } + + /** + * 获取{@link SecureRandom},类提供加密的强随机数生成器 (RNG) + * + * @return {@link SecureRandom} + */ + public static SecureRandom getSecureRandom () { + try { + return SecureRandom.getInstance("SHA1PRNG"); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } + + /** + * 获取随机数生成器对象
+ * ThreadLocalRandom是JDK 7之后提供并发产生随机数,能够解决多个线程发生的竞争争夺。 + * + * @return {@link ThreadLocalRandom} + */ + public static ThreadLocalRandom getRandom () { + return ThreadLocalRandom.current(); + } + + /** + * 返回此 UUID 的 128 位值中的最低有效 64 位。 + * + * @return 此 UUID 的 128 位值中的最低有效 64 位。 + */ + public long getLeastSignificantBits () { + return leastSigBits; + } + + /** + * 返回此 UUID 的 128 位值中的最高有效 64 位。 + * + * @return 此 UUID 的 128 位值中最高有效 64 位。 + */ + public long getMostSignificantBits () { + return mostSigBits; + } + + /** + * 与此 {@code UUID} 相关联的版本号. 版本号描述此 {@code UUID} 是如何生成的。 + *

+ * 版本号具有以下含意: + *

+ * + * @return 此 {@code UUID} 的版本号 + */ + public int version () { + // Version is bits masked by 0x000000000000F000 in MS long + return (int) ((mostSigBits >> 12) & 0x0f); + } + + /** + * 与此 {@code UUID} 相关联的变体号。变体号描述 {@code UUID} 的布局。 + *

+ * 变体号具有以下含意: + *

+ * + * @return 此 {@code UUID} 相关联的变体号 + */ + public int variant () { + // This field is composed of a varying number of bits. + // 0 - - Reserved for NCS backward compatibility + // 1 0 - The IETF aka Leach-Salz variant (used by this class) + // 1 1 0 Reserved, Microsoft backward compatibility + // 1 1 1 Reserved for future definition. + return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62))) & (leastSigBits >> 63)); + } + + /** + * 与此 UUID 相关联的时间戳值。 + * + *

+ * 60 位的时间戳值根据此 {@code UUID} 的 time_low、time_mid 和 time_hi 字段构造。
+ * 所得到的时间戳以 100 毫微秒为单位,从 UTC(通用协调时间) 1582 年 10 月 15 日零时开始。 + * + *

+ * 时间戳值仅在在基于时间的 UUID(其 version 类型为 1)中才有意义。
+ * 如果此 {@code UUID} 不是基于时间的 UUID,则此方法抛出 UnsupportedOperationException。 + * + * @throws UnsupportedOperationException 如果此 {@code UUID} 不是 version 为 1 的 UUID。 + */ + public long timestamp () throws UnsupportedOperationException { + checkTimeBase(); + return (mostSigBits & 0x0FFFL) << 48// + | ((mostSigBits >> 16) & 0x0FFFFL) << 32// + | mostSigBits >>> 32; + } + + /** + * 与此 UUID 相关联的时钟序列值。 + * + *

+ * 14 位的时钟序列值根据此 UUID 的 clock_seq 字段构造。clock_seq 字段用于保证在基于时间的 UUID 中的时间唯一性。 + *

+ * {@code clockSequence} 值仅在基于时间的 UUID(其 version 类型为 1)中才有意义。 如果此 UUID 不是基于时间的 UUID,则此方法抛出 + * UnsupportedOperationException。 + * + * @return 此 {@code UUID} 的时钟序列 + * + * @throws UnsupportedOperationException 如果此 UUID 的 version 不为 1 + */ + public int clockSequence () throws UnsupportedOperationException { + checkTimeBase(); + return (int) ((leastSigBits & 0x3FFF000000000000L) >>> 48); + } + + /** + * 与此 UUID 相关的节点值。 + * + *

+ * 48 位的节点值根据此 UUID 的 node 字段构造。此字段旨在用于保存机器的 IEEE 802 地址,该地址用于生成此 UUID 以保证空间唯一性。 + *

+ * 节点值仅在基于时间的 UUID(其 version 类型为 1)中才有意义。
+ * 如果此 UUID 不是基于时间的 UUID,则此方法抛出 UnsupportedOperationException。 + * + * @return 此 {@code UUID} 的节点值 + * + * @throws UnsupportedOperationException 如果此 UUID 的 version 不为 1 + */ + public long node () throws UnsupportedOperationException { + checkTimeBase(); + return leastSigBits & 0x0000FFFFFFFFFFFFL; + } + + /** + * 返回此{@code UUID} 的字符串表现形式。 + * + *

+ * UUID 的字符串表示形式由此 BNF 描述: + * + *

+     * {@code
+     * UUID                   = ----
+     * time_low               = 4*
+     * time_mid               = 2*
+     * time_high_and_version  = 2*
+     * variant_and_sequence   = 2*
+     * node                   = 6*
+     * hexOctet               = 
+     * hexDigit               = [0-9a-fA-F]
+     * }
+     * 
+ * + * + * + * @return 此{@code UUID} 的字符串表现形式 + * + * @see #toString(boolean) + */ + @Override + public String toString () { + return toString(false); + } + + /** + * 返回此{@code UUID} 的字符串表现形式。 + * + *

+ * UUID 的字符串表示形式由此 BNF 描述: + * + *

+     * {@code
+     * UUID                   = ----
+     * time_low               = 4*
+     * time_mid               = 2*
+     * time_high_and_version  = 2*
+     * variant_and_sequence   = 2*
+     * node                   = 6*
+     * hexOctet               = 
+     * hexDigit               = [0-9a-fA-F]
+     * }
+     * 
+ * + * + * + * @param isSimple 是否简单模式,简单模式为不带'-'的UUID字符串 + * + * @return 此{@code UUID} 的字符串表现形式 + */ + public String toString (boolean isSimple) { + final StringBuilder builder = new StringBuilder(isSimple ? 32 : 36); + // time_low + builder.append(digits(mostSigBits >> 32, 8)); + if (false == isSimple) { + builder.append('-'); + } + // time_mid + builder.append(digits(mostSigBits >> 16, 4)); + if (false == isSimple) { + builder.append('-'); + } + // time_high_and_version + builder.append(digits(mostSigBits, 4)); + if (false == isSimple) { + builder.append('-'); + } + // variant_and_sequence + builder.append(digits(leastSigBits >> 48, 4)); + if (false == isSimple) { + builder.append('-'); + } + // node + builder.append(digits(leastSigBits, 12)); + + return builder.toString(); + } + + // Comparison Operations + + /** + * 返回此 UUID 的哈希码。 + * + * @return UUID 的哈希码值。 + */ + @Override + public int hashCode () { + long hilo = mostSigBits ^ leastSigBits; + return ((int) (hilo >> 32)) ^ (int) hilo; + } + + // ------------------------------------------------------------------------------------------------------------------- + // Private method start + + /** + * 将此对象与指定对象比较。 + *

+ * 当且仅当参数不为 {@code null}、而是一个 UUID 对象、具有与此 UUID 相同的 varriant、包含相同的值(每一位均相同)时,结果才为 {@code true}。 + * + * @param obj 要与之比较的对象 + * + * @return 如果对象相同,则返回 {@code true};否则返回 {@code false} + */ + @Override + public boolean equals (Object obj) { + if ((null == obj) || (obj.getClass() != UUID.class)) { + return false; + } + UUID id = (UUID) obj; + return (mostSigBits == id.mostSigBits && leastSigBits == id.leastSigBits); + } + + /** + * 将此 UUID 与指定的 UUID 比较。 + * + *

+ * 如果两个 UUID 不同,且第一个 UUID 的最高有效字段大于第二个 UUID 的对应字段,则第一个 UUID 大于第二个 UUID。 + * + * @param val 与此 UUID 比较的 UUID + * + * @return 在此 UUID 小于、等于或大于 val 时,分别返回 -1、0 或 1。 + */ + @Override + public int compareTo (UUID val) { + // The ordering is intentionally set up so that the UUIDs + // can simply be numerically compared as two numbers + return (this.mostSigBits < val.mostSigBits ? -1 : // + (this.mostSigBits > val.mostSigBits ? 1 : // + (this.leastSigBits < val.leastSigBits ? -1 : // + (this.leastSigBits > val.leastSigBits ? 1 : // + 0)))); + } + + /** + * 检查是否为time-based版本UUID + */ + private void checkTimeBase () { + if (version() != 1) { + throw new UnsupportedOperationException("Not a time-based UUID"); + } + } + + /** + * SecureRandom 的单例 + */ + private static class Holder { + static final SecureRandom numberGenerator = getSecureRandom(); + } +} diff --git a/src/main/java/com/muyu/loadCenter/controller/LoadCenterController.java b/src/main/java/com/muyu/loadCenter/controller/LoadCenterController.java new file mode 100644 index 0000000..eecdfb1 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/controller/LoadCenterController.java @@ -0,0 +1,124 @@ +package com.muyu.loadCenter.controller; + +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import com.muyu.loadCenter.aliyun.service.AliYunEcsService; +import com.muyu.loadCenter.domain.EcsInstanceInfo; +import com.muyu.loadCenter.domain.Result; +import lombok.extern.log4j.Log4j2; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; +import com.muyu.loadCenter.redis.service.RedisService; +import org.springframework.web.client.RestTemplate; + +import java.util.Set; + + +/** + * 负载中心控制器,负责管理服务器集群的负载情况,并根据负载进行扩容或缩容操作。 + * @Author HuangDaJu + * @Date 2024/4/13 08:43 + */ +@Component +@Log4j2 +public class LoadCenterController { + // 用于临时统计连接数以判断是否需要扩容的变量 + int aa=0; + // 临时变量,无特定用途(根据现有代码) + int bb=0; + @Autowired + StringRedisTemplate redisTemplate; // Redis字符串模板,用于与Redis进行交互 + + @Autowired + private RedisService redisService; // Redis服务,封装了与Redis操作相关的功能 + @Autowired + private AliYunEcsService aliYunEcsService; // 阿里云ECS服务,用于管理云服务器 + @Autowired + private RestTemplate restTemplate; // 用于与其他服务进行HTTP交互的模板 + + /** + * 定时任务,每30秒扫描一次服务器集群的负载情况。 + * 如果任一服务器的车辆连接数达到或超过阈值,则触发扩容; + * 如果服务器的连接数低于阈值,则触发缩容。 + */ + @Scheduled(cron = "0/5 * * * * ?") + public void scheduleECS() throws Exception { + // 客户端初始化 + OkHttpClient client = new OkHttpClient(); + // 从Redis获取服务器IP集合 + Set ipCacheSet = redisService.getCacheZSet("ECS"); + + log.info("共有个"+ipCacheSet.size()+"服务器"); + + // 遍历每台服务器进行负载检查 + for (String ip : ipCacheSet) { + // 构建请求URL和请求头 + String URL = "http://"+ip+":8080/public/cluster"; + Request request = new Request.Builder() + .url(URL) + .get() + .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)") + .addHeader("Accesstoken", "") + .build(); + + try { + Response response = client.newCall(request).execute(); + // 解析响应数据 + JSONArray jsonArray = JSONArray.parseArray(response.body().string()); + JSONObject jsonObject = jsonArray.getJSONObject(0); + JSONObject mqttInfo = jsonObject.getJSONObject("mqttInfo"); + int connectSize = mqttInfo.getIntValue("connectSize"); + + log.info("服务器:"+ip+"-车辆连接数:"+connectSize); + + // 更新Redis中服务器的连接数 + redisService.setCacheZSet("ECS", ip, connectSize); + + // 根据连接数判断是否需要进行扩容或缩容 + if (connectSize >= 5) { + aa++; + // 当满足扩容条件时,记录日志并执行扩容操作 + if (aa == ipCacheSet.size()) { + log.info("需要扩容"); + // 调用阿里云ECS服务创建新实例 + String instanceId = aliYunEcsService.runInstances(); + log.info("扩容的节点ip为:" + instanceId); + + // 休眠5秒以确保新实例创建完成 + Thread.sleep(5000); + + // 获取新实例信息并将其持久化到本地数据库 + EcsInstanceInfo ecsInstanceInfo = aliYunEcsService.selectList(instanceId); + String url = "http://127.0.0.1:9006/ecsInstance/add"; + restTemplate.postForObject(url, ecsInstanceInfo, Result.class); + log.info("实例信息持久化本地"); + + // 将新实例的IP和ID存入Redis + redisService.setCacheZSet("ECS", ecsInstanceInfo.getPublicIpAddress(), 0); + log.info("实例id和公网ip存入redis"); + aa = 0; // 重置计数器 + } + } else if (connectSize <= 2) { + // 缩容逻辑:删除连接数过低的服务器实例 +// String url = "http://127.0.0.1:9006/ecsInstance/select/" + ip; +// Result result = restTemplate.postForObject(url, null, Result.class); +// String instanceId = (String) result.getData(); +// aliYunEcsService.releaseECS(instanceId); // 释放ECS实例 + + Long i = redisService.deleteCacheZset("ECS", ip);// 从Redis中删除该服务器的记录 + + + log.info(i+"连接数小于2,服务器缩容:" + ip); + aa = 0; // 重置计数器 + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/main/java/com/muyu/loadCenter/controller/WorkGatewayNodeController.java b/src/main/java/com/muyu/loadCenter/controller/WorkGatewayNodeController.java new file mode 100644 index 0000000..9fe5867 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/controller/WorkGatewayNodeController.java @@ -0,0 +1,165 @@ +package com.muyu.loadCenter.controller; + +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import com.muyu.loadCenter.LoadCenterApplication; +import com.muyu.loadCenter.domain.Result; +import com.muyu.loadCenter.domain.WorkGatewayNode; +import com.muyu.loadCenter.redis.service.RedisService; +import lombok.extern.log4j.Log4j2; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.*; + +@Log4j2 +@RestController +@RequestMapping("carGoGoGo") +public class WorkGatewayNodeController { + + @Autowired + private RedisService redisService; + @PostMapping("/workGatewayNode") + + public Result getWorkGatewayNode() throws Exception { + +// redisService.setCacheObject("cursor", 0); + + + ArrayList nodeIdList = new ArrayList<>(); + + + + Map map = redisService.getCacheZSetScore("ECS"); + + for (Map.Entry entry : map.entrySet()) { + WorkGatewayNode workGatewayNode1 = new WorkGatewayNode(); + log.info(entry.getKey().toString()+"--"+entry.getValue()); + workGatewayNode1.setNodeId(entry.getKey().toString()); + workGatewayNode1.setWeight(entry.getValue().intValue()); + nodeIdList.add(workGatewayNode1); + } + + log.info("----------------------------分割线----↓--------------------------"); + System.out.println(nodeIdList); + log.info("----------------------------分割线----↑---------------------------"); + +// ArrayList nodeIdList = carWorkGatewayNode(); + + + + List loadNodeList = new ArrayList<>(); + + long count = nodeIdList.stream().mapToInt(WorkGatewayNode::getWeight).sum(); + + + if (count < 100) { + List list = nodeIdList.stream() + .sorted((o1, o2) -> o2.getWeight() - o1.getWeight()) + .toList(); + + int countWeight = 0; + for (long i = count; i < 100; i++) { + WorkGatewayNode workGatewayNode = list.get(countWeight++ % list.size()); + workGatewayNode.setWeight(workGatewayNode.getWeight() + 1); + } + } + + + while (nodeIdList.stream().anyMatch(node -> node.getWeight() > 0)) { + for (WorkGatewayNode workGatewayNode : nodeIdList) { + int weight = workGatewayNode.getWeight(); + if (weight > 0) { + loadNodeList.add(workGatewayNode.getNodeId()); + workGatewayNode.setWeight(weight - 1); + } + } + } + + + + log.info("----------------------------分割线--------------------------"); + +// redisService.setCacheList("work:node:gateway", loadNodeList); + + Map countMap = new HashMap<>(); + + // 统计每个对象出现的次数 + for (Object obj : loadNodeList) { + countMap.put(obj, countMap.getOrDefault(obj, 0) + 1); + } + + // 找出出现次数最少的对象 + Object nodeId = null; + int minCount = Integer.MAX_VALUE; + for (Map.Entry entry : countMap.entrySet()) { + if (entry.getValue() < minCount) { + minCount = entry.getValue(); + nodeId = entry.getKey(); + } + } + // 如果有最小数量的对象,则返回最小数量的对象;否则随机选择一个对象 + if (nodeId != null) { + return Result.success(nodeId); + } else { + Random random = new Random(); + return Result.success(random.nextInt(loadNodeList.size())); + } + + + + + } + +//这里是查询连接数的,现在使用redis的zset方法获得,就不需要下面这些了 + +// public ArrayList carWorkGatewayNode() throws Exception { +// +// Set ipCacheSet = redisService.getCacheZSet("ECS"); +// ArrayList nodeIdList = new ArrayList<>(); +// +// OkHttpClient client = new OkHttpClient(); +// for (String ip : ipCacheSet) { +// System.out.println("ip:"+ip); +// String URL = "http://"+ip+":8080/public/cluster"; +// Request request = new Request.Builder() +// .url(URL) +// .get() +// .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)") +// .addHeader("Accesstoken", "") +// .build(); +// +// try { +// Response response = client.newCall(request).execute(); +// JSONArray jsonArray = JSONArray.parseArray(response.body().string()); +// JSONObject jsonObject = jsonArray.getJSONObject(0); +// JSONObject mqttInfo = jsonObject.getJSONObject("mqttInfo"); +// int connectSize = mqttInfo.getIntValue("connectSize"); +// WorkGatewayNode workGatewayNode = new WorkGatewayNode(); +// log.info("服务器:"+ip+"-车辆连接数:"+connectSize); +// +// workGatewayNode.setWeight(connectSize); +// workGatewayNode.setNodeId(ip); +// +// nodeIdList.add(workGatewayNode); +// +// +// } catch (Exception e) { +// e.printStackTrace(); +// } +// +// +// } +// return nodeIdList; +// +// } + +} diff --git a/src/main/java/com/muyu/loadCenter/domain/ApifoxModel.java b/src/main/java/com/muyu/loadCenter/domain/ApifoxModel.java new file mode 100644 index 0000000..4e38136 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/domain/ApifoxModel.java @@ -0,0 +1,55 @@ +package com.muyu.loadCenter.domain; + +import lombok.Data; + +@Data +public class ApifoxModel { + /** + * 节点ID + */ + private String clusterId; + /** + * CPU使用信息 + */ + private CPUInfo cpuInfo; + /** + * 节点状态 + */ + private FlowInfo flowInfo; + /** + * HTTP请求地址 + */ + private String httpUrl; + /** + * JVM使用信息 + */ + private JVMInfo jvmInfo; + /** + * MQTT事件信息 + */ + private MqttInfo mqttInfo; + /** + * MQTTS请求地址 + */ + private String mqttsUrl; + /** + * MQTT请求地址 + */ + private String mqttUrl; + /** + * 节点名称 + */ + private String nodeName; + /** + * 启动时间 + */ + private String startJvmTime; + /** + * 节点版本 + */ + private String version; + /** + * websocket请求地址 + */ + private String websocketUrl; +} diff --git a/src/main/java/com/muyu/loadCenter/domain/CPUInfo.java b/src/main/java/com/muyu/loadCenter/domain/CPUInfo.java new file mode 100644 index 0000000..d8b1ae7 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/domain/CPUInfo.java @@ -0,0 +1,30 @@ +package com.muyu.loadCenter.domain; + +import lombok.Data; + +/** + * CPU使用信息 + */ +@Data +public class CPUInfo { + /** + * CPU核数 + */ + private long cpuNum; + /** + * 内核态使用率 + */ + private String cSys; + /** + * 空闲率 + */ + private String idle; + /** + * I/O等待 + */ + private String iowait; + /** + * 用户态使用率 + */ + private String user; +} diff --git a/src/main/java/com/muyu/loadCenter/domain/EcsInstanceInfo.java b/src/main/java/com/muyu/loadCenter/domain/EcsInstanceInfo.java new file mode 100644 index 0000000..3acd9b8 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/domain/EcsInstanceInfo.java @@ -0,0 +1,46 @@ +package com.muyu.loadCenter.domain; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + + +/** + * 实例查询结果模型 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class EcsInstanceInfo { + /** + * 实例的ID + */ + private String instanceId; + /** + * 实例的名称 + */ + private String instanceName; + /** + * 状态 + */ + private String status; + /** + * 公网Ip + */ + private String publicIpAddress; + /** + * 私网Ip + */ + private String privateIpAddress; + /** + * 创建时间 + */ + private String creationTime; + /** + * 是否可以回收 + */ + private boolean recyclable; + +} diff --git a/src/main/java/com/muyu/loadCenter/domain/FlowInfo.java b/src/main/java/com/muyu/loadCenter/domain/FlowInfo.java new file mode 100644 index 0000000..1993615 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/domain/FlowInfo.java @@ -0,0 +1,28 @@ +package com.muyu.loadCenter.domain; + +import lombok.Data; /** + * 节点状态 + */ +@Data +public class FlowInfo { + /** + * 上次读取吞吐量 + */ + private String lastReadThroughput; + /** + * 上次写入吞吐量 + */ + private String lastWriteThroughput; + /** + * 读取总吞吐量 + */ + private String readBytesHistory; + /** + * 实写字节 + */ + private String realWriteBytes; + /** + * 写入总吞吐量 + */ + private String writeBytesHistory; +} diff --git a/src/main/java/com/muyu/loadCenter/domain/JVMInfo.java b/src/main/java/com/muyu/loadCenter/domain/JVMInfo.java new file mode 100644 index 0000000..faca84e --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/domain/JVMInfo.java @@ -0,0 +1,58 @@ +package com.muyu.loadCenter.domain; + +import lombok.Data; + +/** + * JVM使用信息 + */ +@Data +public class JVMInfo { + /** + * 文件描述(句柄) + */ + private String fileDescriptors; + /** + * 堆内存 + */ + private String heapCommit; + /** + * 堆初始化空间 + */ + private String heapInit; + /** + * 堆最大内存 + */ + private String heapMax; + /** + * 堆使用空间 + */ + private String heapUsed; + /** + * JAVA目录 + */ + private String jdkHome; + /** + * JDK版本 + */ + private String jdkVersion; + /** + * 非堆空间 + */ + private String noHeapCommit; + /** + * 非堆初始化空间 + */ + private String noHeapInit; + /** + * 非堆最大空间 + */ + private String noHeapMax; + /** + * 非堆使用空间 + */ + private String noHeapUsed; + /** + * 线程数量 + */ + private long threadCount; +} diff --git a/src/main/java/com/muyu/loadCenter/domain/MqttInfo.java b/src/main/java/com/muyu/loadCenter/domain/MqttInfo.java new file mode 100644 index 0000000..34742e3 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/domain/MqttInfo.java @@ -0,0 +1,64 @@ +package com.muyu.loadCenter.domain;// ApifoxModel.java + +import com.muyu.loadCenter.domain.CPUInfo; +import com.muyu.loadCenter.domain.JVMInfo; +import lombok.Data; + + +// JVMInfo.java + + +// MqttInfo.java + + + +/** + * MQTT事件信息 + */ +@Data +public class MqttInfo { + /** + * 关闭事件数量 + */ + private long closeEventSize; + /** + * 连接事件数量 + */ + private long connectEventSize; + /** + * 链接总数 + */ + private long connectSize; + /** + * 断开链接数量 + */ + private long disconnectEventSize; + /** + * 推送数量 + */ + private long publishEventSize; + /** + * 发布重试事件数量 + */ + private long publishRetryEventSize; + /** + * 保留消息数量 + */ + private long retainSize; + /** + * 订阅事件数量 + */ + private long subscribeEventSize; + /** + * 订阅数量 + */ + private long subscribeSize; + /** + * 主题数量 + */ + private long topicSize; + /** + * 取消订阅数量 + */ + private long unSubscribeEventSize; +} diff --git a/src/main/java/com/muyu/loadCenter/domain/Result.java b/src/main/java/com/muyu/loadCenter/domain/Result.java new file mode 100644 index 0000000..5ecbcd3 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/domain/Result.java @@ -0,0 +1,117 @@ +package com.muyu.loadCenter.domain; + + + +import java.io.Serializable; + + +/** + * 响应信息主体 + * + * @author coderjacky + */ +public class Result implements Serializable +{ + private static final long serialVersionUID = 1L; + + /** 成功 */ + public static final int SUCCESS = 200; + + /** 失败 */ + public static final int FAIL = 500; + + private int code; + + private String msg; + + private T data; + + public static Result success() + { + return restResult(null, SUCCESS, null); + } + + public static Result success(T data) + { + return restResult(data, SUCCESS, null); + } + + public static Result success(T data, String msg) + { + return restResult(data, SUCCESS, msg); + } + + public static Result error() + { + return restResult(null, FAIL, null); + } + + public static Result error(String msg) + { + return restResult(null, FAIL, msg); + } + + public static Result error(T data) + { + return restResult(data, FAIL, null); + } + + public static Result error(T data, String msg) + { + return restResult(data, FAIL, msg); + } + + public static Result error(int code, String msg) + { + return restResult(null, code, msg); + } + + private static Result restResult(T data, int code, String msg) + { + Result apiResult = new Result<>(); + apiResult.setCode(code); + apiResult.setData(data); + apiResult.setMsg(msg); + return apiResult; + } + + public int getCode() + { + return code; + } + + public void setCode(int code) + { + this.code = code; + } + + public String getMsg() + { + return msg; + } + + public void setMsg(String msg) + { + this.msg = msg; + } + + public T getData() + { + return data; + } + + public void setData(T data) + { + this.data = data; + } + + public static Boolean isError(Result ret) + { + return !isSuccess(ret); + } + + public static Boolean isSuccess(Result ret) + { + return Result.SUCCESS == ret.getCode(); + } +} diff --git a/src/main/java/com/muyu/loadCenter/domain/WorkGatewayNode.java b/src/main/java/com/muyu/loadCenter/domain/WorkGatewayNode.java new file mode 100644 index 0000000..dd5e706 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/domain/WorkGatewayNode.java @@ -0,0 +1,23 @@ +package com.muyu.loadCenter.domain; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @ProjectName: LoadCenter + * @PackageName: com.muyu.loadCenter.domain + * @Description TODO + * @Author HuangDaJu + * @Date 2024/4/14 11:55 + * @Version 1.0 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class WorkGatewayNode { + + private String nodeId; + + private int weight; +} diff --git a/src/main/java/com/muyu/loadCenter/redis/configure/FastJson2JsonRedisSerializer.java b/src/main/java/com/muyu/loadCenter/redis/configure/FastJson2JsonRedisSerializer.java new file mode 100644 index 0000000..44b9da3 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/redis/configure/FastJson2JsonRedisSerializer.java @@ -0,0 +1,56 @@ +package com.muyu.loadCenter.redis.configure; + +import com.alibaba.fastjson2.JSON; +import com.alibaba.fastjson2.JSONReader; +import com.alibaba.fastjson2.JSONWriter; +import com.alibaba.fastjson2.filter.Filter; + +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.SerializationException; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +/** + * Redis使用FastJson序列化 + * + * @author muyu + */ +public class FastJson2JsonRedisSerializer implements RedisSerializer +{ + public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; + + public static final String[] JSON_WHITELIST_STR = { "org.springframework", "com.muyu" }; + + static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter( JSON_WHITELIST_STR); + + private Class clazz; + + public FastJson2JsonRedisSerializer(Class clazz) + { + super(); + this.clazz = clazz; + } + + @Override + public byte[] serialize(T t) throws SerializationException + { + if (t == null) + { + return new byte[0]; + } + return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET); + } + + @Override + public T deserialize(byte[] bytes) throws SerializationException + { + if (bytes == null || bytes.length <= 0) + { + return null; + } + String str = new String(bytes, DEFAULT_CHARSET); + + return JSON.parseObject(str, clazz, AUTO_TYPE_FILTER); + } +} diff --git a/src/main/java/com/muyu/loadCenter/redis/configure/RedisConfig.java b/src/main/java/com/muyu/loadCenter/redis/configure/RedisConfig.java new file mode 100644 index 0000000..ba6e875 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/redis/configure/RedisConfig.java @@ -0,0 +1,43 @@ +package com.muyu.loadCenter.redis.configure; + +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.cache.annotation.CachingConfigurerSupport; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * redis配置 + * + * @author muyu + */ +@Configuration +@EnableCaching +@AutoConfigureBefore(RedisAutoConfiguration.class) +public class RedisConfig extends CachingConfigurerSupport +{ + @Bean + @SuppressWarnings(value = { "unchecked", "rawtypes" }) + public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) + { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(connectionFactory); + + FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class); + + // 使用StringRedisSerializer来序列化和反序列化redis的key值 + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(serializer); + + // Hash的key也采用StringRedisSerializer的序列化方式 + template.setHashKeySerializer(new StringRedisSerializer()); + template.setHashValueSerializer(serializer); + + template.afterPropertiesSet(); + return template; + } +} diff --git a/src/main/java/com/muyu/loadCenter/redis/service/RedisService.java b/src/main/java/com/muyu/loadCenter/redis/service/RedisService.java new file mode 100644 index 0000000..93ed569 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/redis/service/RedisService.java @@ -0,0 +1,362 @@ +package com.muyu.loadCenter.redis.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.*; +import org.springframework.stereotype.Component; + +import java.util.*; +import java.util.concurrent.TimeUnit; + +/** + * spring redis 工具类 + * + * @author muyu + **/ +@SuppressWarnings(value = { "unchecked", "rawtypes" }) +@Component +public class RedisService +{ + @Autowired + public RedisTemplate redisTemplate; + + /** + * 缓存基本的对象,Integer、String、实体类等 + * + * @param key 缓存的键值 + * @param value 缓存的值 + */ + public void setCacheObject(final String key, final T value) + { + redisTemplate.opsForValue().set(key, value); + } + + /** + * 缓存基本的对象,Integer、String、实体类等 + * + * @param key 缓存的键值 + * @param value 缓存的值 + * @param timeout 时间 + * @param timeUnit 时间颗粒度 + */ + public void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit) + { + redisTemplate.opsForValue().set(key, value, timeout, timeUnit); + } + + /** + * 设置有效时间 + * + * @param key Redis键 + * @param timeout 超时时间 + * @return true=设置成功;false=设置失败 + */ + public boolean expire(final String key, final long timeout) + { + return expire(key, timeout, TimeUnit.SECONDS); + } + + /** + * 设置有效时间 + * + * @param key Redis键 + * @param timeout 超时时间 + * @param unit 时间单位 + * @return true=设置成功;false=设置失败 + */ + public boolean expire(final String key, final long timeout, final TimeUnit unit) + { + return redisTemplate.expire(key, timeout, unit); + } + + /** + * 获取有效时间 + * + * @param key Redis键 + * @return 有效时间 + */ + public long getExpire(final String key) + { + return redisTemplate.getExpire(key); + } + + /** + * 判断 key是否存在 + * + * @param key 键 + * @return true 存在 false不存在 + */ + public Boolean hasKey(String key) + { + return redisTemplate.hasKey(key); + } + + /** + * 获得缓存的基本对象。 + * + * @param key 缓存键值 + * @return 缓存键值对应的数据 + */ + public T getCacheObject(final String key) + { + ValueOperations operation = redisTemplate.opsForValue(); + return operation.get(key); + } + + /** + * 删除单个对象 + * + * @param key + */ + public boolean deleteObject(final String key) + { + return redisTemplate.delete(key); + } + + /** + * 删除集合对象 + * + * @param collection 多个对象 + * @return + */ + public boolean deleteObject(final Collection collection) + { + return redisTemplate.delete(collection) > 0; + } + + /** + * 缓存List数据 + * + * @param key 缓存的键值 + * @param dataList 待缓存的List数据 + * @return 缓存的对象 + */ + public long setCacheList(final String key, final List dataList) + { + Long count = redisTemplate.opsForList().rightPushAll(key, dataList); + return count == null ? 0 : count; + } + public Object leftPopAndRemove(String key) { + return redisTemplate.opsForList().leftPop(key); + } + + public Long rightPush(String key, String value) { + return redisTemplate.opsForList().rightPush(key, value); + } + /** + * 获得缓存的list对象 + * + * @param key 缓存的键值 + * @return 缓存键值对应的数据 + */ + public List getCacheList(final String key) { + return redisTemplate.opsForList().range(key, 0, -1); + } + + + public T getCacheListValue(final String key,long index) { + return (T) redisTemplate.opsForList().index(key,index); + } + /** + * 缓存Set + * + * @param key 缓存键值 + * @param dataSet 缓存的数据 + * @return 缓存数据的对象 + */ + public BoundSetOperations setCacheSet(final String key, final Set dataSet) + { + BoundSetOperations setOperation = redisTemplate.boundSetOps(key); + Iterator it = dataSet.iterator(); + while (it.hasNext()) + { + setOperation.add(it.next()); + } + return setOperation; + } + /** + * 缓存zSet + * + * @param key 缓存键值 + * @param setValue 缓存的数据 + * @param score 缓存的数据 + * @return 缓存数据的对象 + */ + public BoundZSetOperations setCacheZSet(final String key, final T setValue, double score) { + BoundZSetOperations boundZSetOperations = redisTemplate.boundZSetOps(key); + boundZSetOperations.add(setValue, score); + return boundZSetOperations; + } + public Set getCacheZSet(final String key) { + ZSetOperations zSetOperations = redisTemplate.opsForZSet(); + Set ipCacheSet = zSetOperations.range(key, 0, -1); + return ipCacheSet; + } + + + public Map getCacheZSetScore(final String key) { + ZSetOperations zSetOperations = redisTemplate.opsForZSet(); + + // 构建一个 Map 用于存储成员和分数的对应关系 + Map memberScores = new HashMap<>(); + + zSetOperations.rangeWithScores(key, 0, -1).forEach(tuple -> { + memberScores.put(tuple.getValue(), tuple.getScore()); + }); + return memberScores; + } + + + + + /** + * 缓存Set + * + * @param key 缓存键值 + * @param setValue 缓存的数据 + * @return 缓存数据的对象 + */ + public BoundSetOperations setCacheSet(final String key, final T setValue) + { + BoundSetOperations setOperation = redisTemplate.boundSetOps(key); + setOperation.add(setValue); + return setOperation; + } + + /** + * 缓存Set + * + * @param key 缓存键值 + * @param setValue 缓存的数据 + * @return 缓存数据的对象 + */ + public void deleteCacheSet(String key, T setValue) { + + BoundSetOperations setOperation = redisTemplate.boundSetOps(key); + + setOperation.remove(setValue); + } + + + /** + * 获得缓存的set + * + * @param key + * @return + */ + public Set getCacheSet(final String key) + { + return redisTemplate.opsForSet().members(key); + } + + + /** + * 删除缓存的zset的元素 + * + * @param key + * @return + */ + public Long deleteCacheZset(final String key, String value){ + + return redisTemplate.opsForZSet().remove(key,value); + } + + /** + * 缓存Map + * + * @param key + * @param dataMap + */ + public void setCacheMap(final String key, final Map dataMap) + { + if (dataMap != null) { + redisTemplate.opsForHash().putAll(key, dataMap); + } + } + + /** + * 获得缓存的Map + * + * @param key + * @return + */ + public Map getCacheMap(final String key) + { + return redisTemplate.opsForHash().entries(key); + } + + /** + * 往Hash中存入数据 + * + * @param key Redis键 + * @param hKey Hash键 + * @param value 值 + */ + public void setCacheMapValue(final String key, final String hKey, final T value) + { + redisTemplate.opsForHash().put(key, hKey, value); + } + + /** + * 获取Hash中的数据 + * + * @param key Redis键 + * @param hKey Hash键 + * @return Hash中的对象 + */ + public T getCacheMapValue(final String key, final String hKey) + { + HashOperations opsForHash = redisTemplate.opsForHash(); + return opsForHash.get(key, hKey); + } + + + + + /** + * 获取多个Hash中的数据 + * + * @param key Redis键 + * @param hKeys Hash键集合 + * @return Hash对象集合 + */ + public List getMultiCacheMapValue(final String key, final Collection hKeys) + { + return redisTemplate.opsForHash().multiGet(key, hKeys); + } + + /** + * 删除Hash中的某条数据 + * + * @param key Redis键 + * @param hKey Hash键 + * @return 是否成功 + */ + public boolean deleteCacheMapValue(final String key, final String hKey) + { + return redisTemplate.opsForHash().delete(key, hKey) > 0; + } + + /** + * 获得缓存的基本对象列表 + * + * @param pattern 字符串前缀 + * @return 对象列表 + */ + public Collection keys(final String pattern) { + return redisTemplate.keys(pattern); + } + /** + * 获得缓存的基本对象列表 + * + * @param key 字符串前缀 + * @param number 字符串前缀 + * @return 对象列表 + */ + public Long increment(final String key,Long number) { + return redisTemplate.opsForValue().increment(key,number); + } + + public void deleteCacheObject(String cursor) { + redisTemplate.delete(cursor); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..abf09ec --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,30 @@ +# Tomcat +server: + port: 9010 +Spring: + redis: +# 公网地址 + host: 121.89.212.0 +# host: 10.10.26.238 +# host: 127.0.0.1 + port: 6379 + + +config: + ali: + access-key-id: LTAI5tANGefs2gi8nsu4AoSZ + access-key-secret: Ut5RaJvvG7dP8hgK82qjdtvyUA6x8g + region-id: cn-zhangjiakou + instance: + imageId: m-8vb8z0ygyrzgqt54k3wi + instanceType: ecs.t6-c1m1.large + securityGroupId: sg-8vbfx0e48cekrpzgsa72 + vSwitchId: vsw-8vbn6cq2uy0mmw69l6ryq + internetMaxBandwidthOut: 5 + size: 20 + category: cloud_efficiency + instanceChargeType: PostPaid + internetChargeType: PayByTraffic + + + diff --git a/src/test/java/Test1.java b/src/test/java/Test1.java new file mode 100644 index 0000000..141a157 --- /dev/null +++ b/src/test/java/Test1.java @@ -0,0 +1,61 @@ +import com.muyu.loadCenter.LoadCenterApplication; +import com.muyu.loadCenter.domain.WorkGatewayNode; +import com.muyu.loadCenter.redis.service.RedisService; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.ArrayList; +import java.util.List; + +@SpringBootTest(classes = LoadCenterApplication.class) +public class Test1 { + + @Autowired + private RedisService redisService; + + @Test + public void load() { + // 初始化序列 + redisService.increment("cursor", 0L); + + List nodeIdList = new ArrayList<>() {{ + add(new WorkGatewayNode("node1:39.98.113.79", 20)); + add(new WorkGatewayNode("node2:39.98.113.80", 1)); + add(new WorkGatewayNode("node2:39.98.113.80", 2)); + add(new WorkGatewayNode("node2:39.98.113.80", 43)); + add(new WorkGatewayNode("node2:39.98.113.80", 92)); + }}; + + long count = nodeIdList.stream().mapToInt(WorkGatewayNode::getWeight).sum(); + + if (count < 100) { + List list = nodeIdList.stream() + .sorted((o1, o2) -> o2.getWeight() - o1.getWeight()) + .toList(); + + int countWeight = 0; + for (long i = count; i < 100; i++) { + WorkGatewayNode workGatewayNode = list.get(countWeight++ % list.size()); + workGatewayNode.setWeight(workGatewayNode.getWeight() + 1); + } + } + +// List loadNodeList = new ArrayList<>(); +// while (nodeIdList.stream().anyMatch(node -> node.getWeight() > 0)) { + + for (WorkGatewayNode workGatewayNode : nodeIdList) { + int weight = workGatewayNode.getWeight(); + if (weight > 0) { +// loadNodeList.add(workGatewayNode.getNodeId()); + workGatewayNode.setWeight(weight - 1); + System.out.println(workGatewayNode.getNodeId()); + break; // 只输出一个 IP 地址 + } + } +// } + + nodeIdList.forEach(System.out::println); +// redisService.setCacheList("work:node:gateway", loadNodeList); + } +}