Merge branch 'dev.gateway.aliyun' into dev
commit
d472b8d326
|
@ -67,10 +67,10 @@
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- MuYu Common Log -->
|
<!-- MuYu Common Log -->
|
||||||
<dependency>
|
<!-- <dependency>-->
|
||||||
<groupId>com.muyu</groupId>
|
<!-- <groupId>com.muyu</groupId>-->
|
||||||
<artifactId>cloud-common-log</artifactId>
|
<!-- <artifactId>cloud-common-log</artifactId>-->
|
||||||
</dependency>
|
<!-- </dependency>-->
|
||||||
|
|
||||||
<!-- 接口模块 -->
|
<!-- 接口模块 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -28,8 +28,8 @@ public class Sample implements ApplicationRunner{
|
||||||
@Autowired
|
@Autowired
|
||||||
private AliProperties aliProperties;
|
private AliProperties aliProperties;
|
||||||
|
|
||||||
// @Autowired
|
@Autowired
|
||||||
// private RedisService redisService;
|
private RedisService redisService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(ApplicationArguments args) throws Exception {
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
|
@ -42,7 +42,7 @@ public class Sample implements ApplicationRunner{
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
log.info("创建实例成功");
|
log.info("创建实例成功");
|
||||||
// redisService.setCacheList("instanceIds",list);
|
redisService.setCacheList("instanceIds",list);
|
||||||
try{
|
try{
|
||||||
Thread.sleep(6000);
|
Thread.sleep(6000);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
@ -51,7 +51,7 @@ public class Sample implements ApplicationRunner{
|
||||||
List<AliInstance> aliInstances = aliYunEcsService.selectInstance(list);
|
List<AliInstance> aliInstances = aliYunEcsService.selectInstance(list);
|
||||||
log.info("查询实例信息成功:{}",aliInstances);
|
log.info("查询实例信息成功:{}",aliInstances);
|
||||||
//将查询到的实例信息列表存储到redis中
|
//将查询到的实例信息列表存储到redis中
|
||||||
// redisService.setCacheList("instanceList",aliInstances);
|
redisService.setCacheList("instanceList",aliInstances);
|
||||||
log.info("redis存储成功:{}",aliInstances);
|
log.info("redis存储成功:{}",aliInstances);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,8 +34,8 @@ public class AliYunEcsService {
|
||||||
/**
|
/**
|
||||||
* redis缓存
|
* redis缓存
|
||||||
*/
|
*/
|
||||||
// @Autowired
|
@Autowired
|
||||||
// private RedisService redisService;
|
private RedisService redisService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成实例
|
* 生成实例
|
||||||
|
@ -44,8 +44,8 @@ public class AliYunEcsService {
|
||||||
* @return 实例id集合
|
* @return 实例id集合
|
||||||
*/
|
*/
|
||||||
public List<String> generateInstance(Integer amount) {
|
public List<String> generateInstance(Integer amount) {
|
||||||
// redisService.deleteObject("instanceIds");
|
redisService.deleteObject("instanceIds");
|
||||||
// redisService.deleteObject("instanceList");
|
redisService.deleteObject("instanceList");
|
||||||
// 检查生成实例的数量是否有效
|
// 检查生成实例的数量是否有效
|
||||||
if (amount == null || amount <= 0) {
|
if (amount == null || amount <= 0) {
|
||||||
throw new ServiceException("生成数量不能小于1");
|
throw new ServiceException("生成数量不能小于1");
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
package com.muyu.cloud.vehicle.gateway.controller;
|
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.domain.req.VehicleConnectionReq;
|
||||||
import com.muyu.cloud.vehicle.gateway.service.VehicleConnectionService;
|
import com.muyu.cloud.vehicle.gateway.service.VehicleConnectionService;
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -32,9 +33,9 @@ public class VehicleConnectionController {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@PostMapping("/receiveMsg/connect")
|
@PostMapping("/receiveMsg/connect")
|
||||||
public Result receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){
|
public Result<MqttServerModel> receiveMsg(@RequestBody VehicleConnectionReq vehicleConnectionReq){
|
||||||
log.info("=======>"+vehicleConnectionReq);
|
log.info(">"+vehicleConnectionReq);
|
||||||
vehicleConnectionService.getConnect(vehicleConnectionReq);
|
return vehicleConnectionService.getConnect(vehicleConnectionReq);
|
||||||
return Result.success();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.muyu.cloud.vehicle.gateway.domain.model;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ Description:Mqtt服务模型
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class MqttServerModel {
|
||||||
|
/**
|
||||||
|
* Mqtt服务节点
|
||||||
|
*/
|
||||||
|
private String broker;
|
||||||
|
/**
|
||||||
|
* MQTT订阅主题
|
||||||
|
*/
|
||||||
|
private String topic;
|
||||||
|
}
|
|
@ -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状态
|
||||||
|
* true为执行中,false为未执行
|
||||||
|
*/
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.muyu.cloud.vehicle.gateway.domain.properties;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ Description:Mqtt的配置
|
||||||
|
*/
|
||||||
|
@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;
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ Description:调用Ali服务器配置实体类
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@SuperBuilder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class AliServerConfig {
|
||||||
|
/**
|
||||||
|
* 主键
|
||||||
|
*/
|
||||||
|
private Long id;
|
||||||
|
/**
|
||||||
|
* 地域id (实例所属的地域ID)
|
||||||
|
*/
|
||||||
|
private String regionId;
|
||||||
|
/**
|
||||||
|
* 镜像id
|
||||||
|
*/
|
||||||
|
private String imageId;
|
||||||
|
/**
|
||||||
|
* 实例规格 (实例的资源规格)
|
||||||
|
*/
|
||||||
|
private String instanceType;
|
||||||
|
}
|
|
@ -1,12 +1,15 @@
|
||||||
package com.muyu.cloud.vehicle.gateway.service;
|
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.cloud.vehicle.gateway.domain.req.VehicleConnectionReq;
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
|
||||||
public interface VehicleConnectionService {
|
public interface VehicleConnectionService {
|
||||||
/**
|
/**
|
||||||
* 获取连接
|
* 获取连接
|
||||||
* @param vehicleConnectionReq
|
* @param vehicleConnectionReq 车辆连接请求参数
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
void getConnect(VehicleConnectionReq vehicleConnectionReq);
|
Result<MqttServerModel> getConnect(VehicleConnectionReq vehicleConnectionReq);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,17 @@
|
||||||
package com.muyu.cloud.vehicle.gateway.service.impl;
|
package com.muyu.cloud.vehicle.gateway.service.impl;
|
||||||
|
|
||||||
import com.muyu.cloud.vehicle.gateway.domain.VehicleConnection;
|
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.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 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;
|
||||||
|
|
||||||
@Log4j2
|
@Log4j2
|
||||||
|
@ -19,17 +24,75 @@ public class VehicleConnectionServiceImpl implements VehicleConnectionService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private VehicleConnectionMapper vehicleConnectionMapper;
|
private VehicleConnectionMapper vehicleConnectionMapper;
|
||||||
|
|
||||||
@Override
|
@Autowired
|
||||||
public void getConnect(VehicleConnectionReq vehicleConnectionReq) {
|
private StringRedisTemplate redisTemplate;
|
||||||
log.info("车辆连接请求:{}",vehicleConnectionReq.toString());
|
|
||||||
|
/**
|
||||||
|
* 获取连接信息
|
||||||
|
* @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();
|
VehicleConnection vehicleConnection = new VehicleConnection();
|
||||||
|
//车辆vin
|
||||||
vehicleConnection.setVehicleVin(vehicleConnectionReq.getVehicleVin());
|
vehicleConnection.setVehicleVin(vehicleConnectionReq.getVehicleVin());
|
||||||
|
//用户名
|
||||||
vehicleConnection.setUsername(vehicleConnectionReq.getUsername());
|
vehicleConnection.setUsername(vehicleConnectionReq.getUsername());
|
||||||
|
//密码(vin+时间戳+随机数)
|
||||||
vehicleConnection.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());
|
vehicleConnection.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());
|
||||||
|
//添加连接信息
|
||||||
vehicleConnectionMapper.addConnect(vehicleConnection);
|
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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 添加车辆绑定IP地址存入redis中
|
||||||
|
*/
|
||||||
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue