+ * 版本号具有以下含意: + *
+ * 变体号具有以下含意: + *
+ * 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 =+ * + * + * + * @return 此{@code UUID} 的字符串表现形式 + * + * @see #toString(boolean) + */ + @Override + public String toString () { + return toString(false); + } + + /** + * 返回此{@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] + * } + *
+ * UUID 的字符串表示形式由此 BNF 描述: + * + *
+ * {@code + * UUID =+ * + * + * + * @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 + + /** + * 将此对象与指定对象比较。 + *- - - - + * time_low = 4* + * time_mid = 2* + * time_high_and_version = 2* + * variant_and_sequence = 2* + * node = 6* + * hexOctet = + * hexDigit = [0-9a-fA-F] + * } + *
+ * 当且仅当参数不为 {@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