分布式物联网平台EMQX-MQTT

官方文档地址

1、安装(不推荐docker部署)

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、简易版客户端

参考:https://www.cnblogs.com/zhuangyao/p/12320500.html

// 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

7、EMQX主机部署(推荐)

参考文档:

https://www.emqx.io/docs/zh/v5.0/deploy/install.html#rpm-deb%E5%8C%85%E5%AE%89%E8%A3%85-linux

https://gitee.com/emqx/emqx?_from=gitee_search#https://gitee.com/link?target=https%3A%2F%2Fwww.emqx.io%2Fzh%2Fdownloads

下载二进制包:

https://www.emqx.io/zh/downloads?os=CentOS
wget https://www.emqx.com/zh/downloads/broker/5.0.14/emqx-5.0.14-el7-amd64.tar.gz

解压:

tar zxvf emqx-5.0.14-el7-amd64.tar.gz

帮助:emqx -h
启动:emqx start
停止:emqx stop

dashboard端口:18083
默认令牌:admin/public
修改后令牌:admin/Jtzl@root@2022!@#

系统参数调优:

https://www.emqx.io/docs/zh/v5.0/deploy/tune.html#linux-操作系统参数

配置修改:

https://www.emqx.io/docs/zh/v5.0/admin/cfg.html
【emqx.conf】

## NOTE:
## Configs in this file might be overridden by:
## 1. Environment variables which start with 'EMQX_' prefix
## 2. File $EMQX_NODE__DATA_DIR/configs/cluster-override.conf
## 3. File $EMQX_NODE__DATA_DIR/configs/local-override.conf
##
## The *-override.conf files are overwritten at runtime when changes
## are made from EMQX dashboard UI, management HTTP API, or CLI.
## All configuration details can be found in emqx.conf.example

node {
  name = "jtemqxpro@0.0.0.0"
  cookie = "jt_emqx_pro131"
  data_dir = "data"
}

log {
  file_handlers.default {
    level = warning
    file = "log/emqx.log"
  }
}

cluster {
  name = emqxcl
  discovery_strategy = manual
}

listeners.tcp.default {
  bind = "0.0.0.0:1883"
  max_connections = 1024000
  acceptors = 128 
}

mqtt {
  max_mqueue_len = 30000
}

listeners.ws.default {
  bind = "0.0.0.0:8083"
  max_connections = 1024000
  websocket.mqtt_path = "/mqtt"
}

dashboard {
    listeners.http {
        bind = 18083
    }
    default_username = "admin"
    default_password = "public"
}

authorization {
  deny_action = ignore
  no_match = allow
  sources =  [
    {
      type = file
      enable = true
      ## This file is immutable to EMQX.
      ## Once new rules are created from dashboard UI or HTTP API,
      ## the file 'data/authz/acl.conf' is used instead of this one
      path = "etc/acl.conf"
    }
  ]
}

##listeners.ssl.default {
##  bind = "0.0.0.0:8883"
##  max_connections = 512000
##  ssl_options {
##    keyfile = "etc/certs/key.pem"
##    certfile = "etc/certs/cert.pem"
##    cacertfile = "etc/certs/cacert.pem"
##  }
##}

##listeners.wss.default {
##  bind = "0.0.0.0:8084"
##  max_connections = 512000
##  websocket.mqtt_path = "/mqtt"
##  ssl_options {
##    keyfile = "etc/certs/key.pem"
##    certfile = "etc/certs/cert.pem"
##    cacertfile = "etc/certs/cacert.pem"
##  }
##}

## listeners.quic.default {
##  enabled = true
##  bind = "0.0.0.0:14567"
##  max_connections = 1024000
##  keyfile = "etc/certs/key.pem"
##  certfile = "etc/certs/cert.pem"
##}
posted @ 2022-05-19 17:10  黄河大道东  阅读(380)  评论(0编辑  收藏  举报