From ed3830c1aed2e4a749ca9da266f423895cd76c71 Mon Sep 17 00:00:00 2001 From: ywt <3471515127@qq.com> Date: Wed, 9 Oct 2024 21:47:25 +0800 Subject: [PATCH] =?UTF-8?q?feat():=20=E8=BD=A6=E8=BE=86=E4=B8=8A=E7=BA=BF?= =?UTF-8?q?=E5=B0=86=E8=BD=A6=E8=BE=86=E4=BF=A1=E6=81=AF=E5=8F=91=E9=80=81?= =?UTF-8?q?=E5=88=B0=E6=B6=88=E6=81=AF=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aliyun/service/AliYunEcsService.java | 5 +++++ .../VehicleConnectionController.java | 4 ++-- .../impl/VehicleConnectionServiceImpl.java | 20 +++++++++++-------- .../src/main/resources/bootstrap.yml | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-) 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")); } } /** diff --git a/cloud-modules/cloud-modules-vehicle-gateway/src/main/resources/bootstrap.yml b/cloud-modules/cloud-modules-vehicle-gateway/src/main/resources/bootstrap.yml index 96bbaaa..45da106 100644 --- a/cloud-modules/cloud-modules-vehicle-gateway/src/main/resources/bootstrap.yml +++ b/cloud-modules/cloud-modules-vehicle-gateway/src/main/resources/bootstrap.yml @@ -7,7 +7,7 @@ nacos: addr: 47.101.49.53:8848 user-name: nacos password: nacos - namespace: warn + namespace: seven # SPRING_AMQP_DESERIALIZATION_TRUST_ALL=true spring.amqp.deserialization.trust.all # Spring spring: