【EMQX】 Spring Cloud 集成MQTT并异步入库(mongodb)
创始人
2024-02-05 19:33:48
0

1. 需求:

        1. Spring boot项目,集成mqtt,订阅物联网设备数据上报topic1

        2. 处理topic1,并异步入库到mongodb

        3. mongodb 根据设备id分集合

2. 代码实现

2.1 引入pom

  • 子项目(本项目)
        org.eclipse.pahoorg.eclipse.paho.client.mqttv31.2.2org.springframework.bootspring-boot-starter-data-mongodbcom.spring4allmongodb-plus-spring-boot-starter
  • 父-引入pom
    org.springframework.bootspring-boot-starter-parent2.3.5.RELEASEorg.springframework.cloudspring-cloud-dependenciesHoxton.SR12pomimport

2.2 启动类引入注解 @EnableMongoPlus

2.3  application.yml mongodb配置

spring:profiles:active: proddata:mongodb:uri: mongodb://root:${mongo-password}@${host}:${mongodbport}/hzdoption:# 最小连接数min-connection-per-host: 10# 最大连接数max-connection-per-host: 200#允许阻塞连接乘数的线程threads-allowed-to-block-for-connection-multiplier: 5#服务器超时server-selection-timeout: 30000#最长等待时间max-wait-time: 120000#最大连接空闲时间max-connection-idle-time: 0#最大连接生存时间max-connection-life-time: 0#连接超时connect-timeout: 10000#套接字超时10000socket-timeout: 0socket-keep-alive: falsessl-enabled: falsessl-invalid-host-name-allowed: falsealways-use-m-beans: falseheartbeat-socket-timeout: 20000heartbeat-connect-timeout: 20000min-heartbeat-frequency: 500heartbeat-frequency: 10000local-threshold: 15

2.4 代码

1. EmqClient.java                  连接emqx

  • init()  使用@PostConstruct注解,开机自动连接EMQX + 订阅
  • reConnect()  断线重连后,订阅主题
package com.hzd.mqtt.client;import com.hzd.mqtt.enums.QosEnum;
import com.hzd.mqtt.properties.MqttProperties;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;/*** Created by*/
@Component
@Slf4j
public class EmqClient {private IMqttClient client;@Autowiredprivate MqttProperties mqttProperties;@Autowiredprivate MqttCallback mqttCallback;@Autowiredprivate MqttProperties properties;private static final String topic_hzd = "/hzd/pub/#";private static final String topic_device_connect = "/device/connect/#";@PostConstructpublic void init() {MqttClientPersistence mempersitence = new MemoryPersistence();try {if (client == null) {client = new MqttClient(mqttProperties.getBrokerUrl(), mqttProperties.getClientId(), mempersitence);}} catch (MqttException e) {log.error("初始化客户端mqttClient对象失败,errormsg={},brokerUrl={},clientId={}", e.getMessage(), mqttProperties.getBrokerUrl(), mqttProperties.getClientId());}//连接服务端connect(properties.getUsername(), properties.getPassword());//订阅一个主题subscribe(topic_hzd, QosEnum.QoS2);subscribe(topic_device_connect, QosEnum.QoS2);log.info("EmqClient连接成功,订阅成功.topic={},{}", topic_hzd, topic_device_connect);}/*** 连接broker** @param username* @param password*/public void connect(String username, String password) {MqttConnectOptions options = new MqttConnectOptions();options.setAutomaticReconnect(true);options.setUserName(username);options.setPassword(password.toCharArray());options.setCleanSession(true);client.setCallback(mqttCallback);try {client.connect(options);} catch (MqttException e) {log.error("mqtt客户端连接服务端失败,失败原因{}", e.getMessage());}}/*** 断开连接*/@PreDestroypublic void disConnect() {try {client.disconnect();} catch (MqttException e) {log.error("断开连接产生异常,异常信息{}", e.getMessage());}}/*** 重连*/public void reConnect() {try {client.reconnect();while (true){if (client.isConnected()){subscribe(topic_hzd, QosEnum.QoS2);subscribe(topic_device_connect, QosEnum.QoS2);break;}else{Thread.sleep(100);log.info("重连中,请稍后......");}}log.info("EmqClient连接成功,订阅成功.topic={},{}", topic_hzd, topic_device_connect);} catch (MqttException | InterruptedException e) {log.error("重连失败,失败原因{}", e.getMessage());}}/*** 发布消息** @param topic* @param msg* @param qos* @param retain*/public void publish(String topic, String msg, QosEnum qos, boolean retain) {MqttMessage mqttMessage = new MqttMessage();mqttMessage.setPayload(msg.getBytes());mqttMessage.setQos(qos.value());mqttMessage.setRetained(retain);try {client.publish(topic, mqttMessage);} catch (MqttException e) {log.error("发布消息失败,errormsg={},topic={},msg={},qos={},retain={}", e.getMessage(), topic, msg, qos.value(), retain);}}/*** 订阅** @param topicFilter* @param qos*/public void subscribe(String topicFilter, QosEnum qos) {try {client.subscribe(topicFilter, qos.value());} catch (MqttException e) {log.error("订阅主题失败,errormsg={},topicFilter={},qos={}", e.getMessage(), topicFilter, qos.value());}}/*** 取消订阅** @param topicFilter*/public void unSubscribe(String topicFilter) {try {client.unsubscribe(topicFilter);} catch (MqttException e) {log.error("取消订阅失败,errormsg={},topicfiler={}", e.getMessage(), topicFilter);}}}

2. MessageCallback.java         处理订阅消息

