代码重构三次
parent
51e7c2c0c0
commit
48916dce82
|
@ -51,6 +51,9 @@ 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("ipList");
|
||||||
|
redisService.deleteObject("count");
|
||||||
|
redisService.deleteObject("vinIp");
|
||||||
// 检查生成实例的数量是否有效
|
// 检查生成实例的数量是否有效
|
||||||
if (amount == null || amount <= 0) {
|
if (amount == null || amount <= 0) {
|
||||||
throw new ServiceException("生成数量不能小于1");
|
throw new ServiceException("生成数量不能小于1");
|
||||||
|
@ -125,6 +128,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);
|
||||||
|
@ -140,12 +144,16 @@ 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);
|
||||||
|
|
|
@ -36,7 +36,7 @@ public class CarOneClickOperationController {
|
||||||
@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);
|
||||||
MqttServerModel mqttServerModel =carOneClickOperationService.getConnect(vehicleConnectionReq);
|
return carOneClickOperationService.getConnect(vehicleConnectionReq);
|
||||||
return Result.success(mqttServerModel);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,9 +19,9 @@ public class VinIp {
|
||||||
/**
|
/**
|
||||||
* 车辆的vin
|
* 车辆的vin
|
||||||
*/
|
*/
|
||||||
String vehicleVin;
|
String vin;
|
||||||
/**
|
/**
|
||||||
* 车辆的ip
|
* 车辆的ip
|
||||||
*/
|
*/
|
||||||
String ipAddress;
|
String ip;
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,4 +26,6 @@ public class MqttServerModel {
|
||||||
* MQTT订阅主题
|
* MQTT订阅主题
|
||||||
*/
|
*/
|
||||||
private String topic;
|
private String topic;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,4 +18,7 @@ public interface CarOneClickOperationMapper {
|
||||||
void addConnect(VehicleConnection vehicleConnection);
|
void addConnect(VehicleConnection vehicleConnection);
|
||||||
|
|
||||||
List<String> selectByVehicleVin(String vehicleVin);
|
List<String> selectByVehicleVin(String vehicleVin);
|
||||||
|
|
||||||
|
List<String> selectVin();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.muyu.cargateway.service;
|
||||||
|
|
||||||
import com.muyu.cargateway.domain.model.MqttServerModel;
|
import com.muyu.cargateway.domain.model.MqttServerModel;
|
||||||
import com.muyu.cargateway.domain.req.VehicleConnectionReq;
|
import com.muyu.cargateway.domain.req.VehicleConnectionReq;
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ Tool:IntelliJ IDEA
|
* @ Tool:IntelliJ IDEA
|
||||||
|
@ -18,5 +19,5 @@ public interface CarOneClickOperationService {
|
||||||
* @param vehicleConnectionReq 车辆连接请求参数
|
* @param vehicleConnectionReq 车辆连接请求参数
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
MqttServerModel getConnect(VehicleConnectionReq vehicleConnectionReq);
|
Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,15 @@ import com.muyu.cargateway.domain.model.MqttServerModel;
|
||||||
import com.muyu.cargateway.domain.req.VehicleConnectionReq;
|
import com.muyu.cargateway.domain.req.VehicleConnectionReq;
|
||||||
import com.muyu.cargateway.mapper.CarOneClickOperationMapper;
|
import com.muyu.cargateway.mapper.CarOneClickOperationMapper;
|
||||||
import com.muyu.cargateway.service.CarOneClickOperationService;
|
import com.muyu.cargateway.service.CarOneClickOperationService;
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
import com.muyu.common.redis.service.RedisService;
|
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;
|
||||||
|
import org.springframework.data.redis.core.HashOperations;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ Tool:IntelliJ IDEA
|
* @ Tool:IntelliJ IDEA
|
||||||
* @ Author:CHX
|
* @ Author:CHX
|
||||||
|
@ -28,11 +29,12 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CarOneClickOperationMapper carOneClickOperationMapper;
|
private CarOneClickOperationMapper carOneClickOperationMapper;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RabbitTemplate rabbitTemplate;
|
private RabbitTemplate rabbitTemplate;
|
||||||
@Autowired
|
@Autowired
|
||||||
private RedisService redisService;
|
private RedisService redisService;
|
||||||
|
@Autowired
|
||||||
|
private StringRedisTemplate redisTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取连接信息
|
* 获取连接信息
|
||||||
|
@ -40,7 +42,7 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public MqttServerModel getConnect(VehicleConnectionReq vehicleConnectionReq) {
|
public Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq) {
|
||||||
log.info("车辆连接请求:{}",vehicleConnectionReq.toString());
|
log.info("车辆连接请求:{}",vehicleConnectionReq.toString());
|
||||||
|
|
||||||
// 使用交换机发送消息
|
// 使用交换机发送消息
|
||||||
|
@ -55,24 +57,54 @@ public class CarOneClickOperationServiceImpl implements CarOneClickOperationServ
|
||||||
vehicleConnection.setUsername(vehicleConnectionReq.getUsername());
|
vehicleConnection.setUsername(vehicleConnectionReq.getUsername());
|
||||||
//密码(vin+时间戳+随机数)
|
//密码(vin+时间戳+随机数)
|
||||||
vehicleConnection.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());
|
vehicleConnection.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());
|
||||||
//查询车辆vin集合
|
//添加连接信息
|
||||||
List<String> vehicleConnections =carOneClickOperationMapper.selectByVehicleVin(vehicleConnectionReq.getVehicleVin());
|
|
||||||
if(vehicleConnections.isEmpty()){
|
|
||||||
//添加
|
|
||||||
carOneClickOperationMapper.addConnect(vehicleConnection);
|
carOneClickOperationMapper.addConnect(vehicleConnection);
|
||||||
|
//先判断vin码
|
||||||
|
HashOperations<String, String, String> hashOps = redisTemplate.opsForHash();
|
||||||
|
String vinIp = hashOps.get("vinIp", vehicleConnectionReq.getVehicleVin());
|
||||||
|
if(vinIp!=null){
|
||||||
|
log.info("车辆绑定ip失败,已经存在");
|
||||||
|
throw new RuntimeException("车辆绑定ip失败,已经存在");
|
||||||
}
|
}
|
||||||
log.info("该车辆已存在,不能重复预上线");
|
|
||||||
//TODO 返回连接信息 做轮询操作
|
|
||||||
|
|
||||||
|
//判断redis有没有count键
|
||||||
|
if(redisTemplate.hasKey("count")){
|
||||||
|
//取出count
|
||||||
|
Integer count = Integer.valueOf(redisTemplate.opsForValue().get("count"));
|
||||||
|
if(count == 1){
|
||||||
|
redisTemplate.opsForValue().set("count",String.valueOf(0));
|
||||||
|
}else {
|
||||||
|
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);
|
||||||
|
|
||||||
return new MqttServerModel("tcp://"+"106.15.136.7"+":1883","vehicle");
|
log.info("=========================ipList:"+ipList);
|
||||||
|
//关联车辆和服务
|
||||||
|
this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(),ipList.toString()));
|
||||||
|
//响应信息
|
||||||
|
log.info("车辆:{}",vehicleConnectionReq.getVehicleVin()+"绑定成功:{}",ipList);
|
||||||
|
return Result.success(new MqttServerModel("tcp://"+ipList+":1883","vehicle"));
|
||||||
|
|
||||||
|
}else {
|
||||||
|
redisTemplate.opsForValue().set("count",String.valueOf(0));
|
||||||
|
//根据游标count获取服务器Ip
|
||||||
|
Object ipList = redisService.redisTemplate.opsForList().index("ipList", 0);
|
||||||
|
//关联车辆和服务
|
||||||
|
this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(),ipList.toString()));
|
||||||
|
//响应信息
|
||||||
|
log.info("车辆:{}",vehicleConnectionReq.getVehicleVin(),"与:{}绑定成功",ipList);
|
||||||
|
return Result.success(new MqttServerModel("tcp://"+ipList+":1883","vehicle"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 添加车辆绑定IP地址存入redis中
|
* 添加车辆绑定IP地址存入redis中
|
||||||
*/
|
*/
|
||||||
public void addIpAddress(VinIp vinIp){
|
public void addIpAddress(VinIp vinIp) {
|
||||||
redisService.setCacheObject("vehicle_ip_address:"+vinIp.getVehicleVin(),vinIp.getIpAddress());
|
if (vinIp == null || vinIp.getVin() == null || vinIp.getVin().isEmpty() || vinIp.getIp() == null || vinIp.getIp().isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("vin 或 ip 不能为空或无效");
|
||||||
|
}
|
||||||
|
redisTemplate.opsForHash().put("vinIp", vinIp.getVin(), vinIp.getIp());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,4 +82,4 @@ aliyun:
|
||||||
instance-type: ecs.t6-c1m1.large
|
instance-type: ecs.t6-c1m1.large
|
||||||
security-group-id: sg-uf642d5u4ja5gsiitx8y
|
security-group-id: sg-uf642d5u4ja5gsiitx8y
|
||||||
switch-id: vsw-uf66lifrkhxqc94xi06v3
|
switch-id: vsw-uf66lifrkhxqc94xi06v3
|
||||||
amount: 1
|
amount: 2
|
||||||
|
|
|
@ -14,6 +14,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
<select id="selectByVehicleVin" resultType="java.lang.String">
|
<select id="selectByVehicleVin" resultType="java.lang.String">
|
||||||
select vehicle_vin from car_one_click_operation where vehicle_vin = #{vehicleVin}
|
select vehicle_vin from car_one_click_operation where vehicle_vin = #{vehicleVin}
|
||||||
</select>
|
</select>
|
||||||
|
<select id="selectVin" resultType="java.lang.String">
|
||||||
|
select vehicle_vin from car_one_click_operation where vehicle_vin=#{vehicleVin}
|
||||||
|
</select>
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|
Loading…
Reference in New Issue