diff --git a/cloud-modules/cloud-modules-vehiclegateway/pom.xml b/cloud-modules/cloud-modules-vehiclegateway/pom.xml index 112b41e..1150791 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/pom.xml +++ b/cloud-modules/cloud-modules-vehiclegateway/pom.xml @@ -91,6 +91,13 @@ cloud-common-rabbit + + + redis.clients + jedis + 5.0.2 + + org.springframework.boot spring-boot-autoconfigure diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/ManageInstance.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/ManageInstance.java index c8457c5..66c4d37 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/ManageInstance.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/ManageInstance.java @@ -14,6 +14,7 @@ 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.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; import java.util.ArrayList; @@ -30,10 +31,12 @@ public class ManageInstance implements ApplicationRunner { @Autowired private RedisService redisService; + + /** * 镜像ID */ - public static final String IMAGE_ID = "m-uf6ffgkry85fwu4znr6s"; + public static final String IMAGE_ID = "m-uf62k8zpy0ga35jnmkwt"; /** * 实例类型 @@ -57,6 +60,8 @@ public class ManageInstance implements ApplicationRunner { public static final String INSTANCE_CHARGE_TY = "PostPaid"; public static List generateInstance() throws Exception { + + // 创建阿里云ECS客户端 // 创建ECS客户端对象,用于后续调用ECS相关API Client client = CreateClient.createClient(); @@ -118,16 +123,18 @@ public class ManageInstance implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { List list = generateInstance(); + ArrayList ipList = new ArrayList<>(); log.info("创建实例成功"); log.info("正在加载实例"); Thread.sleep(30000); List instanceInfos = SelectInstance.selectInstance(list); log.info("实例信息查询成功"); for (InstanceInfo instanceInfo : instanceInfos) { + ipList.add(instanceInfo.getIpAddress()); redisService.setCacheObject("FourInstanceIdKey:"+instanceInfo.getInstanceId(),instanceInfo); } + redisService.setCacheList("FourIpList",ipList); System.out.println("实例信息:"+instanceInfos); - log.info("实例信息:", JSONObject.toJSONString(instanceInfos)); } } diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CloseInstance.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CloseInstance.java index dd6ab92..e98b6e9 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CloseInstance.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CloseInstance.java @@ -8,13 +8,17 @@ import com.aliyun.tea.TeaException; import com.aliyun.teaopenapi.models.Config; import com.aliyun.teautil.Common; import com.aliyun.teautil.models.RuntimeOptions; +import com.muyu.common.redis.service.RedisService; import com.muyu.vehicle.utils.CreateClient; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; +import redis.clients.jedis.Jedis; import java.util.ArrayList; +import java.util.Set; /** * 删除实例信息 @@ -23,6 +27,8 @@ import java.util.ArrayList; @Component @Log4j2 public class CloseInstance implements DisposableBean{ + @Autowired + private RedisService redisService; /** * description : @@ -33,6 +39,7 @@ public class CloseInstance implements DisposableBean{ */ public static void delInstance() throws Exception { + // 创建ECS客户端对象,用于后续调用ECS相关API Client client = CreateClient.createClient(); @@ -95,6 +102,19 @@ public class CloseInstance implements DisposableBean{ public void destroy() throws Exception { log.info("开始删除实例"); delInstance(); + redisService.deleteObject("FourIpList"); + // 连接到Redis服务器 + Jedis jedis = new Jedis("47.116.173.119", 6379); + // 指定要删除的文件夹(命名空间) + String namespace = "FourInstanceIdKey:"; + // 获取所有以namespace为前缀的键 + Set keys = jedis.keys(namespace + "*"); + // 如果存在键,则删除它们 + if (keys.size() > 0) { + jedis.del(keys.toArray(new String[0])); + } + // 关闭连接 + jedis.close(); } } diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CreateExchange.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CreateExchange.java index eb7436b..49804f8 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CreateExchange.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/CreateExchange.java @@ -9,10 +9,15 @@ import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; +/** + * 创建交换机 + */ @Log4j2 @Component public class CreateExchange implements ApplicationRunner { + + @Autowired private ConnectionFactory connectionFactory; diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/SelectInstance.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/SelectInstance.java index e2048d7..b1b33d2 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/SelectInstance.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/config/SelectInstance.java @@ -29,7 +29,9 @@ import java.util.List; @Log4j2 public class SelectInstance { + public static List selectInstance(List instanceIds) throws Exception { + // 创建ECS客户端对象,用于后续调用ECS相关API Client client = CreateClient.createClient(); ArrayList instanceInfos = new ArrayList<>();// 实例基础信息 @@ -56,7 +58,6 @@ public class SelectInstance { list.add(instanceInfo); } System.out.println("实例信息:"+list); - log.info("实例信息:",list); return list; } diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/controller/CarInstanceController.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/controller/CarInstanceController.java index 0ba1477..c7f621b 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/controller/CarInstanceController.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/controller/CarInstanceController.java @@ -17,10 +17,11 @@ public class CarInstanceController { @PostMapping("/receiveMsg") - public Result receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){ + public Result receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){ log.info("=======>"+vehicleConnectionReq); Result connect = vehicleConnectService.getConnect(vehicleConnectionReq); - return Result.success(connect); + MqttServerModel data = connect.getData(); + return Result.success(data); } } diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/mapper/VehicleConnectMapper.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/mapper/VehicleConnectMapper.java index 50af952..f81af09 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/mapper/VehicleConnectMapper.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/mapper/VehicleConnectMapper.java @@ -3,10 +3,12 @@ package com.muyu.vehicle.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.muyu.vehicle.domain.req.VehicleConnectionReq; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; @Mapper public interface VehicleConnectMapper extends BaseMapper { - VehicleConnectionReq selectByVehicleVin(String vin); + Integer insertVehicleConnection(VehicleConnectionReq vehicleConnectionReq); + VehicleConnectionReq selectByVehicleVin(@Param("vin") String vin); } diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/VehicleConnectService.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/VehicleConnectService.java index cf276da..9db7c6b 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/VehicleConnectService.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/VehicleConnectService.java @@ -7,6 +7,6 @@ import com.muyu.vehicle.domain.req.VehicleConnectionReq; public interface VehicleConnectService extends IService { - Result getConnect(VehicleConnectionReq vehicleConnectionReq); + Result getConnect(VehicleConnectionReq vehicleConnectionReq); } diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleConnectServiceImpl.java b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleConnectServiceImpl.java index 340c273..6c28922 100644 --- a/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleConnectServiceImpl.java +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/java/com/muyu/vehicle/service/impl/VehicleConnectServiceImpl.java @@ -3,20 +3,31 @@ package com.muyu.vehicle.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.muyu.common.core.domain.Result; +import com.muyu.common.redis.service.RedisService; +import com.muyu.rabbitmq.consumer.RabbitMQConsumerUtil; +import com.muyu.rabbitmq.producer.RabbitMQProducerUtil; import com.muyu.vehicle.domain.MqttServerModel; import com.muyu.vehicle.domain.req.VehicleConnectionReq; import com.muyu.vehicle.mapper.VehicleConnectMapper; import com.muyu.vehicle.service.VehicleConnectService; import lombok.extern.log4j.Log4j2; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; + @Service @Log4j2 public class VehicleConnectServiceImpl extends ServiceImpl implements VehicleConnectService { @Autowired private VehicleConnectMapper vehicleConnectMapper; + @Autowired private RedisService redisService; + + @Autowired + private RabbitMQProducerUtil rabbitMQProducerUtil; + @Override public Result getConnect(VehicleConnectionReq vehicleConnectionReq) { log.info("车辆连接信息:{}", vehicleConnectionReq); @@ -24,13 +35,43 @@ public class VehicleConnectServiceImpl extends ServiceImpl 0) { log.info("车辆预上线成功"); }else { - log.info("车辆已预上线成功,禁止重复"); + log.info("车辆预上线失败"); + } + rabbitMQProducerUtil.basicSendMessage("SendVin",vehicleConnectionReq.getVehicleVin()); + //从redis获取信息 + // 获取名为 "ipList" 的列表 + List ipList = redisService.getCacheList("FourIpList"); + 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 < 6) { + // 使用次数加一 + count++; + redisService.setCacheMapValue("ipCounts", selectedIp, String.valueOf(count)); + // 更新索引 + index = (index + 1) % ipList.size(); + redisService.setCacheObject("currentIndex", String.valueOf(index)); + MqttServerModel mqttServerModel = new MqttServerModel("tcp://" + selectedIp.substring(1, selectedIp.length() - 1) + ":1883", "vehicle"); + return Result.success(mqttServerModel); + } else { + // 如果使用次数达到 6 次,跳过该 IP 并更新索引 + index = (index + 1) % ipList.size(); + redisService.setCacheObject("currentIndex", String.valueOf(index)); + return getConnect(vehicleConnectionReq); } - return null; } } diff --git a/cloud-modules/cloud-modules-vehiclegateway/src/main/resources/mapper/VehicleConnectMapper.xml b/cloud-modules/cloud-modules-vehiclegateway/src/main/resources/mapper/VehicleConnectMapper.xml new file mode 100644 index 0000000..684912d --- /dev/null +++ b/cloud-modules/cloud-modules-vehiclegateway/src/main/resources/mapper/VehicleConnectMapper.xml @@ -0,0 +1,15 @@ + + + + + + INSERT INTO `four`.`vehicle_connection` ( `vehicle_vin`, `timestamp`, `username`, `nonce`, `password`) VALUES + (#{vehicleVin},#{timestamp},#{username},#{nonce},#{password}) + + + +