diff --git a/cloud-modules/cloud-modules-car-gateway/pom.xml b/cloud-modules/cloud-modules-car-gateway/pom.xml index c602346..1fd8bb1 100644 --- a/cloud-modules/cloud-modules-car-gateway/pom.xml +++ b/cloud-modules/cloud-modules-car-gateway/pom.xml @@ -86,12 +86,6 @@ cloud-common-core - - com.muyu - cloud-modules-car - 3.6.3 - - com.aliyun ecs20140526 @@ -108,6 +102,10 @@ redis.clients jedis + + com.muyu + cloud-common-security + diff --git a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/domain/api/WebHookConnection.java b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/domain/api/WebHookConnection.java new file mode 100644 index 0000000..7a8eae3 --- /dev/null +++ b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/domain/api/WebHookConnection.java @@ -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; + +} diff --git a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/controller/FluxMqCallbackController.java b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/controller/FluxMqCallbackController.java new file mode 100644 index 0000000..ff4d397 --- /dev/null +++ b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/controller/FluxMqCallbackController.java @@ -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 online(WebHookConnection webHookConnection){ + rabbitTemplate.convertAndSend("getaway","fluxmq",webHookConnection); + return Result.success(); + } + +} diff --git a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/controller/VehicleInformationController.java b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/controller/VehicleInformationController.java index ac4282a..b135979 100644 --- a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/controller/VehicleInformationController.java +++ b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/controller/VehicleInformationController.java @@ -29,7 +29,7 @@ public class VehicleInformationController { @Validated @RequestBody VehicleConnectionReq vehicleConnectionReq ) { - return Result.success(vehicleInformationService.getVehicleData(vehicleConnectionReq)); + return vehicleInformationService.getVehicleData(vehicleConnectionReq); } } diff --git a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/mq/CreateExchange.java b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/mq/CreateExchange.java index 827ccde..60581a2 100644 --- a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/mq/CreateExchange.java +++ b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/mq/CreateExchange.java @@ -1,5 +1,6 @@ package com.muyu.car.gateway.mq; +import com.muyu.common.rabbit.constants.VehicleGatewayConstants; import lombok.extern.log4j.Log4j2; import org.springframework.amqp.core.*; 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.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; +import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; @@ -19,35 +21,14 @@ import org.springframework.stereotype.Component; */ @Log4j2 @Component -public class CreateExchange implements ApplicationRunner { +public class CreateExchange { - @Autowired - private ConnectionFactory connectionFactory; - - @Override - public void run(ApplicationArguments args) { - log.info("=====>开始创建交换机"); - - 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()); - } + /** + * 创建网关路由交换机 + * @return + */ + @Bean + public DirectExchange initVehicleGatewayExchange(){ + return new DirectExchange(VehicleGatewayConstants.VEHICLE_GETAWAY_EXCHANGE); } } diff --git a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/VehicleInformationService.java b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/VehicleInformationService.java index f742a74..3086cf5 100644 --- a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/VehicleInformationService.java +++ b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/VehicleInformationService.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService; import com.muyu.car.domain.api.req.VehicleConnectionReq; import com.muyu.car.domain.model.MqttServerModel; import com.muyu.car.domain.model.VehicleInformation; +import com.muyu.common.core.domain.Result; /** * @Author:蓬叁 @@ -13,5 +14,5 @@ import com.muyu.car.domain.model.VehicleInformation; * @Date:2024/10/6 下午2:40 */ public interface VehicleInformationService{ - MqttServerModel getVehicleData(VehicleConnectionReq vehicleConnectionReq); + Result getVehicleData(VehicleConnectionReq vehicleConnectionReq); } diff --git a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/impl/VehicleInformationServiceImpl.java b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/impl/VehicleInformationServiceImpl.java index 2719b75..f1fec65 100644 --- a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/impl/VehicleInformationServiceImpl.java +++ b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/gateway/service/impl/VehicleInformationServiceImpl.java @@ -6,6 +6,7 @@ import com.muyu.car.domain.model.MqttServerModel; import com.muyu.car.domain.model.VehicleInformation; import com.muyu.car.gateway.service.VehicleInformationService; import com.muyu.car.mapper.VehicleInformationMapper; +import com.muyu.common.core.domain.Result; import com.muyu.common.redis.service.RedisService; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; @@ -33,7 +34,7 @@ public class VehicleInformationServiceImpl implements VehicleInformationService{ @Autowired private RedisService redisService; @Override - public MqttServerModel getVehicleData(VehicleConnectionReq vehicleConnectionReq) { + public Result getVehicleData(VehicleConnectionReq vehicleConnectionReq) { log.info("车辆连接请求:[{}]",vehicleConnectionReq); vehicleConnectionReq.setPassword(vehicleConnectionReq.getVehicleVin()+vehicleConnectionReq.getTimestamp()+vehicleConnectionReq.getNonce()); @@ -45,16 +46,16 @@ public class VehicleInformationServiceImpl implements VehicleInformationService{ log.info("车辆无法重复预上线"); } - // 获取名为 "ipList" 的列表 + // 获取 IP 的列表 List ipList = redisService.getCacheList("ipList"); if (ipList.isEmpty()) { - return null; + return Result.error("IP服务器列表为空"); } // 获取当前使用的索引位置 String indexStr = redisService.getCacheObject("currentIndex"); int index = indexStr!= null? Integer.parseInt(indexStr) : 0; String selectedIp = ipList.get(index); - // 获取该 IP 的使用次数 + // 获取 IP 的使用次数 String countStr = redisService.getCacheMapValue("ipCounts", selectedIp); log.info("IP:[{}]车辆连接数:[{}]",selectedIp,countStr); int count = countStr!= null? Integer.parseInt(countStr) : 0; @@ -65,7 +66,7 @@ public class VehicleInformationServiceImpl implements VehicleInformationService{ // 更新索引 index = (index + 1) % ipList.size(); 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 { // 如果使用次数达到 12 次,跳过该 IP 并更新索引 index = (index + 1) % ipList.size(); diff --git a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/instance/GenerateInstance.java b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/instance/GenerateInstance.java index 7ce87c7..635ff57 100644 --- a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/instance/GenerateInstance.java +++ b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/instance/GenerateInstance.java @@ -45,7 +45,7 @@ public class GenerateInstance implements ApplicationRunner { // 设置地域ID .setRegionId("cn-hangzhou") // 设置镜像ID - .setImageId("m-bp1hkxfctk751s62jqhq") + .setImageId("m-bp1epdalpjow84ornf09") // 设置实例类型 .setInstanceType("ecs.t6-c1m1.large") // 设置安全组ID diff --git a/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/test.java b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/test.java new file mode 100644 index 0000000..3d1fe4d --- /dev/null +++ b/cloud-modules/cloud-modules-car-gateway/src/main/java/com/muyu/car/test.java @@ -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 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 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; + } + +}