Merge branch 'dev.gateway.aliyun' into dev

dev.fault.change
ywt 2024-10-09 14:06:15 +08:00
commit d472b8d326
10 changed files with 269 additions and 24 deletions

View File

@ -67,10 +67,10 @@
</dependency>
<!-- MuYu Common Log -->
<dependency>
<groupId>com.muyu</groupId>
<artifactId>cloud-common-log</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.muyu</groupId>-->
<!-- <artifactId>cloud-common-log</artifactId>-->
<!-- </dependency>-->
<!-- 接口模块 -->
<dependency>

View File

@ -28,8 +28,8 @@ public class Sample implements ApplicationRunner{
@Autowired
private AliProperties aliProperties;
// @Autowired
// private RedisService redisService;
@Autowired
private RedisService redisService;
@Override
public void run(ApplicationArguments args) throws Exception {
@ -42,7 +42,7 @@ public class Sample implements ApplicationRunner{
throw new RuntimeException(e);
}
log.info("创建实例成功");
// redisService.setCacheList("instanceIds",list);
redisService.setCacheList("instanceIds",list);
try{
Thread.sleep(6000);
} catch (InterruptedException e) {
@ -51,7 +51,7 @@ public class Sample implements ApplicationRunner{
List<AliInstance> aliInstances = aliYunEcsService.selectInstance(list);
log.info("查询实例信息成功:{}",aliInstances);
//将查询到的实例信息列表存储到redis中
// redisService.setCacheList("instanceList",aliInstances);
redisService.setCacheList("instanceList",aliInstances);
log.info("redis存储成功{}",aliInstances);
}
}

View File

@ -34,8 +34,8 @@ public class AliYunEcsService {
/**
* redis
*/
// @Autowired
// private RedisService redisService;
@Autowired
private RedisService redisService;
/**
*
@ -44,8 +44,8 @@ public class AliYunEcsService {
* @return id
*/
public List<String> generateInstance(Integer amount) {
// redisService.deleteObject("instanceIds");
// redisService.deleteObject("instanceList");
redisService.deleteObject("instanceIds");
redisService.deleteObject("instanceList");
// 检查生成实例的数量是否有效
if (amount == null || amount <= 0) {
throw new ServiceException("生成数量不能小于1");

View File

@ -1,9 +1,10 @@
package com.muyu.cloud.vehicle.gateway.controller;
import com.alibaba.nacos.api.model.v2.Result;
import com.muyu.cloud.vehicle.gateway.domain.model.MqttServerModel;
import com.muyu.cloud.vehicle.gateway.domain.req.VehicleConnectionReq;
import com.muyu.cloud.vehicle.gateway.service.VehicleConnectionService;
import com.muyu.common.core.domain.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
@ -32,9 +33,9 @@ public class VehicleConnectionController {
* @return
*/
@PostMapping("/receiveMsg/connect")
public Result receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){
log.info("=======>"+vehicleConnectionReq);
vehicleConnectionService.getConnect(vehicleConnectionReq);
return Result.success();
public Result<MqttServerModel> receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){
log.info(">"+vehicleConnectionReq);
return vehicleConnectionService.getConnect(vehicleConnectionReq);
}
}

View File

@ -0,0 +1,24 @@
package com.muyu.cloud.vehicle.gateway.domain.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ DescriptionMqtt
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class MqttServerModel {
/**
* Mqtt
*/
private String broker;
/**
* MQTT
*/
private String topic;
}

View File

@ -0,0 +1,82 @@
package com.muyu.cloud.vehicle.gateway.domain.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @ Description
*/
@Data
@Log4j2
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class TaskModel {
/**
* false
* truefalse
*/
private final AtomicBoolean status =new AtomicBoolean(Boolean.FALSE);
/**
*
*/
private CountDownLatch countDownLatch;
/**
*
*/
private LinkedBlockingDeque<String> carQueue =new LinkedBlockingDeque<>();
/**
*
* true
* false
* @return
*/
private boolean isExecution(){
return !status.get();
}
/**
*
*/
private String taskName;
/**
*
*/
private Integer taskExecutionCount=0;
/**
*
*/
private Long taskStartTime;
/**
*
*/
private AtomicInteger taskSuccessSum=new AtomicInteger();
/**
*
*/
private AtomicInteger taskErrorSum=new AtomicInteger();
/**
*
* @return true
*/
public boolean hashNext(){
return !carQueue.isEmpty();
}
/**
*
* @return VIN
*/
public String next(){
return carQueue.poll();
}
}

View File

@ -0,0 +1,40 @@
package com.muyu.cloud.vehicle.gateway.domain.properties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ DescriptionMqtt
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MqttProperties {
/**
*
*/
private String broker;
/**
*
*/
private String topic;
/**
*
*/
private String userName;
/**
*
*/
private String password;
/**
* id
*/
private String clientId;
/**
*
*/
private int qos = 0;
}

View File

@ -0,0 +1,32 @@
package com.muyu.cloud.vehicle.gateway.domain.resp;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
/**
* @ DescriptionAli
*/
@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class AliServerConfig {
/**
*
*/
private Long id;
/**
* id (ID)
*/
private String regionId;
/**
* id
*/
private String imageId;
/**
* ()
*/
private String instanceType;
}

View File

@ -1,12 +1,15 @@
package com.muyu.cloud.vehicle.gateway.service;
import com.muyu.cloud.vehicle.gateway.domain.model.MqttServerModel;
import com.muyu.cloud.vehicle.gateway.domain.req.VehicleConnectionReq;
import com.muyu.common.core.domain.Result;
public interface VehicleConnectionService {
/**
*
* @param vehicleConnectionReq
* @param vehicleConnectionReq
* @return
*/
void getConnect(VehicleConnectionReq vehicleConnectionReq);
Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq);
}

View File

@ -1,12 +1,17 @@
package com.muyu.cloud.vehicle.gateway.service.impl;
import com.muyu.cloud.vehicle.gateway.domain.VehicleConnection;
import com.muyu.cloud.vehicle.gateway.domain.VinIp;
import com.muyu.cloud.vehicle.gateway.domain.model.MqttServerModel;
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 lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
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;
@Log4j2
@ -19,17 +24,75 @@ public class VehicleConnectionServiceImpl implements VehicleConnectionService {
@Autowired
private VehicleConnectionMapper vehicleConnectionMapper;
@Override
public void getConnect(VehicleConnectionReq vehicleConnectionReq) {
log.info("车辆连接请求:{}",vehicleConnectionReq.toString());
@Autowired
private StringRedisTemplate redisTemplate;
/**
*
* @param vehicleConnectionReq
* @return
*/
@Override
public Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq) {
log.info("车辆连接请求:{}",vehicleConnectionReq.toString());
// 使用交换机发送消息
rabbitTemplate.convertAndSend("exchange_topics_inform","inform.#.email.#",vehicleConnectionReq.getVehicleVin());
log.info("发送消息成功:{}",vehicleConnectionReq.getVehicleVin());
//发送消息
rabbitTemplate.convertAndSend("exchange_topics_inform","",vehicleConnectionReq.getVehicleVin());
VehicleConnection vehicleConnection = new VehicleConnection();
//车辆vin
vehicleConnection.setVehicleVin(vehicleConnectionReq.getVehicleVin());
//用户名
vehicleConnection.setUsername(vehicleConnectionReq.getUsername());
//密码(vin+时间戳+随机数)
vehicleConnection.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());
//添加连接信息
vehicleConnectionMapper.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失败已经存在");
}
//判断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);
//关联车辆和服务
this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(),ip));
//响应信息
log.info("车辆:{}",vehicleConnectionReq.getVehicleVin()+"绑定成功:{}",ip);
return Result.success(new MqttServerModel("tcp://"+ip+":1883","vehicle"));
}else {
redisTemplate.opsForValue().set("count",String.valueOf(0));
//根据游标count获取服务器Ip
String ip = redisTemplate.opsForList().index("ipList", 0);
//关联车辆和服务
this.addIpAddress(new VinIp(vehicleConnectionReq.getVehicleVin(),ip));
//响应信息
log.info("车辆:{}",vehicleConnectionReq.getVehicleVin(),"与:{}绑定成功",ip);
return Result.success(new MqttServerModel("tcp://"+ip+":1883","vehicle"));
}
}
/**
* IPredis
*/
public void addIpAddress(VinIp vinIp) {
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());
}
}