feat():增加ip轮询

dev.cargateway
yuan 2024-10-08 19:52:50 +08:00
parent 9185f33c0c
commit 046b924c96
2 changed files with 40 additions and 6 deletions

View File

@ -6,11 +6,16 @@ import com.muyu.car.domain.model.MqttServerModel;
import com.muyu.car.domain.model.VehicleInformation; import com.muyu.car.domain.model.VehicleInformation;
import com.muyu.car.gateway.service.VehicleInformationService; import com.muyu.car.gateway.service.VehicleInformationService;
import com.muyu.car.mapper.VehicleInformationMapper; import com.muyu.car.mapper.VehicleInformationMapper;
import com.muyu.common.redis.service.RedisService;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @Author * @Author
@ -25,6 +30,8 @@ public class VehicleInformationServiceImpl implements VehicleInformationService{
@Autowired private VehicleInformationMapper vehicleInformationMapper; @Autowired private VehicleInformationMapper vehicleInformationMapper;
@Autowired private RedisService redisService;
@Override @Override
public MqttServerModel getVehicleData(VehicleConnectionReq vehicleConnectionReq) { public MqttServerModel getVehicleData(VehicleConnectionReq vehicleConnectionReq) {
log.info("车辆连接请求:[{}]",vehicleConnectionReq); log.info("车辆连接请求:[{}]",vehicleConnectionReq);
@ -38,7 +45,35 @@ public class VehicleInformationServiceImpl implements VehicleInformationService{
log.info("车辆无法重复预上线"); log.info("车辆无法重复预上线");
} }
return new MqttServerModel("tcp://121.199.172.3:1883","vehicle"); // 获取名为 "ipList" 的列表
List<String> 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);
}
} }
} }

View File

@ -65,7 +65,7 @@ public class GenerateInstance implements ApplicationRunner {
// 设置实例密码 // 设置实例密码
.setPassword("EightGroup123.") .setPassword("EightGroup123.")
// 设置创建实例的数量 // 设置创建实例的数量
.setAmount(1); .setAmount(2);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try { try {
@ -107,13 +107,12 @@ public class GenerateInstance implements ApplicationRunner {
Thread.sleep(30000); Thread.sleep(30000);
List<ExampleInformation> exampleInformations = QueryInstance.queryInstance(InstanceIds); List<ExampleInformation> exampleInformations = QueryInstance.queryInstance(InstanceIds);
log.info("加载成功"); log.info("加载成功");
ArrayList<String> list = new ArrayList<>();
for (ExampleInformation exampleInformation : exampleInformations) { for (ExampleInformation exampleInformation : exampleInformations) {
redisService.setCacheObject("InstanceIdKey:"+exampleInformation.getInstanceId(),exampleInformation); redisService.setCacheObject("InstanceIdKey:"+exampleInformation.getInstanceId(),exampleInformation);
list.add(exampleInformation.getIpAddress());
} }
// for (ExampleInformation exampleInformation : exampleInformations) { redisService.setCacheList("ipList",list);
// ExampleInformation cacheObject = redisService.getCacheObject(exampleInformation.getInstanceId());
// log.info(cacheObject);
// }
log.info("实例信息:{}",exampleInformations); log.info("实例信息:{}",exampleInformations);
} }