diff --git a/couplet-common/couplet-common-business/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/couplet-common/couplet-common-business/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 12794fc..f797d3a 100644 --- a/couplet-common/couplet-common-business/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/couplet-common/couplet-common-business/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,2 +1,2 @@ -com.couplet.remote.factory.RemoteTroubleFallbackFactory com.couplet.remote.factory.RemoteVehicleFallbackFactory +com.couplet.remote.factory.RemoteTroubleFallbackFactory diff --git a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/CoupletBusinessApplication.java b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/CoupletBusinessApplication.java index 4edc8c5..4fc051b 100644 --- a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/CoupletBusinessApplication.java +++ b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/CoupletBusinessApplication.java @@ -15,10 +15,10 @@ import org.springframework.scheduling.annotation.EnableScheduling; @EnableCustomConfig @EnableCustomSwagger2 @EnableMyFeignClients(basePackages = ("com.couplet")) -@SpringBootApplication(scanBasePackages = {"com.couplet"}) +@SpringBootApplication(scanBasePackages = {"com.couplet", "com.couplet.mq.remote"}) @EnableScheduling public class CoupletBusinessApplication { - public static void main (String[] args) { + public static void main(String[] args) { SpringApplication.run(CoupletBusinessApplication.class, args); } diff --git a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/impl/VehicleServiceImpl.java b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/impl/VehicleServiceImpl.java index 7968448..06fcf6a 100644 --- a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/impl/VehicleServiceImpl.java +++ b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/service/impl/VehicleServiceImpl.java @@ -16,6 +16,7 @@ import com.couplet.common.domain.VehicleType; import com.couplet.common.domain.request.VehicleEditParams; import com.couplet.common.domain.request.VehicleInsertParams; import com.couplet.common.domain.request.VehicleListParams; +import com.couplet.mq.remote.RemoteFenceService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -36,6 +37,10 @@ public class VehicleServiceImpl extends ServiceImpl impl @Autowired private VehicleMapper vehicleMapper; + //远程发送mq + @Autowired + private RemoteFenceService remoteFenceService; + //车辆类型服务 @Autowired private VehicleTypeService vehicleTypeService; @@ -166,6 +171,19 @@ public class VehicleServiceImpl extends ServiceImpl impl //删除绑定之后,再添加新的绑定 vehicleAndLogoService.vehicleBindLogo(editParams.getVehicleId(), editParams.getLogoIds()); + + //mq + List logoList = getBindLogoById(editParams.getVehicleId()); + if (0 != logoList.size()) { + String ids = ""; + for (Long l : logoList) { + ids = "," + l; + } + ids = ids.substring(1); + remoteFenceService.vehicleQueue(editParams.getVehicleId() + "-" + ids); + } + + result = "编辑成功!"; return result; @@ -234,6 +252,17 @@ public class VehicleServiceImpl extends ServiceImpl impl int i = vehicleAndLogoService.vehicleBindLogo(vehicle.getVehicleId(), insertParams.getLogoIds()); + List logoList = getBindLogoById(vehicle.getVehicleId()); + if (0 != logoList.size()) { + String ids = ""; + for (Long l : logoList) { + ids = "," + l; + } + ids = ids.substring(1); + remoteFenceService.vehicleQueue(vehicle.getVehicleId() + "-" + ids); + } + + result = "新增成功!"; return result; @@ -256,7 +285,7 @@ public class VehicleServiceImpl extends ServiceImpl impl /* * @param userId: - * @return List + * @return List * @author 付凡芮 * @description 根据登入人id查询管理车辆 * @@ -304,4 +333,5 @@ public class VehicleServiceImpl extends ServiceImpl impl return this.list(queryWrapper); } + } diff --git a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/time/Timer.java b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/time/Timer.java index 95c11bd..c2c3925 100644 --- a/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/time/Timer.java +++ b/couplet-modules/couplet-business/src/main/java/com/couplet/business/server/time/Timer.java @@ -3,7 +3,6 @@ package com.couplet.business.server.time; import com.couplet.business.server.service.VehicleService; import com.couplet.common.domain.Vehicle; import com.couplet.common.domain.request.VehicleListParams; -import com.couplet.common.redis.service.RedisService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; @@ -29,32 +28,44 @@ public class Timer { private VehicleService vehicleService; + @Scheduled(cron = "0/1 * * * * *") + public void aa() { + System.out.println("********************************************************"); + } + //判断车辆是否下线 - @Scheduled(cron = "* * * * * *") - public void outLine() { + @Scheduled(cron = "0/1 * * * * *") + public void downLine() { log.info("定时器启动"); //先查询车辆列表 List list = vehicleService.list(new VehicleListParams(null, null, null, null)); - for (Vehicle vehicle : list) { - //只针对已经上线的车辆 - if (redis.hasKey(vehicle.getVin())) { - //如果vin的缓存 时间还剩一秒,则判断为已经下线 - if (redis.getExpire(vehicle.getVin()) <= 3) { - log.info(vehicle.getVin() + "的车辆已经下线"); - //执行修改下线状态的方法 - Integer i = vehicleService.onOrOutLineByVIN(vehicle.getVin(), 0); + list.forEach(vehicle -> { + try { + //只针对已经上线的车辆 + if (redis.hasKey(vehicle.getVin())) { -// if (0 == 1) { -// log.error("下线状态修改失败"); -// } + //如果vin的缓存 时间还剩一秒,则判断为已经下线 + if (redis.getExpire(vehicle.getVin()) <= 3) { + log.info(vehicle.getVin() + "的车辆已经下线"); - log.info("下线状态修改成功"); + //执行修改下线状态的方法 + Integer i = vehicleService.onOrOutLineByVIN(vehicle.getVin(), 0); + + if (0 == i) { + log.error("下线状态修改失败"); + } else { + log.info("下线状态修改成功"); + } + + } } + } catch (Exception e) { + log.error(e.getMessage()); } - } + }); } diff --git a/couplet-modules/couplet-business/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/couplet-modules/couplet-business/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports deleted file mode 100644 index ff28764..0000000 --- a/couplet-modules/couplet-business/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ /dev/null @@ -1 +0,0 @@ -com.couplet.business.server.time.Timer diff --git a/couplet-modules/couplet-business/src/main/resources/bootstrap.yml b/couplet-modules/couplet-business/src/main/resources/bootstrap.yml index d900876..b88e464 100644 --- a/couplet-modules/couplet-business/src/main/resources/bootstrap.yml +++ b/couplet-modules/couplet-business/src/main/resources/bootstrap.yml @@ -16,9 +16,11 @@ spring: discovery: # 服务注册地址 server-addr: 121.89.211.230:8848 + namespace: 172469 config: # 配置中心地址 server-addr: 121.89.211.230:8848 + namespace: 172469 # 配置文件格式 file-extension: yml # 共享配置 diff --git a/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/config/RabbitMQConfig.java b/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/config/RabbitMQConfig.java index f203824..5e789ec 100644 --- a/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/config/RabbitMQConfig.java +++ b/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/config/RabbitMQConfig.java @@ -27,15 +27,18 @@ public class RabbitMQConfig implements RabbitTemplate.ConfirmCallback, RabbitTem @Value("queueName") public String queueName; //队列名 - public static final String finByVinQueueName="finByVinQueueName"; - public static final String FENCE_QUEUE ="fenceQueue"; + public static final String finByVinQueueName = "finByVinQueueName"; + public static final String FENCE_QUEUE = "fenceQueue"; + public static final String vehicleQueue = "vehicleQueue"; //交换机 - public static final String VinExchangeName="vinExchangeName"; - public static final String FENCE_EXCHANGE="fenceExchange"; + public static final String VinExchangeName = "vinExchangeName"; + public static final String FENCE_EXCHANGE = "fenceExchange"; + public static final String vehicleQueueExchange = "vehicleQueueExchange"; //路由键 - public static final String VinRoutingKey="vinRoutingKey"; - public static final String FENCE_ROUTINGKEY="fenceRoutingKey"; + public static final String VinRoutingKey = "vinRoutingKey"; + public static final String FENCE_ROUTINGKEY = "fenceRoutingKey"; + public static final String VehicleQueue_ROUTINGKEY = "VehicleQueueRoutingKey"; //队列名 @@ -74,15 +77,23 @@ public class RabbitMQConfig implements RabbitTemplate.ConfirmCallback, RabbitTem public Queue queue() { return new Queue(queueName, true); } + @Bean("fenceQueue") public Queue queue2() { return new Queue(FENCE_QUEUE, true); } + @Bean("finByVinQueueName") public Queue finByVinQueueName() { return new Queue(finByVinQueueName, true); } + //当增删改车辆的时候,发送当前车辆的id到"vehicleQueue"队列 + @Bean("vehicleQueue") + public Queue vehicleQueue() { + return new Queue("vehicleQueue", true); + } + /* * @Author: LiuYunHu * @Date: 2024/3/29 21:26 @@ -100,11 +111,16 @@ public class RabbitMQConfig implements RabbitTemplate.ConfirmCallback, RabbitTem return new DirectExchange(FENCE_EXCHANGE); } - @Bean("vinExchangeName") + @Bean("vinExchangeName") public DirectExchange vinExchangeName() { return new DirectExchange(VinExchangeName); } + @Bean("vehicleQueueExchange") + public DirectExchange vehicleQueueExchange() { + return new DirectExchange("vehicleQueueExchange"); + } + /* * @Author: LiuYunHu @@ -152,11 +168,17 @@ public class RabbitMQConfig implements RabbitTemplate.ConfirmCallback, RabbitTem public Binding binding2() { return BindingBuilder.bind(queue2()).to(directExchange2()).with(FENCE_ROUTINGKEY); } + @Bean("vinRoutingKey") public Binding binding3() { return BindingBuilder.bind(finByVinQueueName()).to(vinExchangeName()).with(VinRoutingKey); } + @Bean("VehicleQueueRoutingKey") + public Binding binding4() { + return BindingBuilder.bind(vehicleQueue()).to(vehicleQueueExchange()).with(VehicleQueue_ROUTINGKEY); + } + /* * @Author: LiuYunHu * @Date: 2024/3/29 21:28 diff --git a/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/controller/MqController.java b/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/controller/MqController.java index d7be058..f1921d2 100644 --- a/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/controller/MqController.java +++ b/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/controller/MqController.java @@ -90,4 +90,13 @@ public class MqController { }, new CorrelationData(IdUtils.randomUUID()) ); } + + @GetMapping("/vehicleQueue/{msg}") + public void vehicleQueue(@PathVariable("msg") String msg){ + rabbitTemplate.convertAndSend(RabbitMQConfig.vehicleQueueExchange, RabbitMQConfig.VehicleQueue_ROUTINGKEY, msg, message -> { + message.getMessageProperties().setMessageId(IdUtils.randomUUID()); + return message; + }, new CorrelationData(IdUtils.randomUUID()) + ); + } } diff --git a/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/remote/RemoteFenceService.java b/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/remote/RemoteFenceService.java index a764791..bf41efe 100644 --- a/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/remote/RemoteFenceService.java +++ b/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/remote/RemoteFenceService.java @@ -4,6 +4,8 @@ import com.couplet.common.core.constant.ServiceNameConstants; import com.couplet.common.domain.request.FenceUpdateRequest; import com.couplet.mq.remote.factory.RemoteFenceFallbackFactory; import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -16,4 +18,7 @@ public interface RemoteFenceService { @PostMapping("/mq/fenceQueue") public void fenceQueue(@RequestBody FenceUpdateRequest teRequest); + + @GetMapping("/mq/vehicleQueue/{msg}") + public void vehicleQueue(@PathVariable("msg") String msg); } diff --git a/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/remote/factory/RemoteFenceFallbackFactory.java b/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/remote/factory/RemoteFenceFallbackFactory.java index 24d5e0b..4db267a 100644 --- a/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/remote/factory/RemoteFenceFallbackFactory.java +++ b/couplet-modules/couplet-modules-mq/src/main/java/com/couplet/mq/remote/factory/RemoteFenceFallbackFactory.java @@ -25,6 +25,11 @@ public class RemoteFenceFallbackFactory implements FallbackFactory