From 0cd280e8f185357772775be775d4bc1e9859dcae 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: Thu, 18 Apr 2024 10:00:09 +0800
Subject: [PATCH] 4.18
---
pom.xml | 2 +-
.../controller/DownLineController.java | 41 ++++++
.../controller/LoadCenterController.java | 48 +++++--
.../controller/WorkGatewayNodeController.java | 127 ++++++------------
.../redis/service/RedisService.java | 5 +-
.../service/ReleaseEcsDownLine.java | 51 +++++++
6 files changed, 172 insertions(+), 102 deletions(-)
create mode 100644 src/main/java/com/muyu/loadCenter/controller/DownLineController.java
create mode 100644 src/main/java/com/muyu/loadCenter/service/ReleaseEcsDownLine.java
diff --git a/pom.xml b/pom.xml
index 750bc4d..b94de24 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
4.0.0
com.muyu
- LoadCenter
+ load_center
1.0-SNAPSHOT
20
diff --git a/src/main/java/com/muyu/loadCenter/controller/DownLineController.java b/src/main/java/com/muyu/loadCenter/controller/DownLineController.java
new file mode 100644
index 0000000..0dc77c7
--- /dev/null
+++ b/src/main/java/com/muyu/loadCenter/controller/DownLineController.java
@@ -0,0 +1,41 @@
+package com.muyu.loadCenter.controller;
+
+import com.muyu.loadCenter.redis.service.RedisService;
+import lombok.extern.log4j.Log4j2;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * 处理车辆下线业务
+ * @Author HuangDaJu
+ * @Date 2024/4/17 21:28
+ * @Version 1.0
+ */
+@Log4j2
+@RestController
+@RequestMapping("downLine")
+public class DownLineController {
+
+
+ @Autowired
+ private RedisService redisService;
+
+ @PostMapping("/carDownLine/{vin}")
+ public void getVinDownLine(@PathVariable String vin) throws Exception {
+
+
+ String nodeId = redisService.getCacheObject("delete:" + vin);
+
+ redisService.deleteObject("delete:"+vin);
+
+ redisService.deleteCacheSet("release:"+nodeId,vin);
+
+ log.info("车辆下线成功");
+ }
+
+
+
+}
diff --git a/src/main/java/com/muyu/loadCenter/controller/LoadCenterController.java b/src/main/java/com/muyu/loadCenter/controller/LoadCenterController.java
index eecdfb1..9b5a09f 100644
--- a/src/main/java/com/muyu/loadCenter/controller/LoadCenterController.java
+++ b/src/main/java/com/muyu/loadCenter/controller/LoadCenterController.java
@@ -1,10 +1,12 @@
package com.muyu.loadCenter.controller;
+import com.alibaba.fastjson2.JSON;
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 com.muyu.loadCenter.service.ReleaseEcsDownLine;
import lombok.extern.log4j.Log4j2;
import okhttp3.OkHttpClient;
import okhttp3.Request;
@@ -31,15 +33,19 @@ public class LoadCenterController {
int aa=0;
// 临时变量,无特定用途(根据现有代码)
int bb=0;
- @Autowired
- StringRedisTemplate redisTemplate; // Redis字符串模板,用于与Redis进行交互
+// Redis字符串模板,用于与Redis进行交互
@Autowired
private RedisService redisService; // Redis服务,封装了与Redis操作相关的功能
+
@Autowired
private AliYunEcsService aliYunEcsService; // 阿里云ECS服务,用于管理云服务器
+
@Autowired
- private RestTemplate restTemplate; // 用于与其他服务进行HTTP交互的模板
+ private RestTemplate restTemplate;
+
+ @Autowired
+ private ReleaseEcsDownLine releaseEcsDownLine;// 用于与其他服务进行HTTP交互的模板
/**
* 定时任务,每30秒扫描一次服务器集群的负载情况。
@@ -57,6 +63,7 @@ public class LoadCenterController {
// 遍历每台服务器进行负载检查
for (String ip : ipCacheSet) {
+
// 构建请求URL和请求头
String URL = "http://"+ip+":8080/public/cluster";
Request request = new Request.Builder()
@@ -76,11 +83,11 @@ public class LoadCenterController {
log.info("服务器:"+ip+"-车辆连接数:"+connectSize);
- // 更新Redis中服务器的连接数
+ // 更新Redis中服务器的连接数ZSet数据类型
redisService.setCacheZSet("ECS", ip, connectSize);
// 根据连接数判断是否需要进行扩容或缩容
- if (connectSize >= 5) {
+ if (connectSize >= 6) {
aa++;
// 当满足扩容条件时,记录日志并执行扩容操作
if (aa == ipCacheSet.size()) {
@@ -94,26 +101,43 @@ public class LoadCenterController {
// 获取新实例信息并将其持久化到本地数据库
EcsInstanceInfo ecsInstanceInfo = aliYunEcsService.selectList(instanceId);
- String url = "http://127.0.0.1:9006/ecsInstance/add";
- restTemplate.postForObject(url, ecsInstanceInfo, Result.class);
+
+
+ //String数据类型:创建好并查询的对象转换为JSON字符串,信息存入redis第一个数据类型 先存入redis 确保正常运行了在假如Zset表
+ redisService.setCacheObject("ecsInstance:"+ecsInstanceInfo.getPublicIpAddress(), ecsInstanceInfo.getInstanceId());
+
+
+
+ //这里模拟(也可以在别的类里完成) ECS创建成功后,服务器发送一条消息服务器正常启动,mq可以正常使用,存入redis
+ redisService.setCacheZSet("ECS", ecsInstanceInfo.getPublicIpAddress(), 0);
+
+// 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) {
- // 缩容逻辑:删除连接数过低的服务器实例
+ }
+ 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();
+
+ releaseEcsDownLine.releaseEcsDownLine(ip);
+
// aliYunEcsService.releaseECS(instanceId); // 释放ECS实例
- Long i = redisService.deleteCacheZset("ECS", ip);// 从Redis中删除该服务器的记录
+// Long i = redisService.deleteCacheZset("ECS", ip);// 从Redis中删除该服务器的记录
- log.info(i+"连接数小于2,服务器缩容:" + ip);
+ log.info("连接数小于2,服务器缩容:" + ip);
aa = 0; // 重置计数器
}
} catch (Exception e) {
diff --git a/src/main/java/com/muyu/loadCenter/controller/WorkGatewayNodeController.java b/src/main/java/com/muyu/loadCenter/controller/WorkGatewayNodeController.java
index 9fe5867..35bbe1d 100644
--- a/src/main/java/com/muyu/loadCenter/controller/WorkGatewayNodeController.java
+++ b/src/main/java/com/muyu/loadCenter/controller/WorkGatewayNodeController.java
@@ -1,45 +1,35 @@
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 org.springframework.web.bind.annotation.*;
import java.util.*;
+/**
+ * 获取工作网关节点信息并进行负载均衡处理
+ */
@Log4j2
@RestController
-@RequestMapping("carGoGoGo")
+@RequestMapping("carGoGo")
public class WorkGatewayNodeController {
@Autowired
private RedisService redisService;
- @PostMapping("/workGatewayNode")
- public Result getWorkGatewayNode() throws Exception {
-// redisService.setCacheObject("cursor", 0);
+ @PostMapping("/workGatewayNode/{vin}")
+ public Result getWorkGatewayNode(@PathVariable String vin) throws Exception {
+// int size = vinList.size();
+ redisService.setCacheObject("cursor", 0);
+ // 从Redis获取ECS的集合,并转换为工作网关节点列表
ArrayList nodeIdList = new ArrayList<>();
-
-
-
Map