分布式物联网平台EMQX-MQTT


目录
  • 1、安装
  • 2、整合springboot
      • 报错处理
  • 3、简易版客户端
  • 4、设备模拟
  • 6、可能遇到的问题

官方文档地址

1、安装

docker部署

docker run -d --name mqtt-broker-emqx-zhgsgl -p 1883:1883 -p 8081:8081 -p 8083:8083 -p 8883:8883 -p 8084:8084 -p 18083:18083 emqx/emqx:v4.0.0

http://192.168.2.201:18083/

admin/public

2、整合springboot

集成springboot文档

报错处理

整合过程中发生已存在的bean定义,需要在配置文件添加配置

@MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
public interface MqttGateway {

    void sendToMqtt(String data, @Header(MqttHeaders.TOPIC) String topic);

}
main:
  allow-bean-definition-overriding: true

3、简易版客户端

参考:

// package com.jtsmartway.zhgsgl.iot.platform.mqttbroker;
//
// import org.eclipse.paho.client.mqttv3.*;
// import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
// import org.springframework.context.annotation.Bean;
// import org.springframework.context.annotation.Configuration;
//
// /**
//  * @author JHL
//  * @version 1.0
//  * @date 2022/5/17 18:07
//  * @since : JDK 11
//  */
// @Configuration
// public class TestEmqxClient {
//
//     
//
//     @Bean
//     public MqttClient getMqttClient() {
//         try {
//             // host为主机名,test为clientid即连接MQTT的客户端ID,一般以客户端唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存
//             MqttClient client = new MqttClient("tcp://192.168.2.201:1883", "test", new MemoryPersistence());
//             // MQTT的连接设置
//             MqttConnectOptions options = new MqttConnectOptions();
//             // 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
//             options.setCleanSession(true);
//             // 设置连接的用户名
//             options.setUserName("admin");
//             // 设置连接的密码
//             options.setPassword("public".toCharArray());
//             // 设置超时时间 单位为秒
//             options.setConnectionTimeout(10);
//             // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
//             options.setKeepAliveInterval(20);
//             // 设置回调函数
//             client.setCallback(new MqttCallback() {
//                 @Override
//                 public void connectionLost(Throwable cause) {
//                     System.out.println("connectionLost");
//                 }
//                 @Override
//                 public void messageArrived(String topic, MqttMessage message) throws Exception {
//                     System.out.println(message);
//                     // handleMessage(topic,message);
//                 }
//                 @Override
//                 public void deliveryComplete(IMqttDeliveryToken token) {
//                     System.out.println("deliveryComplete---------" + token.isComplete());
//                 }
//             });
//             client.connect(options);
//             //订阅消息
//             // System.out.println(topic);
//             // client.subscribe(topic);
//             return client;
//         } catch (Exception e) {
//             e.printStackTrace();
//             return null;
//         }
//     }
// }

4、设备模拟

MQTT工具下载

可能问题:工具无法连接MQTT broker,将1.工具安装至c盘,2.选仅为我安装

验证EMQ X服务器的搭建以及MQTTX的简单使用

5、二次开发EMQX的HTTP API

https://www.emqx.io/docs/zh/v4.4/advanced/http-api.html#接口安全

6、可能遇到的问题

spring-integration-mqtt 集成broker后启动项目频繁报 Lost connection 错误

Lost connection: 已断开连接; retrying...

解决方案原文:https://blog.csdn.net/lisheng19870305/article/details/119896635