b1gc8v 发表于 2024-10-3 12:57:47

MQTT 与 EMQ快速入门


    <h1 style="color: black; text-align: left; margin-bottom: 10px;">MQTT 与 EMQ</h1>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1. 物联网<span style="color: black;">信息</span>协议MQTT</span></h2>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">1.1. MQTT简介</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MQTT(Message Queuing Telemetry Transport,<span style="color: black;">信息</span>队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的"轻量级"通讯协议。</p><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9gEjL0ibhgSpzibplf7jw54mcIBgpPBM6qXc9QjeXyRP65T1qVYtyVsOQ/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">客户端服务端一个<span style="color: black;">运用</span>MQTT协议的应用程序<span style="color: black;">或</span>设备,它总是<span style="color: black;">创立</span>到服务器的网络连接。客户端<span style="color: black;">能够</span>:<span style="color: black;">亦</span><span style="color: black;">叫作</span>为"<span style="color: black;">信息</span>代理"(Broker),<span style="color: black;">能够</span>是一个应用程序或一台设备。它是<span style="color: black;">位置于</span><span style="color: black;">信息</span>发布者和订阅者之间,它<span style="color: black;">能够</span>:- (1)发布其他客户端可能会订阅的信息;- (1)接受来自客户的网络连接;- (2)订阅其它客户端发布的<span style="color: black;">信息</span>;- (2)接受客户发布的应用信息;- (3)退订或删除应用程序的<span style="color: black;">信息</span>;- (3)处理来自客户端的订阅和退订请求;- (4)断开与服务器连接。- (4)向订阅的客户转发应用程序<span style="color: black;">信息</span>。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">设计原则:</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">①精简,不添加可有可无的功能;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">②发布/订阅(Pub/Sub)模式,方便<span style="color: black;">信息</span>在传感器之间传递,解耦Client/Server模式,带来的好处在于不必预先<span style="color: black;">晓得</span>对方的存在(ip/port),不必<span style="color: black;">同期</span>运行;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">③<span style="color: black;">准许</span>用户动态创建主题(不需要预先创建主题),零运维成本;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">④把传输量降到最低以<span style="color: black;">加强</span>传输效率;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">⑤把低带宽、高延迟、不稳定的网络等<span style="color: black;">原因</span><span style="color: black;">思虑</span>在内;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">⑥支持连续的会话保持和<span style="color: black;">掌控</span>(心跳);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">⑦理解客户端计算能力可能很低;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">⑧<span style="color: black;">供给</span>服务质量( quality of service level:QoS)管理</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">⑨不强求传输数据的类型与格式,保持灵活性(指的是应用层业务数据)。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">MQTT应用<span style="color: black;">行业</span>:</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">①物联网M2M通信,物联网大数据采集</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">②Android<span style="color: black;">信息</span>推送,WEB<span style="color: black;">信息</span>推送</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">③移动即时<span style="color: black;">信息</span>,例如Facebook Messenger</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">②智能硬件、智能家居、智能电器</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">⑤车联网通信,电动车站桩采集</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">⑥智慧城市、远程医疗、远程教育</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">⑦电力、石油与能源等行业市场</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">1.2 MQTT协议<span style="color: black;">关联</span>概念</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">MQTT协议<span style="color: black;">形成</span></strong></p><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9J6Fd9a3YVvs1QJXoNfDQqIqY5uUp3M0toJiazDJZHeO0svqTiccekVqg/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">MQTT协议中的<span style="color: black;">办法</span></strong></p>- (1)<strong style="color: blue;">CONNECT</strong>:客户端连接到服务器- (9)SUBACK:订阅确认- (2)CONNACK:连接确认- (10)UNSUBSCRIBE:取消订阅- (3)<strong style="color: blue;">PUBLISH</strong>:发布<span style="color: black;">信息</span>- (11)UNSUBACK:取消订阅确认- (4)PUBACK:发布确认- (12)PINGREQ:客户端发送心跳- (5)PUBREC:发布的<span style="color: black;">信息</span>已接收- (13)PINGRESP:服务端心跳响应- (6)PUBREL:发布的<span style="color: black;">信息</span>已释放- (14)<strong style="color: blue;">DISCONNECT</strong>:断开连接- (7)PUBCOMP:发布完成- (15)AUTH:认证- (8)<strong style="color: blue;">SUBSCRIBE</strong>:订阅请求<h3 style="color: black; text-align: left; margin-bottom: 10px;">1.3. <span style="color: black;">信息</span>服务质量QOS</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MQTT 协议中规定了<span style="color: black;">信息</span>服务质量(Quality of Service),它<span style="color: black;">保准</span>了在<span style="color: black;">区别</span>的网络环境下<span style="color: black;">信息</span>传递的<span style="color: black;">靠谱</span>性,QoS 的设计是 MQTT 协议里的重点。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MQTT 设计了 3 个 QoS 等级。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- QoS 0:<span style="color: black;">信息</span>最多传递一次。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- QoS 1:<span style="color: black;">信息</span>传递<span style="color: black;">最少</span> 1 次。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- QoS 2:<span style="color: black;">信息</span>仅传送一次。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">QoS0:"至多一次",<span style="color: black;">信息</span>发布完全依赖底层TCP/IP网络。会<span style="color: black;">出现</span><span style="color: black;">信息</span>丢失。一个<span style="color: black;">信息</span>不会被接收端应答,<span style="color: black;">亦</span>不会被发送者存储并再发送。这个<span style="color: black;">亦</span>被叫做“即发即弃”</p><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9O1GIkiavjose40CrwEsj1zoTjYCEob6NllLuRfX7kx2Pe20KArk3DKg/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">QoS1:"<span style="color: black;">最少</span>一次",<span style="color: black;">保证</span><span style="color: black;">信息</span>到达,但<span style="color: black;">信息</span>重复可能会<span style="color: black;">出现</span>。发送者将会存储发送的信息直到发送者收到一次来自接收者的PUBACK格式的应答。</p><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9Kpia3QlicrrpmibkB8L1ORjwXT6ElKZRPRtEbXiaJsiavM6GKOMkyicicRtaw/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">QoS2:"<span style="color: black;">仅有</span>一次",<span style="color: black;">保证</span><span style="color: black;">信息</span>到达一次。<span style="color: black;">倘若</span>接收端接收到了一个QoS为2的PUBLISH<span style="color: black;">信息</span>,它将相应地处理PUBLISH<span style="color: black;">信息</span>,并<span style="color: black;">经过</span>PUBREC<span style="color: black;">信息</span>向发送方确认。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PUBLISH:发布<span style="color: black;">信息</span>
      PUBREC:发布收到
      PUBREL:发布释放
      PUBCOMP:发布完成</p><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9zDx679BzJTDz3WZtKNZJoibLpyCM60y0bxFnVeSe6zfCkEnyWmPq9sg/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">发送订阅<span style="color: black;">区别</span>QOS<span style="color: black;">状况</span>下是<span style="color: black;">怎样</span>生效的</p><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9NIXCfNQia1676O9WOdQU91T3WvBAFwYyb1iaGa10pTubRBnYVKrSkia1w/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">1.4. topic通配符匹配规则</h3><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK95Q5QEt5zKxHwXpPibiaWF8D5y5UzuBl9dvEsbZxTxR0HwreXIHGWXDmw/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9Qp2kcrzicLjwygUU4nCVrb5R1DtBe3ibb3SAodvxfXibD5Q4zmJXnyhRw/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9rDElGZc5UKjWsJ7goHW6RSO4UwZcIQYibcHaNEQDhic2cffSj2UR5dxA/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9EAbdJt6rHHKUy8rVvl3Nhiat04VytHxJW64ngTy1gWcVCM5CRc2EdiaA/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2. 物联网级<span style="color: black;">信息</span>中间件EMQ</span></h2>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">2.1. EMQX 简介</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EMQ X Broker 是基于高并发的 Erlang/OTP 语言平台<span style="color: black;">研发</span>,支持百万级连接和分布式集群架构,发布订阅模式的开源 MQTT <span style="color: black;">信息</span>服务器。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EMQ官网:https://www.emqx.cn/</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为何</span><span style="color: black;">选取</span>EMQ X ?从支持 MQTT5.0、稳定性、扩展性、集群能力等方面<span style="color: black;">思虑</span>,EMQX 的表现应该是最好的。</p><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9UyHZFoxdCMEtLePTDmBtlzAP3ZlyicdzPriaiaAUEW613Telib5FOrOowQ/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">EMQX的特点</strong></p>EMQ X <span style="color: black;">日前</span>为开源社区中最流行的 MQTT <span style="color: black;">信息</span>中间件;EMQ X 是开源社区中<span style="color: black;">第1</span>个支持 5.0协议规范的<span style="color: black;">信息</span>服务器,并且完全兼容 MQTT V3.1 和 V3.1.1 协议。除了 MQTT 协议之外,EMQ X 还支持MQTT-SN、CoAP、 LwM2M、LoRaWAN 和 WebSocket 等物联网协议单机支持百万连接,集群支持千万级连接;毫秒级<span style="color: black;">信息</span>转发。易于安装和<span style="color: black;">运用</span>;中国本地的技术支持服务;扩展模块和插件,EMQ X <span style="color: black;">供给</span>了灵活的扩展机制,支持企业的<span style="color: black;">有些</span>定制场景;桥接共享订阅<h3 style="color: black; text-align: left; margin-bottom: 10px;">2.2. 环境搭建与配置</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Docker 运行与安装</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">拉取emqx镜像</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">#&nbsp;docker&nbsp;pull&nbsp;emqx/emqx:v4.1.0</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">创建emqx容器</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">#&nbsp;docker&nbsp;run&nbsp;-tid&nbsp;--name&nbsp;emqx&nbsp;-p&nbsp;1883:1883&nbsp;-p&nbsp;8083:8083&nbsp;-p&nbsp;8081:8081&nbsp;-p&nbsp;8883:8883&nbsp;-p&nbsp;8084:8084&nbsp;-p&nbsp;18083:18083&nbsp;&nbsp;emqx/emqx:v4.1.0</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">EMQ X 目录结构</strong></p><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9cMINm960e3xXAkFXhMWERmXujouNCBVcibRmCXjWXUYTC4ZDMqNiad5w/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">2.3. EMQDashboard</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EMQ X <span style="color: black;">供给</span>了 Dashboard 以方便用户管理设备与监控<span style="color: black;">关联</span>指标。<span style="color: black;">经过</span> Dashboard<span style="color: black;">能够</span>查看服务器基本信息、负载<span style="color: black;">状况</span>和统计数据,<span style="color: black;">能够</span>查看某个客户端的连接状态等信息<span style="color: black;">乃至</span>断开其连接,<span style="color: black;">亦</span><span style="color: black;">能够</span>动态加载和卸载指定插件。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">拜访</span><span style="color: black;">位置</span> http://192.168.150.102:18083 来查看Dashboard,**默认用户名是 admin,<span style="color: black;">秘码</span>是 public*</p><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9Z2VyTTTlnQmibmITWmicuIzM6tdmFphJXw9nR02gxVPESYEUWwC4zbZg/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9Gwk7TuR4PsEMDn9GJicGJFrUMT0yN2AV35Lnib8HzAIVjKvjtk2MVZUQ/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9zthtKm5H1BMjsTe3Fw6LicMNWElqoWicpcHpfVr1ubicpPn3ebcChBticg/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">2.4. 客户端调试工具MQTTX</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MQTT X 是 EMQ 开源的一款优雅的跨平台 MQTT 5.0 桌面客户端,它支持 macOS, Linux, Windows。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MQTT X 的 UI 采用了聊天界面形式,简化了页面操作<span style="color: black;">规律</span>,用户<span style="color: black;">能够</span>快速创建连接,<span style="color: black;">准许</span><span style="color: black;">保留</span>多个客户端,方便用户快速测试 MQTT/MQTTS 连接,及 MQTT <span style="color: black;">信息</span>的订阅和发布。</p><img src="https://mmbiz.qpic.cn/mmbiz_gif/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK95ToUCRRekUeqERajYand8doxNuI0rQZ6qhdqMA1TYaAibxuWXRmibY0Q/640?wx_fmt=gif&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1" style="width: 50%; margin-bottom: 20px;">
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">2.5. 延迟<span style="color: black;">信息</span></h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">EMQ X 的延迟发布功能<span style="color: black;">能够</span>实现<span style="color: black;">根据</span>用户配置的时间间隔延迟发布 PUBLISH 报文的功能。模块开启emqx_mod_delayed</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">延迟发布主题的<span style="color: black;">详细</span>格式如下:</p><span style="color: black;">$delayed</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/{DelayInterval}/{TopicName}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- <span style="color: black;"><span style="color: black;"><span style="color: black;">运用</span></span></span>delayed <span style="color: black;">做为</span>主题前缀的<span style="color: black;">信息</span>都将被视为需要延迟发布的<span style="color: black;">信息</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- {DelayInterval}: 指定该 MQTT <span style="color: black;">信息</span>延迟发布的时间间隔,单位是秒,<span style="color: black;">准许</span>的最大间隔是 4294967 秒。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- {TopicName}: MQTT <span style="color: black;">信息</span>的主题名<span style="color: black;">叫作</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.在Websocket上订阅主题:t2/a</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.在Websocket上发布<span style="color: black;">信息</span>主题:topic: &nbsp;$delayed/10/t2/a</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">观察运行效果</p><img src="https://mmbiz.qpic.cn/mmbiz_png/fx1lx9ichONbSBvAuI72ZHNdlqPHpzCK9y6oByJmRu0ZsUDAC8EIFyNUQ22HDYxMBWolgTPXN43ibXQB617Eyjicg/640?wx_fmt=png&amp;from=appmsg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;">
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">2.6. 共享订阅</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">不带群组的共享订阅</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">格式:</p><span style="color: black;">$queue</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/{TopicName}</p><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;">
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">带群组的共享订阅</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">格式:</p><span style="color: black;">$share</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//{TopicName}
    </p><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;">
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3.Eclipse Paho</span></h2>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">3.1. Eclipse Paho是什么</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Eclipse paho 是EMQx官方<span style="color: black;">举荐</span>的实现了mqtt协议java客户端。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其关系类似于Mysql于JDBC,<span style="color: black;">咱们</span>的项目代码要连接数据库需要用到JDBC,而<span style="color: black;">咱们</span>的项目需要连接EMQX需要用到Eclipse Paho &nbsp;,并且它<span style="color: black;">供给</span>了<span style="color: black;">基本</span>的<span style="color: black;">信息</span>收发。</p>
    <h3 style="color: black; text-align: left; margin-bottom: 10px;">3.2. Eclipse Paho技术调研</h3>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">集成Eclipse paho</strong></p><span style="color: black;">&lt;<span style="color: black;">dependency</span>&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">&lt;<span style="color: black;">groupId</span>&gt;</span>org.eclipse.paho<span style="color: black;">&lt;/<span style="color: black;">groupId</span>&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">&lt;<span style="color: black;">artifactId</span>&gt;</span>org.eclipse.paho.client.mqttv3<span style="color: black;">&lt;/<span style="color: black;">artifactId</span>&gt;</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">&lt;<span style="color: black;">version</span>&gt;</span>1.2.5<span style="color: black;">&lt;/<span style="color: black;">version</span>&gt;</span><span style="color: black;">&lt;/<span style="color: black;">dependency</span>&gt;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">发布<span style="color: black;">信息</span>到EMQ</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1)搭建基本的springBoot程序。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(2)编写controller,新增发布<span style="color: black;">信息</span><span style="color: black;">办法</span>。</p><span style="color: black;">@GetMapping</span>(<span style="color: black;">"/publish"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p><span style="color: black;"><span style="color: black;">public</span>&nbsp;<span style="color: black;">void</span>&nbsp;<span style="color: black;">publish</span><span style="color: black;">()</span>&nbsp;<span style="color: black;">throws</span>&nbsp;MqttException&nbsp;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;MqttClientPersistence&nbsp;persistence&nbsp;=&nbsp;<span style="color: black;">new</span>&nbsp;MemoryPersistence();;<span style="color: black;">//内存持久化</span>MqttClient&nbsp;client&nbsp;=<span style="color: black;">new</span>&nbsp;MqttClient(<span style="color: black;">"tcp://192.168.200.128:1883"</span>,&nbsp;<span style="color: black;">"abc"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;persistence);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//连接选项中定义用户名<span style="color: black;">秘码</span>和其它配置</span>&nbsp;&nbsp;&nbsp;&nbsp;MqttConnectOptions&nbsp;options&nbsp;=&nbsp;<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;MqttConnectOptions();</p>options.setCleanSession(<span style="color: black;">true</span>);<span style="color: black;">//参数为true<span style="color: black;">暗示</span>清除缓存,<span style="color: black;">亦</span><span style="color: black;">便是</span>非持久化订阅者,这个时候只要参数设为true,<span style="color: black;">必定</span>是非持久化订阅者。而参数设为false时,<span style="color: black;">暗示</span>服务器<span style="color: black;">保存</span>客户端的连接记录</span>&nbsp;&nbsp;&nbsp;&nbsp;options.setAutomaticReconnect(<span style="color: black;">true</span>);<span style="color: black;">//<span style="color: black;">是不是</span>自动重连</span>options.setConnectionTimeout(<span style="color: black;">30</span>);<span style="color: black;">//连接超时时间&nbsp;&nbsp;秒</span>&nbsp;&nbsp;&nbsp;&nbsp;options.setKeepAliveInterval(<span style="color: black;">10</span>);<span style="color: black;">//连接保持<span style="color: black;">检测</span>周期&nbsp;&nbsp;秒</span>&nbsp;&nbsp;&nbsp;&nbsp;options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);&nbsp;<span style="color: black;">//版本</span>client.connect(options);<span style="color: black;">//连接</span>&nbsp;&nbsp;&nbsp;&nbsp;client.publish(<span style="color: black;">"topic"</span>,&nbsp;<span style="color: black;">"发送内容"</span>.getBytes(),&nbsp;<span style="color: black;">2</span>,&nbsp;<span style="color: black;">false</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">订阅<span style="color: black;">信息</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在controller新增<span style="color: black;">办法</span>,订阅<span style="color: black;">信息</span></p><span style="color: black;">@GetMapping</span>(<span style="color: black;">"/subscribe"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p><span style="color: black;"><span style="color: black;">public</span>&nbsp;<span style="color: black;">void</span>&nbsp;<span style="color: black;">subscribe</span><span style="color: black;">()</span>&nbsp;<span style="color: black;">throws</span>MqttException</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;MqttClientPersistence&nbsp;persistence&nbsp;=&nbsp;<span style="color: black;">new</span>&nbsp;MemoryPersistence();;<span style="color: black;">//内存持久化</span>&nbsp;&nbsp;&nbsp;&nbsp;MqttClient&nbsp;client&nbsp;=&nbsp;<span style="color: black;">new</span>&nbsp;MqttClient(<span style="color: black;">"tcp://192.168.200.128:1883"</span>,&nbsp;<span style="color: black;">"abc"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,&nbsp;persistence);</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//连接选项中定义用户名<span style="color: black;">秘码</span>和其它配置</span>&nbsp;&nbsp;&nbsp;&nbsp;MqttConnectOptions&nbsp;options&nbsp;=&nbsp;<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;MqttConnectOptions();</p>&nbsp;&nbsp;&nbsp;&nbsp;options.setCleanSession(<span style="color: black;">true</span>);<span style="color: black;">//参数为true<span style="color: black;">暗示</span>清除缓存,<span style="color: black;">亦</span><span style="color: black;">便是</span>非持久化订阅者,这个时候只要参数设为true,<span style="color: black;">必定</span>是非持久化订阅者。而参数设为false时,<span style="color: black;">暗示</span>服务器<span style="color: black;">保存</span>客户端的连接记录</span>&nbsp;&nbsp;&nbsp;&nbsp;options.setAutomaticReconnect(<span style="color: black;">true</span>);<span style="color: black;">//<span style="color: black;">是不是</span>自动重连</span>&nbsp;&nbsp;&nbsp;&nbsp;options.setConnectionTimeout(<span style="color: black;">30</span>);<span style="color: black;">//连接超时时间&nbsp;&nbsp;秒</span>options.setKeepAliveInterval(<span style="color: black;">10</span>);<span style="color: black;">//连接保持<span style="color: black;">检测</span>周期&nbsp;&nbsp;秒</span>&nbsp;&nbsp;&nbsp;&nbsp;options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);&nbsp;<span style="color: black;">//版本</span>&nbsp;&nbsp;&nbsp;&nbsp;client.setCallback(<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;MqttCallbackExtended()&nbsp;{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">@Override</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;"><span style="color: black;">public</span>&nbsp;<span style="color: black;">void</span>&nbsp;<span style="color: black;">connectionLost</span><span style="color: black;">(Throwable&nbsp;throwable)</span>&nbsp;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span style="color: black;">"连接丢失!"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">@Override</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;"><span style="color: black;">public</span>&nbsp;<span style="color: black;">void</span>&nbsp;<span style="color: black;">messageArrived</span><span style="color: black;">(String&nbsp;s,&nbsp;MqttMessage&nbsp;mqttMessage)</span>&nbsp;<span style="color: black;">throws</span>Exception</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&nbsp;<span style="color: black;">"接收到<span style="color: black;">信息</span>&nbsp;&nbsp;topic:"</span>&nbsp;+s+<span style="color: black;">"&nbsp;&nbsp;id:"</span>+mqttMessage.getId()&nbsp;+<span style="color: black;">"&nbsp;message:"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">+&nbsp;mqttMessage.toString());</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">@Override</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;"><span style="color: black;">public</span>&nbsp;<span style="color: black;">void</span>&nbsp;<span style="color: black;">deliveryComplete</span><span style="color: black;">(IMqttDeliveryToken&nbsp;iMqttDeliveryToken)</span>&nbsp;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">@Override</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;"><span style="color: black;">public</span>&nbsp;<span style="color: black;">void</span>&nbsp;<span style="color: black;">connectComplete</span><span style="color: black;">(<span style="color: black;">boolean</span>&nbsp;b,&nbsp;String&nbsp;s)</span>&nbsp;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<span style="color: black;">"连接成功!"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;});</p>&nbsp;&nbsp;&nbsp;&nbsp;client.connect(options);<span style="color: black;">//连接</span>&nbsp;&nbsp;&nbsp;&nbsp;client.subscribe(<span style="color: black;">"test"</span>);&nbsp;&nbsp;<span style="color: black;">//订阅主题</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>您觉得本文不错,</span><span style="color: black;">欢迎关注,点赞,<span style="color: black;">保藏</span>支持</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,您的关注是我<span style="color: black;">保持</span>的动力!</p>

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">原创<span style="color: black;">很难</span>,转载请注明出处,感谢支持!<span style="color: black;">倘若</span>本文对您有用,欢迎转发分享!</span></p>




nqkk58 发表于 2024-11-13 01:52:10

顶楼主,说得太好了!
页: [1]
查看完整版本: MQTT 与 EMQ快速入门