diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/cloud/vehicle/gateway/aliyun/service/AliYunEcsService.java b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/cloud/vehicle/gateway/aliyun/service/AliYunEcsService.java index d1d33be..36abf32 100644 --- a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/cloud/vehicle/gateway/aliyun/service/AliYunEcsService.java +++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/cloud/vehicle/gateway/aliyun/service/AliYunEcsService.java @@ -46,6 +46,7 @@ public class AliYunEcsService { public List generateInstance(Integer amount) { redisService.deleteObject("instanceIds"); redisService.deleteObject("instanceList"); + redisService.deleteObject("count"); // 检查生成实例的数量是否有效 if (amount == null || amount <= 0) { throw new ServiceException("生成数量不能小于1"); @@ -120,6 +121,7 @@ public class AliYunEcsService { // 创建运行时选项对象,用于配置请求的额外参数 RuntimeOptions runtimeOptions = new RuntimeOptions(); List aliInstances = new ArrayList<>(); + List stringArrayList = new ArrayList<>(); try { // 发送请求并获取响应对象 DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(request, runtimeOptions); @@ -135,12 +137,15 @@ public class AliYunEcsService { // ip地址 String ipAddress = bodyInstance.getPublicIpAddress().getIpAddress().get(0); log.info("实例ip为:{}", ipAddress); + stringArrayList.add(ipAddress); // 实例状态 String status = bodyInstance.getStatus(); log.info("实例状态为:{}", status); AliInstance aliInstance = new AliInstance(instanceId, ipAddress, status); aliInstances.add(aliInstance); } + log.info("======================ipList:{}",stringArrayList); + redisService.setCacheList("ipList",stringArrayList); log.info("查询成功"); } catch (Exception e) { log.error("查询服务器实例错误:[{}]", e.getMessage(), e); diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/cloud/vehicle/gateway/controller/VehicleConnectionController.java b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/cloud/vehicle/gateway/controller/VehicleConnectionController.java index c014662..f3b1b2a 100644 --- a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/cloud/vehicle/gateway/controller/VehicleConnectionController.java +++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/cloud/vehicle/gateway/controller/VehicleConnectionController.java @@ -35,7 +35,7 @@ public class VehicleConnectionController { @PostMapping("/receiveMsg/connect") public Result receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){ log.info(">"+vehicleConnectionReq); - return vehicleConnectionService.getConnect(vehicleConnectionReq); - + vehicleConnectionService.getConnect(vehicleConnectionReq); + return Result.success(); } } diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/cloud/vehicle/gateway/service/impl/VehicleConnectionServiceImpl.java b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/cloud/vehicle/gateway/service/impl/VehicleConnectionServiceImpl.java index 18679a6..67aa6d2 100644 --- a/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/cloud/vehicle/gateway/service/impl/VehicleConnectionServiceImpl.java +++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/java/com/muyu/cloud/vehicle/gateway/service/impl/VehicleConnectionServiceImpl.java @@ -7,6 +7,7 @@ import com.muyu.cloud.vehicle.gateway.domain.req.VehicleConnectionReq; import com.muyu.cloud.vehicle.gateway.mapper.VehicleConnectionMapper; import com.muyu.cloud.vehicle.gateway.service.VehicleConnectionService; import com.muyu.common.core.domain.Result; +import com.muyu.common.redis.service.RedisService; import lombok.extern.log4j.Log4j2; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; @@ -26,6 +27,8 @@ public class VehicleConnectionServiceImpl implements VehicleConnectionService { @Autowired private StringRedisTemplate redisTemplate; + @Autowired + private RedisService redisService; /** * 获取连接信息 @@ -68,22 +71,23 @@ public class VehicleConnectionServiceImpl implements VehicleConnectionService { redisTemplate.opsForValue().set("count",String.valueOf(count+1)); } //根据游标count获取服务IP - String ip = redisTemplate.opsForList().index("ipList", count); + Object ipList = redisService.redisTemplate.opsForList().index("ipList", count); //关联车辆和服务 - this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(),ip)); + this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(),ipList.toString())); //响应信息 - log.info("车辆:{}",vehicleConnectionReq.getVehicleVin()+"绑定成功:{}",ip); - return Result.success(new MqttServerModel("tcp://"+ip+":1883","vehicle")); + log.info("车辆:{}",vehicleConnectionReq.getVehicleVin()+"绑定成功:{}",ipList); + return Result.success(new MqttServerModel("tcp://"+ipList+":1883","vehicle")); }else { redisTemplate.opsForValue().set("count",String.valueOf(0)); //根据游标count获取服务器Ip - String ip = redisTemplate.opsForList().index("ipList", 0); +// String ip = redisTemplate.opsForList().index("ipList", 0); + Object ipList = redisService.redisTemplate.opsForList().index("ipList", 0); //关联车辆和服务 - this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(),ip)); + this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(),ipList.toString())); //响应信息 - log.info("车辆:{}",vehicleConnectionReq.getVehicleVin(),"与:{}绑定成功",ip); - return Result.success(new MqttServerModel("tcp://"+ip+":1883","vehicle")); + log.info("车辆:{}",vehicleConnectionReq.getVehicleVin(),"与:{}绑定成功",ipList); + return Result.success(new MqttServerModel("tcp://"+ipList+":1883","vehicle")); } } /**