feat(): 车辆上线将车辆信息发送到消息队列

dev.gateway.aliyun
ywt 2024-10-09 21:47:25 +08:00
parent edeeb878c5
commit ed3830c1ae
4 changed files with 20 additions and 11 deletions

View File

@ -46,6 +46,7 @@ public class AliYunEcsService {
public List<String> generateInstance(Integer amount) { public List<String> generateInstance(Integer amount) {
redisService.deleteObject("instanceIds"); redisService.deleteObject("instanceIds");
redisService.deleteObject("instanceList"); redisService.deleteObject("instanceList");
redisService.deleteObject("count");
// 检查生成实例的数量是否有效 // 检查生成实例的数量是否有效
if (amount == null || amount <= 0) { if (amount == null || amount <= 0) {
throw new ServiceException("生成数量不能小于1"); throw new ServiceException("生成数量不能小于1");
@ -120,6 +121,7 @@ public class AliYunEcsService {
// 创建运行时选项对象,用于配置请求的额外参数 // 创建运行时选项对象,用于配置请求的额外参数
RuntimeOptions runtimeOptions = new RuntimeOptions(); RuntimeOptions runtimeOptions = new RuntimeOptions();
List<AliInstance> aliInstances = new ArrayList<>(); List<AliInstance> aliInstances = new ArrayList<>();
List<String> stringArrayList = new ArrayList<>();
try { try {
// 发送请求并获取响应对象 // 发送请求并获取响应对象
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(request, runtimeOptions); DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(request, runtimeOptions);
@ -135,12 +137,15 @@ public class AliYunEcsService {
// ip地址 // ip地址
String ipAddress = bodyInstance.getPublicIpAddress().getIpAddress().get(0); String ipAddress = bodyInstance.getPublicIpAddress().getIpAddress().get(0);
log.info("实例ip为{}", ipAddress); log.info("实例ip为{}", ipAddress);
stringArrayList.add(ipAddress);
// 实例状态 // 实例状态
String status = bodyInstance.getStatus(); String status = bodyInstance.getStatus();
log.info("实例状态为:{}", status); log.info("实例状态为:{}", status);
AliInstance aliInstance = new AliInstance(instanceId, ipAddress, status); AliInstance aliInstance = new AliInstance(instanceId, ipAddress, status);
aliInstances.add(aliInstance); aliInstances.add(aliInstance);
} }
log.info("======================ipList{}",stringArrayList);
redisService.setCacheList("ipList",stringArrayList);
log.info("查询成功"); log.info("查询成功");
} catch (Exception e) { } catch (Exception e) {
log.error("查询服务器实例错误:[{}]", e.getMessage(), e); log.error("查询服务器实例错误:[{}]", e.getMessage(), e);

View File

@ -35,7 +35,7 @@ public class VehicleConnectionController {
@PostMapping("/receiveMsg/connect") @PostMapping("/receiveMsg/connect")
public Result<MqttServerModel> receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){ public Result<MqttServerModel> receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){
log.info(">"+vehicleConnectionReq); log.info(">"+vehicleConnectionReq);
return vehicleConnectionService.getConnect(vehicleConnectionReq); vehicleConnectionService.getConnect(vehicleConnectionReq);
return Result.success();
} }
} }

View File

@ -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.mapper.VehicleConnectionMapper;
import com.muyu.cloud.vehicle.gateway.service.VehicleConnectionService; import com.muyu.cloud.vehicle.gateway.service.VehicleConnectionService;
import com.muyu.common.core.domain.Result; import com.muyu.common.core.domain.Result;
import com.muyu.common.redis.service.RedisService;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -26,6 +27,8 @@ public class VehicleConnectionServiceImpl implements VehicleConnectionService {
@Autowired @Autowired
private StringRedisTemplate redisTemplate; private StringRedisTemplate redisTemplate;
@Autowired
private RedisService redisService;
/** /**
* *
@ -68,22 +71,23 @@ public class VehicleConnectionServiceImpl implements VehicleConnectionService {
redisTemplate.opsForValue().set("count",String.valueOf(count+1)); redisTemplate.opsForValue().set("count",String.valueOf(count+1));
} }
//根据游标count获取服务IP //根据游标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); log.info("车辆:{}",vehicleConnectionReq.getVehicleVin()+"绑定成功:{}",ipList);
return Result.success(new MqttServerModel("tcp://"+ip+":1883","vehicle")); return Result.success(new MqttServerModel("tcp://"+ipList+":1883","vehicle"));
}else { }else {
redisTemplate.opsForValue().set("count",String.valueOf(0)); redisTemplate.opsForValue().set("count",String.valueOf(0));
//根据游标count获取服务器Ip //根据游标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); log.info("车辆:{}",vehicleConnectionReq.getVehicleVin(),"与:{}绑定成功",ipList);
return Result.success(new MqttServerModel("tcp://"+ip+":1883","vehicle")); return Result.success(new MqttServerModel("tcp://"+ipList+":1883","vehicle"));
} }
} }
/** /**

View File

@ -7,7 +7,7 @@ nacos:
addr: 47.101.49.53:8848 addr: 47.101.49.53:8848
user-name: nacos user-name: nacos
password: nacos password: nacos
namespace: warn namespace: seven
# SPRING_AMQP_DESERIALIZATION_TRUST_ALL=true spring.amqp.deserialization.trust.all # SPRING_AMQP_DESERIALIZATION_TRUST_ALL=true spring.amqp.deserialization.trust.all
# Spring # Spring
spring: spring: