Compare commits
3 Commits
daf43b843b
...
26e1591f43
Author | SHA1 | Date |
---|---|---|
|
26e1591f43 | |
|
e990dd304f | |
|
046b924c96 |
|
@ -255,4 +255,14 @@ public class RedisService {
|
|||
public Collection<String> keys (final String pattern) {
|
||||
return redisTemplate.keys(pattern);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 获取使用的对象的次数
|
||||
// * @param key Redis键
|
||||
// * @param hKey Hash键
|
||||
// */
|
||||
// public void getNumber (final String key, final String hKey){
|
||||
// Object o = redisTemplate.opsForHash().get(key, hKey);
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
|
@ -22,6 +22,8 @@
|
|||
<module>cloud-common-rabbit</module>
|
||||
<module>cloud-common-saas</module>
|
||||
<module>cloud-common-wechat</module>
|
||||
<module>cloud-common-cache</module>
|
||||
<module>cloud-common-kafka</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>cloud-common</artifactId>
|
||||
|
|
|
@ -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<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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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<ExampleInformation> exampleInformations = QueryInstance.queryInstance(InstanceIds);
|
||||
log.info("加载成功");
|
||||
ArrayList<String> 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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue