Compare commits
4 Commits
5b7de4a971
...
87e6afc257
Author | SHA1 | Date |
---|---|---|
|
87e6afc257 | |
|
41354f4a8f | |
|
239c6001c8 | |
|
83f0060b4e |
|
@ -0,0 +1,22 @@
|
||||||
|
package com.muyu.common.rabbit.constants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:蓬叁
|
||||||
|
* @Package:com.muyu.common.rabbit.contants
|
||||||
|
* @Project:cloud-server-8
|
||||||
|
* @name:VehicleGatewayContants
|
||||||
|
* @Date:2024/10/10 上午9:02
|
||||||
|
*/
|
||||||
|
public class VehicleGatewayConstants {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 车辆网关交换机
|
||||||
|
*/
|
||||||
|
public final static String VEHICLE_GETAWAY_EXCHANGE = "vehicle.getaway";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 车辆网关交换机上线路由键
|
||||||
|
*/
|
||||||
|
public final static String VEHICLE_GETAWAY_ROUTING_KEY = "vehicle.getaway.online";
|
||||||
|
}
|
||||||
|
|
|
@ -64,6 +64,7 @@
|
||||||
<artifactId>spring-cloud-loadbalancer</artifactId>
|
<artifactId>spring-cloud-loadbalancer</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<!--验证码 -->
|
<!--验证码 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>pro.fessional</groupId>
|
<groupId>pro.fessional</groupId>
|
||||||
|
@ -76,6 +77,12 @@
|
||||||
<artifactId>cloud-common-redis</artifactId>
|
<artifactId>cloud-common-redis</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- rabbitMq 消息队列 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.xiaoymin</groupId>
|
<groupId>com.github.xiaoymin</groupId>
|
||||||
<artifactId>knife4j-gateway-spring-boot-starter</artifactId>
|
<artifactId>knife4j-gateway-spring-boot-starter</artifactId>
|
||||||
|
|
|
@ -102,6 +102,10 @@
|
||||||
<groupId>redis.clients</groupId>
|
<groupId>redis.clients</groupId>
|
||||||
<artifactId>jedis</artifactId>
|
<artifactId>jedis</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.muyu</groupId>
|
||||||
|
<artifactId>cloud-common-security</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.muyu.car.domain.api;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:蓬叁
|
||||||
|
* @Package:com.muyu.car.domain.api.req
|
||||||
|
* @Project:cloud-server-8
|
||||||
|
* @name:WebHookConnection
|
||||||
|
* @Date:2024/10/10 下午7:49
|
||||||
|
*/
|
||||||
|
public class WebHookConnection {
|
||||||
|
|
||||||
|
private String protocol;
|
||||||
|
private String timestamp;
|
||||||
|
private String version;
|
||||||
|
private String keepalive;
|
||||||
|
private String cleanSession;
|
||||||
|
private String nodeIp;
|
||||||
|
private String clientId;
|
||||||
|
private String clientIp;
|
||||||
|
private String clientPort;
|
||||||
|
private String MessageId;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.muyu.car.gateway.controller;
|
||||||
|
|
||||||
|
import com.muyu.car.domain.api.WebHookConnection;
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:蓬叁
|
||||||
|
* @Package:com.muyu.gateway.controller
|
||||||
|
* @Project:cloud-server-8
|
||||||
|
* @name:FluxMqCallbackController
|
||||||
|
* @Date:2024/10/10 下午2:44
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/fluxmq")
|
||||||
|
public class FluxMqCallbackController {
|
||||||
|
|
||||||
|
@Autowired private RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
@PostMapping("/send")
|
||||||
|
public Result<String> online(WebHookConnection webHookConnection){
|
||||||
|
rabbitTemplate.convertAndSend("getaway","fluxmq",webHookConnection);
|
||||||
|
return Result.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -29,7 +29,7 @@ public class VehicleInformationController {
|
||||||
@Validated @RequestBody VehicleConnectionReq vehicleConnectionReq
|
@Validated @RequestBody VehicleConnectionReq vehicleConnectionReq
|
||||||
) {
|
) {
|
||||||
|
|
||||||
return Result.success(vehicleInformationService.getVehicleData(vehicleConnectionReq));
|
return vehicleInformationService.getVehicleData(vehicleConnectionReq);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.muyu.car.gateway.mq;
|
package com.muyu.car.gateway.mq;
|
||||||
|
|
||||||
|
import com.muyu.common.rabbit.constants.VehicleGatewayConstants;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.amqp.core.*;
|
import org.springframework.amqp.core.*;
|
||||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||||
|
@ -7,6 +8,7 @@ import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.ApplicationArguments;
|
import org.springframework.boot.ApplicationArguments;
|
||||||
import org.springframework.boot.ApplicationRunner;
|
import org.springframework.boot.ApplicationRunner;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,35 +21,14 @@ import org.springframework.stereotype.Component;
|
||||||
*/
|
*/
|
||||||
@Log4j2
|
@Log4j2
|
||||||
@Component
|
@Component
|
||||||
public class CreateExchange implements ApplicationRunner {
|
public class CreateExchange {
|
||||||
|
|
||||||
@Autowired
|
/**
|
||||||
private ConnectionFactory connectionFactory;
|
* 创建网关路由交换机
|
||||||
|
* @return
|
||||||
@Override
|
*/
|
||||||
public void run(ApplicationArguments args) {
|
@Bean
|
||||||
log.info("=====>开始创建交换机");
|
public DirectExchange initVehicleGatewayExchange(){
|
||||||
|
return new DirectExchange(VehicleGatewayConstants.VEHICLE_GETAWAY_EXCHANGE);
|
||||||
try {
|
|
||||||
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
|
|
||||||
|
|
||||||
// 创建Fanout类型的交换机
|
|
||||||
FanoutExchange exchange = new FanoutExchange("ONLINE_EXCHANGE", true, false);
|
|
||||||
|
|
||||||
rabbitAdmin.declareExchange(exchange);
|
|
||||||
|
|
||||||
// 创建队列
|
|
||||||
Queue queue = new Queue("GO_ONLINE", true, false, false);
|
|
||||||
|
|
||||||
rabbitAdmin.declareQueue(queue);
|
|
||||||
|
|
||||||
// Fanout交换机绑定
|
|
||||||
rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange));
|
|
||||||
|
|
||||||
log.info("=====>交换机创建成功");
|
|
||||||
log.info("=====>队列创建成功并绑定到交换机");
|
|
||||||
}catch (Exception e) {
|
|
||||||
log.info(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
import com.muyu.car.domain.api.req.VehicleConnectionReq;
|
import com.muyu.car.domain.api.req.VehicleConnectionReq;
|
||||||
import com.muyu.car.domain.model.MqttServerModel;
|
import com.muyu.car.domain.model.MqttServerModel;
|
||||||
import com.muyu.car.domain.model.VehicleInformation;
|
import com.muyu.car.domain.model.VehicleInformation;
|
||||||
|
import com.muyu.common.core.domain.Result;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author:蓬叁
|
* @Author:蓬叁
|
||||||
|
@ -13,5 +14,5 @@ import com.muyu.car.domain.model.VehicleInformation;
|
||||||
* @Date:2024/10/6 下午2:40
|
* @Date:2024/10/6 下午2:40
|
||||||
*/
|
*/
|
||||||
public interface VehicleInformationService{
|
public interface VehicleInformationService{
|
||||||
MqttServerModel getVehicleData(VehicleConnectionReq vehicleConnectionReq);
|
Result<MqttServerModel> getVehicleData(VehicleConnectionReq vehicleConnectionReq);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import com.muyu.car.domain.model.MqttServerModel;
|
||||||
import com.muyu.car.domain.model.VehicleInformation;
|
import com.muyu.car.domain.model.VehicleInformation;
|
||||||
import com.muyu.car.gateway.service.VehicleInformationService;
|
import com.muyu.car.gateway.service.VehicleInformationService;
|
||||||
import com.muyu.car.mapper.VehicleInformationMapper;
|
import com.muyu.car.mapper.VehicleInformationMapper;
|
||||||
|
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.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -33,7 +34,7 @@ public class VehicleInformationServiceImpl implements VehicleInformationService{
|
||||||
@Autowired private RedisService redisService;
|
@Autowired private RedisService redisService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MqttServerModel getVehicleData(VehicleConnectionReq vehicleConnectionReq) {
|
public Result<MqttServerModel> getVehicleData(VehicleConnectionReq vehicleConnectionReq) {
|
||||||
log.info("车辆连接请求:[{}]",vehicleConnectionReq);
|
log.info("车辆连接请求:[{}]",vehicleConnectionReq);
|
||||||
vehicleConnectionReq.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());
|
vehicleConnectionReq.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce());
|
||||||
|
|
||||||
|
@ -45,16 +46,16 @@ public class VehicleInformationServiceImpl implements VehicleInformationService{
|
||||||
log.info("车辆无法重复预上线");
|
log.info("车辆无法重复预上线");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取名为 "ipList" 的列表
|
// 获取 IP 的列表
|
||||||
List<String> ipList = redisService.getCacheList("ipList");
|
List<String> ipList = redisService.getCacheList("ipList");
|
||||||
if (ipList.isEmpty()) {
|
if (ipList.isEmpty()) {
|
||||||
return null;
|
return Result.error("IP服务器列表为空");
|
||||||
}
|
}
|
||||||
// 获取当前使用的索引位置
|
// 获取当前使用的索引位置
|
||||||
String indexStr = redisService.getCacheObject("currentIndex");
|
String indexStr = redisService.getCacheObject("currentIndex");
|
||||||
int index = indexStr!= null? Integer.parseInt(indexStr) : 0;
|
int index = indexStr!= null? Integer.parseInt(indexStr) : 0;
|
||||||
String selectedIp = ipList.get(index);
|
String selectedIp = ipList.get(index);
|
||||||
// 获取该 IP 的使用次数
|
// 获取 IP 的使用次数
|
||||||
String countStr = redisService.getCacheMapValue("ipCounts", selectedIp);
|
String countStr = redisService.getCacheMapValue("ipCounts", selectedIp);
|
||||||
log.info("IP:[{}]车辆连接数:[{}]",selectedIp,countStr);
|
log.info("IP:[{}]车辆连接数:[{}]",selectedIp,countStr);
|
||||||
int count = countStr!= null? Integer.parseInt(countStr) : 0;
|
int count = countStr!= null? Integer.parseInt(countStr) : 0;
|
||||||
|
@ -65,7 +66,7 @@ public class VehicleInformationServiceImpl implements VehicleInformationService{
|
||||||
// 更新索引
|
// 更新索引
|
||||||
index = (index + 1) % ipList.size();
|
index = (index + 1) % ipList.size();
|
||||||
redisService.setCacheObject("currentIndex", String.valueOf(index));
|
redisService.setCacheObject("currentIndex", String.valueOf(index));
|
||||||
return new MqttServerModel("tcp://"+selectedIp.substring(1,selectedIp.length()-1)+":1883","vehicle");
|
return Result.success(new MqttServerModel("tcp://"+selectedIp.substring(1,selectedIp.length()-1)+":1883","vehicle"));
|
||||||
} else {
|
} else {
|
||||||
// 如果使用次数达到 12 次,跳过该 IP 并更新索引
|
// 如果使用次数达到 12 次,跳过该 IP 并更新索引
|
||||||
index = (index + 1) % ipList.size();
|
index = (index + 1) % ipList.size();
|
||||||
|
|
|
@ -45,7 +45,7 @@ public class GenerateInstance implements ApplicationRunner {
|
||||||
// 设置地域ID
|
// 设置地域ID
|
||||||
.setRegionId("cn-hangzhou")
|
.setRegionId("cn-hangzhou")
|
||||||
// 设置镜像ID
|
// 设置镜像ID
|
||||||
.setImageId("m-bp1hkxfctk751s62jqhq")
|
.setImageId("m-bp1epdalpjow84ornf09")
|
||||||
// 设置实例类型
|
// 设置实例类型
|
||||||
.setInstanceType("ecs.t6-c1m1.large")
|
.setInstanceType("ecs.t6-c1m1.large")
|
||||||
// 设置安全组ID
|
// 设置安全组ID
|
||||||
|
|
|
@ -0,0 +1,98 @@
|
||||||
|
package com.muyu.car;
|
||||||
|
|
||||||
|
import com.aliyun.ecs20140526.models.RunInstancesResponse;
|
||||||
|
import com.aliyun.ecs20140526.models.RunInstancesResponseBody;
|
||||||
|
import com.aliyun.tea.TeaException;
|
||||||
|
import com.muyu.car.instance.CreateClient;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:蓬叁
|
||||||
|
* @Package:com.muyu.car.gateway
|
||||||
|
* @Project:cloud-server-8
|
||||||
|
* @name:test
|
||||||
|
* @Date:2024/10/10 上午10:30
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
public class test {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
|
generateInstance();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启动自动创建实例
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static List<String> generateInstance() throws Exception {
|
||||||
|
|
||||||
|
// 创建ECS客户端对象,用于后续调用ECS相关API
|
||||||
|
com.aliyun.ecs20140526.Client client = CreateClient.createClient();
|
||||||
|
|
||||||
|
com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk()
|
||||||
|
.setSize("40")
|
||||||
|
.setCategory("cloud_essd");
|
||||||
|
com.aliyun.ecs20140526.models.RunInstancesRequest runInstancesRequest = new com.aliyun.ecs20140526.models.RunInstancesRequest()
|
||||||
|
// 设置地域ID
|
||||||
|
.setRegionId("cn-hangzhou")
|
||||||
|
// 设置镜像ID
|
||||||
|
.setImageId("m-bp1epdalpjow84ornf09")
|
||||||
|
// 设置实例类型
|
||||||
|
.setInstanceType("ecs.t6-c1m1.large")
|
||||||
|
// 设置安全组ID
|
||||||
|
.setSecurityGroupId("sg-bp1a7fk8js5pn3fw9p2m")
|
||||||
|
// 设置虚拟交换机ID
|
||||||
|
.setVSwitchId("vsw-bp193np7r01vssqxhh24e")
|
||||||
|
// 设置实例名称
|
||||||
|
.setInstanceName("server-mqtt")
|
||||||
|
// 设置实例付费类型为后付费按量付费
|
||||||
|
.setInstanceChargeType("PostPaid")
|
||||||
|
// 设置互联网最大出带宽为1 Mbps
|
||||||
|
.setInternetMaxBandwidthOut(1)
|
||||||
|
// 设置系统盘配置
|
||||||
|
.setSystemDisk(systemDisk)
|
||||||
|
// 设置主机名
|
||||||
|
.setHostName("root")
|
||||||
|
// 设置实例密码
|
||||||
|
.setPassword("EightGroup123.")
|
||||||
|
// 设置创建实例的数量
|
||||||
|
.setAmount(1);
|
||||||
|
|
||||||
|
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||||
|
try {
|
||||||
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
RunInstancesResponse runInstancesResponse = client.runInstancesWithOptions(runInstancesRequest, runtime);
|
||||||
|
// 获取body返回值对象
|
||||||
|
RunInstancesResponseBody body = runInstancesResponse.getBody();
|
||||||
|
ArrayList<String> list = new ArrayList<>();
|
||||||
|
// 得到实例ID数组
|
||||||
|
for (String instance : body.getInstanceIdSets().getInstanceIdSet()) {
|
||||||
|
list.add(instance);
|
||||||
|
}
|
||||||
|
log.info("实例ID:{}",list);
|
||||||
|
return list;
|
||||||
|
} catch (TeaException error) {
|
||||||
|
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||||
|
// 错误 message
|
||||||
|
log.info(error.getMessage());
|
||||||
|
// 诊断地址
|
||||||
|
log.info(error.getData().get("Recommend"));
|
||||||
|
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||||
|
} catch (Exception _error) {
|
||||||
|
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||||
|
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||||
|
// 错误 message
|
||||||
|
log.info(error.getMessage());
|
||||||
|
// 诊断地址
|
||||||
|
log.info(error.getData().get("Recommend"));
|
||||||
|
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue