Compare commits

...

3 Commits

10 changed files with 111 additions and 12 deletions

View File

@ -91,6 +91,13 @@
<artifactId>cloud-common-rabbit</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>

View File

@ -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<String> 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<String> list = generateInstance();
ArrayList<String> ipList = new ArrayList<>();
log.info("创建实例成功");
log.info("正在加载实例");
Thread.sleep(30000);
List<InstanceInfo> 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));
}
}

View File

@ -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;
/**
* <b>description</b> :
@ -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<String> keys = jedis.keys(namespace + "*");
// 如果存在键,则删除它们
if (keys.size() > 0) {
jedis.del(keys.toArray(new String[0]));
}
// 关闭连接
jedis.close();
}
}

View File

@ -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;

View File

@ -29,7 +29,9 @@ import java.util.List;
@Log4j2
public class SelectInstance {
public static List<InstanceInfo> selectInstance(List<String> instanceIds) throws Exception {
// 创建ECS客户端对象用于后续调用ECS相关API
Client client = CreateClient.createClient();
ArrayList<InstanceInfo> instanceInfos = new ArrayList<>();// 实例基础信息
@ -56,7 +58,6 @@ public class SelectInstance {
list.add(instanceInfo);
}
System.out.println("实例信息:"+list);
log.info("实例信息:",list);
return list;
}

View File

@ -17,10 +17,11 @@ public class CarInstanceController {
@PostMapping("/receiveMsg")
public Result receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){
public Result<MqttServerModel> receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){
log.info("=======>"+vehicleConnectionReq);
Result<MqttServerModel> connect = vehicleConnectService.getConnect(vehicleConnectionReq);
return Result.success(connect);
MqttServerModel data = connect.getData();
return Result.success(data);
}
}

View File

@ -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> {
VehicleConnectionReq selectByVehicleVin(String vin);
Integer insertVehicleConnection(VehicleConnectionReq vehicleConnectionReq);
VehicleConnectionReq selectByVehicleVin(@Param("vin") String vin);
}

View File

@ -7,6 +7,6 @@ import com.muyu.vehicle.domain.req.VehicleConnectionReq;
public interface VehicleConnectService extends IService<VehicleConnectionReq> {
Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq);
Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq);
}

View File

@ -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<VehicleConnectMapper, VehicleConnectionReq> implements VehicleConnectService {
@Autowired
private VehicleConnectMapper vehicleConnectMapper;
@Autowired private RedisService redisService;
@Autowired
private RabbitMQProducerUtil rabbitMQProducerUtil;
@Override
public Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq) {
log.info("车辆连接信息:{}", vehicleConnectionReq);
@ -24,13 +35,43 @@ public class VehicleConnectServiceImpl extends ServiceImpl<VehicleConnectMapper,
vehicleConnectionReq.setPassword(vehicleConnectionReq.getVehicleVin() + vehicleConnectionReq.getTimestamp()
+ vehicleConnectionReq.getNonce());
VehicleConnectionReq connection = vehicleConnectMapper.selectByVehicleVin(vehicleConnectionReq.getVehicleVin());
if (connection==null){
vehicleConnectMapper.insert(vehicleConnectionReq);
Integer i = vehicleConnectMapper.insertVehicleConnection(vehicleConnectionReq);
if (i > 0) {
log.info("车辆预上线成功");
}else {
log.info("车辆已预上线成功,禁止重复");
log.info("车辆预上线失败");
}
rabbitMQProducerUtil.basicSendMessage("SendVin",vehicleConnectionReq.getVehicleVin());
//从redis获取信息
// 获取名为 "ipList" 的列表
List<String> 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;
}
}

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.muyu.vehicle.mapper.VehicleConnectMapper">
<insert id="insertVehicleConnection">
INSERT INTO `four`.`vehicle_connection` ( `vehicle_vin`, `timestamp`, `username`, `nonce`, `password`) VALUES
(#{vehicleVin},#{timestamp},#{username},#{nonce},#{password})
</insert>
<select id="selectByVehicleVin" resultType="com.muyu.vehicle.domain.req.VehicleConnectionReq">
select * from vehicle_connection where vehicle_vin=#{vin}
</select>
</mapper>