Browse Source

'在service-iot服务模块新增rabbitMq配置,优化APP用户心跳信息-新增接口,增加RabbitMQ推送心跳消息,监听消息'

james 10 months ago
parent
commit
8b760fb4bf

+ 4 - 0
service-iot/service-iot-biz/pom.xml

@@ -71,6 +71,10 @@
             <artifactId>weixin-java-mp</artifactId>
             <version>4.3.0</version>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-amqp</artifactId>
+        </dependency>
 
     </dependencies>
 

+ 2 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/RuoYiSystemApplication.java

@@ -11,6 +11,7 @@ import me.chanjar.weixin.mp.config.impl.WxMpMapConfigImpl;
 import org.mybatis.spring.annotation.MapperScan;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.amqp.rabbit.annotation.EnableRabbit;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.openfeign.EnableFeignClients;
@@ -33,6 +34,7 @@ import java.net.UnknownHostException;
 @MapperScan(value = "com.usky.iot.mapper")
 @ComponentScan("com.usky")
 @SpringBootApplication
+@EnableRabbit
 public class RuoYiSystemApplication
 {
     private static final Logger LOGGER = LoggerFactory.getLogger(RuoYiSystemApplication.class);

+ 44 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/config/rabbitmq/RabbitMQConfig.java

@@ -0,0 +1,44 @@
+package com.usky.iot.service.config.rabbitmq;
+
+import org.springframework.amqp.core.*;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class RabbitMQConfig {
+
+    //心跳广播
+    public String infoFEventExchange = "Info_FEvent";
+
+    @Bean
+    public FanoutExchange infoFExchange(){
+        return new FanoutExchange(infoFEventExchange,true,false);
+    }
+
+
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 18 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/BaseAppInfoServiceImpl.java

@@ -7,6 +7,8 @@ import com.usky.iot.domain.BaseAppInfo;
 import com.usky.iot.mapper.BaseAppInfoMapper;
 import com.usky.iot.service.BaseAppInfoService;
 import com.usky.common.mybatis.core.AbstractCrudService;
+import com.usky.iot.service.config.rabbitmq.RabbitMQConfig;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -14,8 +16,11 @@ import org.springframework.web.context.request.RequestAttributes;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
 
+import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import java.time.LocalDateTime;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * <p>
@@ -34,6 +39,12 @@ public class BaseAppInfoServiceImpl extends AbstractCrudService<BaseAppInfoMappe
     @Value("${agBox.push}")
     private Integer pushFlag;
 
+    @Resource
+    private RabbitTemplate rabbitTemplate;
+
+    @Autowired
+    private RabbitMQConfig rabbitMQConfig;
+
     public String getIpAddress(HttpServletRequest request) {
         String ip = request.getHeader("X-Forwarded-For");
         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
@@ -68,6 +79,13 @@ public class BaseAppInfoServiceImpl extends AbstractCrudService<BaseAppInfoMappe
         baseAppInfo.setCreateBy(SecurityUtils.getUsername());
         baseAppInfo.setCreateTime(LocalDateTime.now());
         baseAppInfo.setTenantId(SecurityUtils.getTenantId());
+
+        Map<String,Object> map = new HashMap<>();
+        map.put("deviceId",baseAppInfo.getDeviceId());
+        map.put("userName",baseAppInfo.getUserName());
+        map.put("createTime",baseAppInfo.getCreateTime());
+        rabbitTemplate.convertAndSend(rabbitMQConfig.infoFEventExchange,"",JSONObject.toJSONString(map));
+
         this.save(baseAppInfo);
         if (pushFlag.equals(1)){
             JSONObject a = remotePatrolAgboxService.updateHeart(jsonObj.toJSONString());

+ 29 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/listener/RabbitMQListener.java

@@ -0,0 +1,29 @@
+package com.usky.iot.service.listener;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.core.ExchangeTypes;
+import org.springframework.amqp.core.Message;
+import org.springframework.amqp.rabbit.annotation.*;
+import org.springframework.stereotype.Component;
+
+import java.util.Map;
+
+@Slf4j
+@Component
+public class RabbitMQListener {
+
+
+    @RabbitHandler
+    @RabbitListener(bindings = @QueueBinding(
+            value = @Queue(),
+            exchange = @Exchange(value = "Info_FEvent",type = ExchangeTypes.FANOUT)
+    ))
+    public void getData(Message message){
+        try {
+            String str = new String(message.getBody(),"utf-8");
+            System.out.println("FanoutReceiver消费者收到心跳消息: " + str);
+        } catch (Exception e){
+            e.printStackTrace();
+        }
+    }
+}