commit 206835eac821ae521700f56321c8d48849ed6ed9 Author: 黄大举 <13970129+huandgaju@user.noreply.gitee.com> Date: Thu Apr 18 22:01:11 2024 +0800 4.18黄大举更新 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..dd6dcd0 --- /dev/null +++ b/pom.xml @@ -0,0 +1,264 @@ + + + 4.0.0 + + com.muyu + load_center + 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/service/AliYunEcsService.java b/src/main/java/com/muyu/loadCenter/aliyun/service/AliYunEcsService.java new file mode 100644 index 0000000..eac1741 --- /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.config.AliConfig; + +import com.muyu.loadCenter.aliyun.utils.UserUtil; +import com.muyu.loadCenter.domain.EcsInstanceInfo; +import com.muyu.loadCenter.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/config/AliConfig.java b/src/main/java/com/muyu/loadCenter/config/AliConfig.java new file mode 100644 index 0000000..a3a623b --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/config/AliConfig.java @@ -0,0 +1,54 @@ +package com.muyu.loadCenter.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/config/InstanceConfig.java b/src/main/java/com/muyu/loadCenter/config/InstanceConfig.java new file mode 100644 index 0000000..b92276b --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/config/InstanceConfig.java @@ -0,0 +1,62 @@ +package com.muyu.loadCenter.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/config/RestClientConfig.java b/src/main/java/com/muyu/loadCenter/config/RestClientConfig.java new file mode 100644 index 0000000..338339a --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/config/RestClientConfig.java @@ -0,0 +1,23 @@ +package com.muyu.loadCenter.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/controller/GatewayController.java b/src/main/java/com/muyu/loadCenter/controller/GatewayController.java new file mode 100644 index 0000000..f076407 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/controller/GatewayController.java @@ -0,0 +1,49 @@ +package com.muyu.loadCenter.controller; + +import com.muyu.loadCenter.domain.Result; +import com.muyu.loadCenter.service.GatewayLoadService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @ProjectName: load_center + * @PackageName: com.muyu.loadCenter.controller + * @Description 网关控制层->车辆控制 + * @Author HuangDaJu + * @Date 2024/4/18 14:14 + * @Version 1.0 + */ +@RestController +@RequestMapping("/gateway") +public class GatewayController { + + @Autowired + private GatewayLoadService gatewayLoadService; + + /** + * 发送上线请求,返回一个节点id + */ + @GetMapping("/load/node") + public Result loadNode(){ + return Result.success(gatewayLoadService.loadNode()); + } + /** + * 定时任务,每30秒扫描一次服务器集群的负载情况。 + * 如果任一服务器的车辆连接数达到或超过阈值,则触发扩容; + * 如果服务器的连接数低于阈值,则触发缩容。 + */ + @Scheduled(cron = "0/5 * * * * ?") + public void scheduleECS() throws Exception { + gatewayLoadService.scheduleECS(); + } + + + + + + +} diff --git a/src/main/java/com/muyu/loadCenter/controller/GatewayVehicleController.java b/src/main/java/com/muyu/loadCenter/controller/GatewayVehicleController.java new file mode 100644 index 0000000..a13dc97 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/controller/GatewayVehicleController.java @@ -0,0 +1,57 @@ +package com.muyu.loadCenter.controller; + +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import com.muyu.loadCenter.domain.Result; +import com.muyu.loadCenter.service.GatewayVehicleService; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.ArrayList; +import java.util.Map; + +/** + * @Description 操作车辆上下线 + * @Author HuangDaJu + * @Date 2024/4/18 18:37 + * @Version 1.0 + */ + + +@RestController +@RequestMapping("/vehicle") +public class GatewayVehicleController { + + @Autowired + private GatewayVehicleService gatewayVehicleService; + + /** + * 车辆上线 + */ + + @GetMapping("/load/topLine/{vin}") + public Result topLine(@PathVariable String vin){ + return gatewayVehicleService.topLine(vin); + } + + /** + * 车辆下线 + */ + + @GetMapping("/load/downLine/{vin}") + public void loadDownLine(@PathVariable String vin){ + gatewayVehicleService.loadDownLine(vin); + } + + + + + +} 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/gateway/abs/GatewayCacheAbs.java b/src/main/java/com/muyu/loadCenter/gateway/abs/GatewayCacheAbs.java new file mode 100644 index 0000000..a2548ac --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/gateway/abs/GatewayCacheAbs.java @@ -0,0 +1,27 @@ +package com.muyu.loadCenter.gateway.abs; + +import com.muyu.loadCenter.redis.service.RedisService; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * @ProjectName: load_center + * @PackageName: com.muyu.loadCenter.gateway.abs + * @Description 缓存抽象类 + * @Author HuangDaJu + * @Date 2024/4/18 11:37 + * @Version 1.0 + */ +public abstract class GatewayCacheAbs { + + @Autowired + public RedisService redisService; + + public abstract String getPre(); + + public String encode(K key){ + return getPre()+key; + } + + + +} diff --git a/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayLoadNodeCache.java b/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayLoadNodeCache.java new file mode 100644 index 0000000..b16c98b --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayLoadNodeCache.java @@ -0,0 +1,65 @@ +package com.muyu.loadCenter.gateway.cache; + +import com.muyu.loadCenter.gateway.abs.GatewayCacheAbs; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @Description 网关负载节点 按比例存 redis 100 个数据那个 + * @Author HuangDaJu + * @Date 2024/4/18 11:35 + * @Version 1.0 + */ +@Component +public class GatewayLoadNodeCache extends GatewayCacheAbs { + + private final static String gatewayLoadNodeKey="node"; + + + @Override + public String getPre() { + return "gateway:lode:"; + } + + /** + * 存负载节点 + * @param nodeList 节点权重集合 + */ + + public void put(List nodeList){ + redisService.deleteObject(encode(gatewayLoadNodeKey)); + redisService.setCacheList(encode(gatewayLoadNodeKey),nodeList); + } + + /** + * 获取所有负载节点 + * @return 负载节点集合 + */ + public List get(){ + return redisService.getCacheList(encode(gatewayLoadNodeKey)); + } + + + + /** + * 通过下标获取节点 + * @param index 下标 + * @return 指定节点 + */ + + public String getByIndex(Long index){ + if (index ==null || index > 100){ + throw new RuntimeException("下标违法,0-100"); + } + return redisService.getCacheListValue(encode(gatewayLoadNodeKey),index); + } + + + + + + + + +} diff --git a/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayLoadSeriesCache.java b/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayLoadSeriesCache.java new file mode 100644 index 0000000..81c74ee --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayLoadSeriesCache.java @@ -0,0 +1,63 @@ +package com.muyu.loadCenter.gateway.cache; + +import com.muyu.loadCenter.gateway.abs.GatewayCacheAbs; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +/** + * @Description 网关负载序列 自增序列 series + * @Author HuangDaJu + * @Date 2024/4/18 11:57 + * @Version 1.0 + */ +@Component +public class GatewayLoadSeriesCache extends GatewayCacheAbs { + + private final static String gatewayLoadSeriesKey = "series"; + + + @Override + public String getPre () { + return "gateway:load:"; + } + + /** + * 获取自增序列值 + * @return 自增后的值 + */ + public Long incrementAndGet(){ + return redisService.increment(encode(gatewayLoadSeriesKey), 1L); + } + + /** + * bean创建完成之后执行方法 + */ + @PostConstruct + public void init(){ + redisService.setCacheObject(encode(gatewayLoadSeriesKey), 0); + } + + /** + * 获取当前序列值 + * @return 序列值 + */ + public Long get(){ + return redisService.getCacheObject(encode(gatewayLoadSeriesKey)); + } + + + + /** + * 重置 + */ + public void reset(){ + this.init(); + } + + + + +} + + diff --git a/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayNodeCache.java b/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayNodeCache.java new file mode 100644 index 0000000..869bcbf --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayNodeCache.java @@ -0,0 +1,59 @@ +package com.muyu.loadCenter.gateway.cache; + +import com.muyu.loadCenter.gateway.abs.GatewayCacheAbs; +import com.muyu.loadCenter.gateway.model.GatewayNodeInfo; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @Description 网关节点缓存 String数据类型 取的是对象 info 对象 + * @Author HuangDaJu + * @Date 2024/4/18 14:08 + * @Version 1.0 + */ +@Component +public class GatewayNodeCache extends GatewayCacheAbs { + + private final static String gatewayNodeCache = "info:"; + @Override + public String getPre() { + return "gateway:node:"; + } + + /** + * 增加缓存数据 + * @param gatewayNodeInfo + */ + public void put(String ip,GatewayNodeInfo gatewayNodeInfo){ + redisService.setCacheObject(encode(gatewayNodeCache)+ip,gatewayNodeInfo); + } + + + + /** + * 获取单个缓存数据 + */ + public GatewayNodeInfo get(String ip){ + return redisService.getCacheObject(encode(gatewayNodeCache)+ip); + } + + + public List get(){ + return redisService.getCacheObject(encode(gatewayNodeCache)); + } + + /** + * 删除网关节点 + */ + + public void remove(String nodeId){ + redisService.deleteObject(encode(nodeId)); + } + + + + + + +} diff --git a/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayNodeSetVinCache.java b/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayNodeSetVinCache.java new file mode 100644 index 0000000..82d23bd --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayNodeSetVinCache.java @@ -0,0 +1,47 @@ +package com.muyu.loadCenter.gateway.cache; + +import com.muyu.loadCenter.gateway.abs.GatewayCacheAbs; +import org.springframework.stereotype.Component; + +/** + * @Description 一个节点对多辆车的vin集合 + * @Author HuangDaJu + * @Date 2024/4/18 19:13 + * @Version 1.0 + */ +@Component +public class GatewayNodeSetVinCache extends GatewayCacheAbs { + private final static String gatewayNodeSetVinCache="many:"; + @Override + public String getPre() { + return "gateway:node:"; + } + + + /** + * 节点添加vin + */ + + public void put(String node,String vin){ + redisService.setCacheSet(encode(gatewayNodeSetVinCache)+node,vin); + } + + + /** + * 删除节点对应的vin + */ + + public void remove(String nodeId,String vin){ + redisService.deleteCacheSet(encode(gatewayNodeSetVinCache)+nodeId,vin); + } + + + + + + + + + + +} diff --git a/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayVehicleNode.java b/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayVehicleNode.java new file mode 100644 index 0000000..d7f43fb --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayVehicleNode.java @@ -0,0 +1,50 @@ +package com.muyu.loadCenter.gateway.cache; + +import com.muyu.loadCenter.gateway.abs.GatewayCacheAbs; +import org.springframework.stereotype.Component; + +/** + * @Description 车辆上线业务+vin:网关节点id + * @Author HuangDaJu + * @Date 2024/4/18 17:20 + * @Version 1.0 + */ +@Component +public class GatewayVehicleNode extends GatewayCacheAbs { + + private final static String gatewayCarBusinessKey="business:"; + @Override + public String getPre() { + return "gateway:car:"; + } + + /** + * 添加车辆vin gateway:car:business+vin, 网关节点id + */ + public void put(String vin,String nodeId){ + redisService.setCacheObject(encode(gatewayCarBusinessKey)+vin,nodeId); + } + + + + + /** + * 删除车辆vin gateway:car:business + */ + public void remove(String vin){ + redisService.deleteObject(encode(gatewayCarBusinessKey)+vin); + } + + + /** + * 获取车辆 nodeId + */ + public String get(String vin){ + return redisService.getCacheObject(encode(gatewayCarBusinessKey)+vin); + } + + + + + +} diff --git a/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayZSetNodeCache.java b/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayZSetNodeCache.java new file mode 100644 index 0000000..14700de --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/gateway/cache/GatewayZSetNodeCache.java @@ -0,0 +1,55 @@ +package com.muyu.loadCenter.gateway.cache; + +import com.muyu.loadCenter.gateway.abs.GatewayCacheAbs; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Map; + +/** + * @Description 服务器节点+车辆连接数 + * @Author HuangDaJu + * @Date 2024/4/18 15:32 + * @Version 1.0 + */ +@Component +public class GatewayZSetNodeCache extends GatewayCacheAbs { + + private final static String gatewayZSetCount="count"; + + + @Override + public String getPre() { + return "gateway:zSet:"; + } + + + /** + * 获取所有zset数据 + * @return 负载节点集合 + */ + public Map get(){ + return redisService.getCacheZSetScore(encode(gatewayZSetCount)); + } + + /** + * 修改服务器与在线车辆 + */ + + public void put(String nodeId ,Integer onlineVehicle){ + redisService.setCacheZSet(encode(gatewayZSetCount),nodeId,onlineVehicle); + } + + /** + * 删除服务器的时候删除zset服务器列表,防止重新添加车辆 + */ + + + public void remove(String nodeId){ + redisService.deleteCacheZset(encode(gatewayZSetCount),nodeId); + } + + + + +} diff --git a/src/main/java/com/muyu/loadCenter/gateway/model/GatewayNodeInfo.java b/src/main/java/com/muyu/loadCenter/gateway/model/GatewayNodeInfo.java new file mode 100644 index 0000000..89446b8 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/gateway/model/GatewayNodeInfo.java @@ -0,0 +1,38 @@ +package com.muyu.loadCenter.gateway.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @ProjectName: load_center + * @PackageName: com.muyu.loadCenter.gateway.model + * @Description 网关节点信息 + * @Author HuangDaJu + * @Date 2024/4/18 11:28 + * @Version 1.0 + */ + + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class GatewayNodeInfo { + + /** + * 节点Id + */ + private String nodeId; + /** + * 公网ip + */ + private String publicIdAddress; + /** + * 内网ip + */ + private String privateIdAddress; + + +} 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..9e366be --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/redis/service/RedisService.java @@ -0,0 +1,363 @@ +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 void deleteCacheZset(final String key, final String value){ + + 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/java/com/muyu/loadCenter/service/GatewayLoadService.java b/src/main/java/com/muyu/loadCenter/service/GatewayLoadService.java new file mode 100644 index 0000000..a014832 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/service/GatewayLoadService.java @@ -0,0 +1,24 @@ +package com.muyu.loadCenter.service; + +/** + * @ProjectName: load_center + * @PackageName: com.muyu.loadCenter.service + * @Description TODO + * @Author HuangDaJu + * @Date 2024/4/18 14:16 + * @Version 1.0 + */ +public interface GatewayLoadService { + /** + * 负载节点 + * @return 返回负载节点 + */ + String loadNode(); + + + void scheduleECS(); + + + + +} diff --git a/src/main/java/com/muyu/loadCenter/service/GatewayVehicleService.java b/src/main/java/com/muyu/loadCenter/service/GatewayVehicleService.java new file mode 100644 index 0000000..06d8fa5 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/service/GatewayVehicleService.java @@ -0,0 +1,26 @@ +package com.muyu.loadCenter.service; + +import com.muyu.loadCenter.domain.Result; + +/** + * @ProjectName: load_center + * @PackageName: com.muyu.loadCenter.service + * @Description TODO + * @Author HuangDaJu + * @Date 2024/4/18 18:39 + * @Version 1.0 + */ +public interface GatewayVehicleService { + + + Result topLine(String vin); + + + void loadDownLine(String vin); + + + + + + +} diff --git a/src/main/java/com/muyu/loadCenter/service/impl/GatewayLoadServiceImpl.java b/src/main/java/com/muyu/loadCenter/service/impl/GatewayLoadServiceImpl.java new file mode 100644 index 0000000..d958044 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/service/impl/GatewayLoadServiceImpl.java @@ -0,0 +1,285 @@ +package com.muyu.loadCenter.service.impl; + +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.WorkGatewayNode; +import com.muyu.loadCenter.gateway.cache.*; +import com.muyu.loadCenter.gateway.model.GatewayNodeInfo; +import com.muyu.loadCenter.redis.service.RedisService; +import com.muyu.loadCenter.service.GatewayLoadService; +import lombok.AllArgsConstructor; +import lombok.extern.log4j.Log4j2; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * @ProjectName: load_center + * @PackageName: com.muyu.loadCenter.service.impl + * @Description 网关负载业务 + * @Author HuangDaJu + * @Date 2024/4/18 14:16 + * @Version 1.0 + */ +@Service +@Log4j2 +@AllArgsConstructor +public class GatewayLoadServiceImpl implements GatewayLoadService { + + private final Long nodeLength = 100L; + /** + * 负载信息 + */ + private final GatewayLoadNodeCache gatewayLoadNodeCache; + + /** + * 负载序列 + * @return + */ + private final GatewayLoadSeriesCache gatewayLoadSeriesCache; + + /** + * 节点信息 + * @return + */ + private final GatewayNodeCache gatewayNodeCache; + + @Autowired + private RedisService redisService; + + + + private final GatewayZSetNodeCache gatewayZSetNodeCache; + + /** + * 阿里云ECS服务,用于管理云服务器 + */ + @Autowired + private AliYunEcsService aliYunEcsService; + + /** + * aa是计算服务器到达 扩容阈值的节点个数 符合条件+1 和节点个数个对比 相等就扩容 + */ + static int aa=0; + /** + * bb是计算服务器到达 缩容阈值的节点个数 符合条件+1 >=指定个数 相等就执行缩容 + */ + static int bb=0; + + + @Override + public String loadNode() { + + List loadNodeList = new ArrayList<>(); + ArrayList nodeIdList = new ArrayList<>(); + + + Map map = gatewayZSetNodeCache.get(); + + 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); + } + + 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); + } + } + whFor: + while (true) { + for (WorkGatewayNode workGatewayNode : nodeIdList) { + int weight = workGatewayNode.getWeight(); + if (weight > 0) { + loadNodeList.add( + workGatewayNode.getNodeId() + ); + workGatewayNode.setWeight(weight - 1); + } + } + int sum = nodeIdList.stream() + .mapToInt(WorkGatewayNode::getWeight) + .sum(); + if (sum <= 0) { + break whFor; + } + } + + gatewayLoadNodeCache.put(loadNodeList); + + Long seriesLoad = gatewayLoadSeriesCache.incrementAndGet(); + System.out.println("seriesLoad:" + seriesLoad); + + Long seriesLoadIndex = seriesLoad % nodeLength; + + String loadNodeId = gatewayLoadNodeCache.getByIndex(seriesLoadIndex); + + GatewayNodeInfo gatewayNodeInfo = gatewayNodeCache.get(loadNodeId); + + return gatewayNodeInfo.getPublicIdAddress(); + + } + + @Override + public void scheduleECS() { + // 客户端初始化 + OkHttpClient client = new OkHttpClient(); + ArrayList ipCacheSet = new ArrayList<>(); + + // 从Redis获取服务器IP集合 + Map map = gatewayZSetNodeCache.get(); + + for (Map.Entry entry : map.entrySet()) { + ipCacheSet.add(entry.getKey().toString()); + } + + 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中服务器的连接数ZSet数据类型 + gatewayZSetNodeCache.put(ip,connectSize); + + // 根据连接数判断是否需要进行扩容或缩容 + if (connectSize >= 8) { + aa++; + // 当满足扩容条件时,记录日志并执行扩容操作 + if (aa == ipCacheSet.size()) { + log.info(aa+"------------------------------------------"+ipCacheSet.size()); + log.info("需要扩容"); + // 调用阿里云ECS服务创建新实例 + String instanceId = aliYunEcsService.runInstances(); + log.info("扩容的节点ip为:" + instanceId); + + // 休眠5秒以确保新实例创建完成 + Thread.sleep(5000); + + // 获取新实例信息并将其放入redis + EcsInstanceInfo ecsInstanceInfo = aliYunEcsService.selectList(instanceId); + + GatewayNodeInfo gatewayNodeInfo = new GatewayNodeInfo(); + gatewayNodeInfo.setNodeId(ecsInstanceInfo.getInstanceId()); + gatewayNodeInfo.setPublicIdAddress(ecsInstanceInfo.getPublicIpAddress()); + gatewayNodeInfo.setPrivateIdAddress(ecsInstanceInfo.getPrivateIpAddress()); + + + gatewayNodeCache.put(ecsInstanceInfo.getPublicIpAddress(), gatewayNodeInfo); + + + //这里模拟(也可以在别的类里完成) ECS创建成功后,服务器发送一条消息服务器正常启动,mq可以正常使用,存入redis + gatewayZSetNodeCache.put(ecsInstanceInfo.getPublicIpAddress(), 0); + log.info("实例id和公网ip存入redis"); + aa = 0; // 重置计数器 + } + }else { + aa = 0; + } + + + if (connectSize <= 2) { + bb++; + System.out.println("bb:"+bb); + + //缩容逻辑:删除连接数过低的服务器实例,出现连续两个节点连接数低于5的,则执行缩容操作 + if (bb >= 2){ + log.info("释放实例"+ip); + + gatewayZSetNodeCache.remove(ip); + + GatewayNodeInfo gatewayNodeInfo = gatewayNodeCache.get(ip); + + //这里再来个异步操作 将 GatewayNodeInfo 对象传走,进行异步操作 + +// 1.分批次解除连接的车辆 +// 2.删除各个相关的redis缓存 + +// aliYunEcsService.releaseECS(gatewayNodeInfo.getNodeId()); // 释放ECS实例 + + bb = 0; + + } + + }else{ + bb = 0; + } + + + + + + + + + + + + + + + + + + + + + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + +} + +// else if (connectSize <= 2) { + +//删除ECS里面的ip,车辆再次上线,找不到这个要缩容的服务器,让找不到 +// redisService.deleteCacheZset("ECS" ,ip); + +// 缩容逻辑:删除连接数过低的服务器实例 +// String url = "http://127.0.0.1:9006/ecsInstance/select/" + ip; +// Result result = restTemplate.postForObject(url, null, Result.class); +// String instanceId = (String) result.getData(); + + + +// Long i = redisService.deleteCacheZset("ECS", ip);// 从Redis中删除该服务器的记录 + +// } diff --git a/src/main/java/com/muyu/loadCenter/service/impl/GatewayVehicleServiceImpl.java b/src/main/java/com/muyu/loadCenter/service/impl/GatewayVehicleServiceImpl.java new file mode 100644 index 0000000..848d068 --- /dev/null +++ b/src/main/java/com/muyu/loadCenter/service/impl/GatewayVehicleServiceImpl.java @@ -0,0 +1,82 @@ +package com.muyu.loadCenter.service.impl; + +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import com.muyu.loadCenter.domain.Result; +import com.muyu.loadCenter.gateway.cache.GatewayNodeSetVinCache; +import com.muyu.loadCenter.gateway.cache.GatewayVehicleNode; +import com.muyu.loadCenter.gateway.cache.GatewayZSetNodeCache; +import com.muyu.loadCenter.service.GatewayVehicleService; +import lombok.AllArgsConstructor; +import lombok.extern.log4j.Log4j2; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import java.util.ArrayList; +import java.util.Map; + +/** + * @ProjectName: load_center + * @PackageName: com.muyu.loadCenter.service.impl + * @Description TODO + * @Author HuangDaJu + * @Date 2024/4/18 18:39 + * @Version 1.0 + */ +@Service +@AllArgsConstructor +@Log4j2 +public class GatewayVehicleServiceImpl implements GatewayVehicleService { + + + + private final GatewayNodeSetVinCache gatewayNodeSetVinCache; + + + private final GatewayZSetNodeCache gatewayZSetNodeCache; + + + private final GatewayVehicleNode gatewayVehicleNode; + + @Autowired + private RestTemplate restTemplate; + + + + /** + * 车辆上线 + * @param vin + * @return nodeId + */ + @Override + public Result topLine(String vin) { + + String url="http://127.0.0.1:9010/gateway/load/node"; + + Result data = restTemplate.getForObject(url, Result.class); + + gatewayVehicleNode.put(vin,data.getData().toString()); + + gatewayNodeSetVinCache.put(data.getData().toString(),vin); + + return Result.success(data.getData().toString()); + } + + @Override + public void loadDownLine(String vin) { + + String nodeId = gatewayVehicleNode.get(vin); + + gatewayNodeSetVinCache.remove(nodeId,vin); + + gatewayVehicleNode.remove(vin); + } + + + + +} 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/LoadTest.java b/src/test/java/LoadTest.java new file mode 100644 index 0000000..f817001 --- /dev/null +++ b/src/test/java/LoadTest.java @@ -0,0 +1,30 @@ + +import com.muyu.loadCenter.LoadCenterApplication; +import com.muyu.loadCenter.domain.WorkGatewayNode; +import com.muyu.loadCenter.gateway.model.GatewayNodeInfo; +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; +/** + * @author DongZl + * @description: 负载测试 + * @Date 2024/4/12 下午5:13 + */ +@SpringBootTest(classes = LoadCenterApplication.class) +public class LoadTest { + + @Autowired + private RedisService redisService; + + @Test + public void load() { + GatewayNodeInfo gatewayNodeInfo = redisService.getCacheObject("gateway:node:info:39.101.193.188"); + System.out.println(gatewayNodeInfo); + + + } +}