find()更新缓存

dev.yang
Yueng 2024-10-09 14:02:11 +08:00
parent b2a2a2bcb7
commit 82bbab11db
2 changed files with 0 additions and 199 deletions

View File

@ -1,79 +0,0 @@
package com.muyu.car.gateway.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.car.domain.api.req.VehicleConnectionReq;
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
* @Packagecom.muyu.car.gateway.service.impl
* @Projectcloud-server-8
* @nameVehicleInformationServiceImpl
* @Date2024/10/6 2:41
*/
@Log4j2
@Service
public class VehicleInformationServiceImpl implements VehicleInformationService{
@Autowired private VehicleInformationMapper vehicleInformationMapper;
@Autowired private RedisService redisService;
@Override
public MqttServerModel getVehicleData(VehicleConnectionReq vehicleConnectionReq) {
log.info("车辆连接请求:[{}]",vehicleConnectionReq);
vehicleConnectionReq.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());
List<String> selectVehicle =vehicleInformationMapper.selectVehicleVin(vehicleConnectionReq.getVehicleVin());
if(selectVehicle.isEmpty()) {
vehicleInformationMapper.addVehicleConnection(vehicleConnectionReq);
log.info("车辆预上线成功");
}else {
log.info("车辆无法重复预上线");
}
// 获取名为 "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

@ -1,120 +0,0 @@
package com.muyu.car.instance;
import com.aliyun.ecs20140526.models.*;
import com.aliyun.tea.TeaException;
import com.muyu.car.domain.example.ExampleInformation;
import com.muyu.common.redis.service.RedisService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
*
* @Author
* @Packagecom.muyu.car.domain
* @Projectcloud-server-8
* @nameGenerateInstance
* @Date2024/9/29 10:42
*/
@Component
@Log4j2
@Tag(name = "启动时创建实例")
public class GenerateInstance implements ApplicationRunner {
@Autowired private RedisService redisService;
/**
*
* @throws Exception
*/
public static List<String> generateInstance() throws Exception {
// 创建ECS客户端对象用于后续调用ECS相关API
com.aliyun.ecs20140526.Client client = CreateClient.createClient();
com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk()
.setSize("40")
.setCategory("cloud_essd");
com.aliyun.ecs20140526.models.RunInstancesRequest runInstancesRequest = new com.aliyun.ecs20140526.models.RunInstancesRequest()
// 设置地域ID
.setRegionId("cn-hangzhou")
// 设置镜像ID
.setImageId("m-bp1hkxfctk751s62jqhq")
// 设置实例类型
.setInstanceType("ecs.t6-c1m1.large")
// 设置安全组ID
.setSecurityGroupId("sg-bp1a7fk8js5pn3fw9p2m")
// 设置虚拟交换机ID
.setVSwitchId("vsw-bp193np7r01vssqxhh24e")
// 设置实例名称
.setInstanceName("server-mqtt")
// 设置实例付费类型为后付费按量付费
.setInstanceChargeType("PostPaid")
// 设置互联网最大出带宽为1 Mbps
.setInternetMaxBandwidthOut(1)
// 设置系统盘配置
.setSystemDisk(systemDisk)
// 设置主机名
.setHostName("root")
// 设置实例密码
.setPassword("EightGroup123.")
// 设置创建实例的数量
.setAmount(2);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// 复制代码运行请自行打印 API 的返回值
RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runtime);
// 获取body返回值对象
RunInstancesResponseBody body = runInstancesResponse.getBody();
ArrayList<String> list = new ArrayList<>();
// 得到实例ID数组
for (String instance : body.getInstanceIdSets().getInstanceIdSet()) {
list.add(instance);
}
log.info("实例ID:{}",list);
return list;
} catch (TeaException error) {
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
log.info(error.getMessage());
// 诊断地址
log.info(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
log.info(error.getMessage());
// 诊断地址
log.info(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
return null;
}
@Override
public void run(ApplicationArguments args) throws Exception {
List<String> InstanceIds = generateInstance();
log.info("创建实例成功");
log.info("正在加载实例");
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());
}
redisService.setCacheList("ipList",list);
log.info("实例信息:{}",exampleInformations);
}
}