From 82bbab11db34349e6f482b748deb1eb5be678b6f Mon Sep 17 00:00:00 2001 From: Yueng <14617246+YuengMeYuuer@user.noreply.gitee.com> Date: Wed, 9 Oct 2024 14:02:11 +0800 Subject: [PATCH] =?UTF-8?q?find()=E6=9B=B4=E6=96=B0=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/VehicleInformationServiceImpl.java | 79 ------------ .../muyu/car/instance/GenerateInstance.java | 120 ------------------ 2 files changed, 199 deletions(-) delete mode 100644 cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/impl/VehicleInformationServiceImpl.java delete mode 100644 cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/instance/GenerateInstance.java 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 deleted file mode 100644 index 2719b75..0000000 --- a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/impl/VehicleInformationServiceImpl.java +++ /dev/null @@ -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:蓬叁 - * @Package:com.muyu.car.gateway.service.impl - * @Project:cloud-server-8 - * @name:VehicleInformationServiceImpl - * @Date:2024/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 selectVehicle =vehicleInformationMapper.selectVehicleVin(vehicleConnectionReq.getVehicleVin()); - if(selectVehicle.isEmpty()) { - vehicleInformationMapper.addVehicleConnection(vehicleConnectionReq); - log.info("车辆预上线成功"); - }else { - log.info("车辆无法重复预上线"); - } - - // 获取名为 "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 deleted file mode 100644 index 7ce87c7..0000000 --- a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/instance/GenerateInstance.java +++ /dev/null @@ -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:蓬叁 - * @Package:com.muyu.car.domain - * @Project:cloud-server-8 - * @name:GenerateInstance - * @Date:2024/9/29 上午10:42 - */ -@Component -@Log4j2 -@Tag(name = "启动时创建实例") -public class GenerateInstance implements ApplicationRunner { - - @Autowired private RedisService redisService; - - /** - * 启动自动创建实例 - * @throws Exception - */ - public static List 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 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 InstanceIds = generateInstance(); - log.info("创建实例成功"); - log.info("正在加载实例"); - 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()); - } - redisService.setCacheList("ipList",list); - log.info("实例信息:{}",exampleInformations); - } - -} -