From 046b924c96bcba138d28a6e37cb166a9eba63dfa Mon Sep 17 00:00:00 2001 From: yuan <1363654894@qq.com> Date: Tue, 8 Oct 2024 19:52:50 +0800 Subject: [PATCH] =?UTF-8?q?feat():=E5=A2=9E=E5=8A=A0ip=E8=BD=AE=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/VehicleInformationServiceImpl.java | 37 ++++++++++++++++++- .../muyu/car/instance/GenerateInstance.java | 9 ++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/impl/VehicleInformationServiceImpl.java b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/impl/VehicleInformationServiceImpl.java index ed34e74..2719b75 100644 --- a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/impl/VehicleInformationServiceImpl.java +++ b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/impl/VehicleInformationServiceImpl.java @@ -6,11 +6,16 @@ import com.muyu.car.domain.model.MqttServerModel; import com.muyu.car.domain.model.VehicleInformation; import com.muyu.car.gateway.service.VehicleInformationService; import com.muyu.car.mapper.VehicleInformationMapper; +import com.muyu.common.redis.service.RedisService; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; +import java.util.HashMap; +import java.util.LinkedList; import java.util.List; +import java.util.Map; /** * @Author:蓬叁 @@ -25,6 +30,8 @@ public class VehicleInformationServiceImpl implements VehicleInformationService{ @Autowired private VehicleInformationMapper vehicleInformationMapper; + @Autowired private RedisService redisService; + @Override public MqttServerModel getVehicleData(VehicleConnectionReq vehicleConnectionReq) { log.info("车辆连接请求:[{}]",vehicleConnectionReq); @@ -38,7 +45,35 @@ public class VehicleInformationServiceImpl implements VehicleInformationService{ log.info("车辆无法重复预上线"); } - return new MqttServerModel("tcp://121.199.172.3:1883","vehicle"); + // 获取名为 "ipList" 的列表 + List ipList = redisService.getCacheList("ipList"); + if (ipList.isEmpty()) { + return null; + } + // 获取当前使用的索引位置 + String indexStr = redisService.getCacheObject("currentIndex"); + int index = indexStr!= null? Integer.parseInt(indexStr) : 0; + String selectedIp = ipList.get(index); + // 获取该 IP 的使用次数 + String countStr = redisService.getCacheMapValue("ipCounts", selectedIp); + log.info("IP:[{}]车辆连接数:[{}]",selectedIp,countStr); + int count = countStr!= null? Integer.parseInt(countStr) : 0; + if (count < 12) { + // 使用次数加一 + count++; + redisService.setCacheMapValue("ipCounts", selectedIp, String.valueOf(count)); + // 更新索引 + index = (index + 1) % ipList.size(); + redisService.setCacheObject("currentIndex", String.valueOf(index)); + return new MqttServerModel("tcp://"+selectedIp.substring(1,selectedIp.length()-1)+":1883","vehicle"); + } else { + // 如果使用次数达到 12 次,跳过该 IP 并更新索引 + index = (index + 1) % ipList.size(); + redisService.setCacheObject("currentIndex", String.valueOf(index)); + return getVehicleData(vehicleConnectionReq); + } } + + } diff --git a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/instance/GenerateInstance.java b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/instance/GenerateInstance.java index e9e3fb4..7ce87c7 100644 --- a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/instance/GenerateInstance.java +++ b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/instance/GenerateInstance.java @@ -65,7 +65,7 @@ public class GenerateInstance implements ApplicationRunner { // 设置实例密码 .setPassword("EightGroup123.") // 设置创建实例的数量 - .setAmount(1); + .setAmount(2); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); try { @@ -107,13 +107,12 @@ public class GenerateInstance implements ApplicationRunner { Thread.sleep(30000); List exampleInformations = QueryInstance.queryInstance(InstanceIds); log.info("加载成功"); + ArrayList list = new ArrayList<>(); for (ExampleInformation exampleInformation : exampleInformations) { redisService.setCacheObject("InstanceIdKey:"+exampleInformation.getInstanceId(),exampleInformation); + list.add(exampleInformation.getIpAddress()); } -// for (ExampleInformation exampleInformation : exampleInformations) { -// ExampleInformation cacheObject = redisService.getCacheObject(exampleInformation.getInstanceId()); -// log.info(cacheObject); -// } + redisService.setCacheList("ipList",list); log.info("实例信息:{}",exampleInformations); }