  • init() 加载异步入库处理类DataStorage
  • connectionLost(Throwable cause) 断线重连
  • messageArrived(String topic, MqttMessage message)  处理订阅消息
package com.hzd.mqtt.client;import com.hzd.mqtt.async.DataStorage;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;/****/
@Component
@Slf4j
public class MessageCallback implements MqttCallback {@Autowiredprivate DataStorage dataStorage;@Autowiredprivate EmqClient emqClient;public static MessageCallback MessageCallback;@PostConstruct //通过@PostConstruct实现初始化bean之前进行的操作public void init() {log.info("初始化");MessageCallback = this;
//        MessageCallback.dataPageRepository = this.dataPageRepository;MessageCallback.dataStorage = this.dataStorage;}/*** 丢失了对服务端的连接后触发的回调** @param cause*/@Overridepublic void connectionLost(Throwable cause) {// 资源的清理  重连log.info("断开emqx连接,开始重连......");emqClient.reConnect();log.info("emqx连接成功......");}/*** 应用收到消息后触发的回调** @param topic* @param message* @throws Exception*/@Overridepublic void messageArrived(String topic, MqttMessage message) throws Exception {dataStorage.saveDataToMongo(topic, message);}/*** 消息发布者消息发布完成产生的回调** @param token*/@Overridepublic void deliveryComplete(IMqttDeliveryToken token) {int messageId = token.getMessageId();String[] topics = token.getTopics();log.info("消息发布完成,messageid={},topics={}", messageId, topics);}
}


3. DataStorage.java          异步入库处理类

package com.hzd.mqtt.async;import com.alibaba.fastjson2.JSON;
import com.hzd.entity.po.ControlLog;
import com.hzd.entity.po.DataExtend;
import com.hzd.entity.po.MqttClientLog;
import com.hzd.entity.vo.MqttPayload;
import com.hzd.entity.vo.MqttTopic;
import com.hzd.mqtt.enums.TopicType;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.data.mongodb.UncategorizedMongoDbException;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.util.Date;/*** Coding by 李炯 on 2022/10/28 15:12*/
@Component
@Slf4j
public class DataStorage {@Resourceprivate MongoTemplate mongoTemplate;//    @Resource
//    private DataPageRepository dataPageRepository;@Asyncpublic void saveDataToMongo(String topic, MqttMessage message) {MqttTopic t = MqttTopic.init(topic);String payload = new String(message.getPayload());if (t == null) return;if (payload.contains("\"type\":\"data\"")) {DataExtend dataExtend = DataExtend.init(payload);mongoTemplate.insert(dataExtend, t.getFac_id());log.info("设备数据上报,topic={},fac_id={},messageid={},qos={},payload={}",topic, t.getFac_id(), message.getId(), message.getQos(), payload);} else if (payload.contains("\"type\":\"config\"")) {ControlLog controlLog = ControlLog.init(payload);Query query = new Query(Criteria.where("_id").is(controlLog.get_id()));Update update = Update.update("status", controlLog.getStatus());update.set("status", controlLog.getStatus());update.set("msg", controlLog.getMsg());if (null != controlLog.getRelayIndexLst()) {update.set("relayIndexLst", controlLog.getRelayIndexLst());}if (null != controlLog.getRelayNumLst()) {update.set("relayNumLst", controlLog.getRelayNumLst());}if (null != controlLog.getReadInterval()) {update.set("readInterval", controlLog.getReadInterval());}if (null != controlLog.getRelayphoto()) {update.set("relayphoto", controlLog.getRelayphoto());}if (null != controlLog.getLongitude()) {update.set("longitude", controlLog.getLongitude());}if (null != controlLog.getLatitude()) {update.set("latitude", controlLog.getLatitude());}if (null != controlLog.getControlStatus()) {update.set("controlStatus", controlLog.getControlStatus());}if (null != controlLog.getReserve()) {update.set("reserve", controlLog.getReserve());}update.set("updateTime", new Date());mongoTemplate.upsert(query, update, "log_control");log.info("{},topic={},messageid={},qos={},payload={}","配置返回", topic, message.getId(), message.getQos(), payload);} else if (t.getTopicType() == TopicType.connect) {MqttPayload pl = MqttPayload.init(payload);MqttClientLog clientLog = MqttClientLog.init(pl);updateConnectStatus(topic, clientLog);log.info("{},topic={},messageid={},qos={},payload={}",clientLog.getStatus(), topic, message.getId(), message.getQos(), payload);}}@Asyncpublic void updateConnectStatus(String topic, MqttClientLog clientLog) {try{MqttTopic t = MqttTopic.init(topic);if (t == null) return;mongoTemplate.insert(clientLog, "log_mqtt_client");Query query = new Query(Criteria.where("username").is(clientLog.getUserName()).and("deviceid").is(clientLog.getDeviceId()));Update update = Update.update("connectStatus", clientLog.getConnectStatus());update.set("status", clientLog.getStatus());update.set("updateTime", clientLog.getUpdateTime());mongoTemplate.upsert(query, update, "status_mqtt_client");}catch (Exception e){log.error("mongoDB 错误:MqttClientLog={},错误消息为{}",JSON.toJSONString(clientLog),e.getMessage());}}
}

相关内容

热门资讯

李光洁32天走7500公里吃8... 李光洁32天走7500公里吃8城,《拿一座城市下酒》这部纪录片怎么样?我觉得这部纪录片非常好,观看的...
每我世如你果只没界喜就欢的生一... 每我世如你果只没界喜就欢的生一爱有你过在想会。把这22个字组成一句话。我想过,如果在每一生只喜欢你,...
调查校园里的植物和动物说说有哪... 调查校园里的植物和动物说说有哪些动植物?可以分成几类?兰花,梅花可人丌··植物:乔木(杨树、柳树、银...
血脂高的原因? 血脂高的原因?血脂高的原因高血脂的诱因包括原发性和继发性两种:原发性高血脂症的病因:1、遗传因素。2...
为什么孩子总是重复看同一集动画... 为什么孩子总是重复看同一集动画片?是在传递这3个信号 小孩子爱看动画片是非常普遍的,动画片带给他们动...
终极三国里 49集刘备为什么这... 终极三国里 49集刘备为什么这么做?有没有官方回答?下集自己看吧 现在不会有官方回答的哟第一:可能真...
西式糕点制作大全的内容简介 西式糕点制作大全的内容简介《西式糕点制作大全》主要介绍了制作甜点的基本知识,例如各种制作工具,制作点...
逻辑思维又是什么?就是推理吗,... 逻辑思维又是什么?就是推理吗,怎么培养!?逻辑思维是一种严格分析思维。不一定是推理。推理是逻辑思维的...
囊萤夜读有一句俗语就是出自这个... 囊萤夜读有一句俗语就是出自这个故事你知道是什么吗?囊萤映雪 ( náng yíng yìng xuě...
什么是不伦恋情? 什么是不伦恋情?什么是不伦恋情.,?男跟女年龄相差很大?还是?男的比女的小?还是老夫少妻?是近亲谈恋...
有书名带晨星的嘛? 有书名带晨星的嘛?有书名带晨星的嘛?晨星传这本书。漫画书晨星物语
如果有些事情说不出口怎么办? 如果有些事情说不出口怎么办?烦恼皆是因为自己过分的执着 即使你在这样子下去 更不就不会有好的结果 为...
有哪些类似于《非自然死亡》题材... 有哪些类似于《非自然死亡》题材的日剧推荐?非自然死亡的题材电影确实不多,电视剧的话也不好找啊。不喜欢...
素书全集的内容简介 素书全集的内容简介 本书采用了《素书》的权威原著,参照《四库全书》并加上了宋代宰相张商英的注和清代王...
【世纪花园】小区对口的学校有重... 【世纪花园】小区对口的学校有重点小学和初中吗?世纪花园东区里有未来强者幼儿园,小区南边有个华兴小区,...
东南大学现有的专业中有哪些是属... 东南大学现有的专业中有哪些是属于老东南的1928年学校改名为国立中央大学,设理、工、医、农、文、法、...
一个男人一有钱就请朋友吃饭,没... 一个男人一有钱就请朋友吃饭,没钱就又说,买东西还赊账,商店里的老板都找上门来了?像陵念前这种男人的话...
坟上栽什么草好? 坟上栽什么草好?坟地种什么草好耐旱坟上栽野蕨草、扎根不深、浅根植物、可以固土、南方雨水多、不会造成坟...
关于国富潜力基金 关于国富潜力基金我9月24日上午买的国富基金,申购价格是9月24日开盘的价格吗?还是9月28日开盘的...
我是一个高中生。想学武术。在学... 我是一个高中生。想学武术。在学校没什么时间。是练散打还是跆拳道好。我是一个高中生。想学武术。在学校没...