MQTT协议之Mosquitto

MQTT协议简介

  • 消息队列遥测传输协议 Message Queuing Telemetry Transport. Based on TCP/IP, published by IBM in 1999.
  • MQTT协议可用极少的代码和有限的带宽,为连接远程设备提供实时可靠的消息服务
    作为一种低开销、低带宽占用的即时通讯协议,使其在物联网、小型设备、移动应用等方面有较广泛的应用。
  • MQTT协议是一种基于客户端-服务器的消息发布/订阅传输协议。它提供了一对多的消息分发机制,从而实现与应用程序的解耦
  • 轻量、简单、开放和易于实现
  • QoS(Quality of Service levels):
    • 0:会发生消息丢失或重复,消息发布依赖于底层TCP/IP网络。即:<=1
    • 1 承诺消息将至少传送一次给订阅者。
    • 2 我们保证消息仅传送到目的地一次。为此,带有唯一消息 ID 的消息会存储两次,首先来自发送者,然后是接收者。QoS 级别 2 在网络中具有最高的开销,因为在发送方和接收方之间需要两个流。
  • 应用架构
    MQTT Broker(经纪人、中间商)作为MQTT服务器,接收发布客户端发布的消息,发布到所有订阅相关消息的订阅客户端,客户端/服务端完全解耦。
    MQTT应用架构

Mosquitto简介

Mosquitto是由Eclipse Foundation开源的MQTT broker,同时也提供了基于C语言的MQTT clients(包括发布端和订阅端)Mosquitto官网🔗
Mosquitto支持Linux、Windows、MacOS、Arm-linux等系统。

一、基于源码安装

# 1、直接clone github中最新代码
git clone https://github.com/eclipse/mosquitto.git
# 2、基于cmake安装
mkdir build && cd build
cmake ..
make
# 2.1、安装编译失败(本人在MacOS Monterey中编译失败),修改下列文件即可。
CMakeLists.txt
 option(WITH_BUNDLED_DEPS "Build with bundled dependencies?" ON)
 option(WITH_TLS
-       "Include SSL/TLS support?" ON)
+       "Include SSL/TLS support?" OFF)
 option(WITH_TLS_PSK
-       "Include TLS-PSK support (requires WITH_TLS)?" ON)
+       "Include TLS-PSK support (requires WITH_TLS)?" OFF)
 option(WITH_EC
-       "Include Elliptic Curve support (requires WITH_TLS)?" ON)
+       "Include Elliptic Curve support (requires WITH_TLS)?" OFF)
注释以下两个文件:
plugins/message-timestamp/CMakeLists.txt
plugins/payload-modification/CMakeLists.txt

# 3、编译完成后生成的主要文件说明
CMakeCache.txt
...
src/mosquitto                   # mosquitto broker
client/mosquitto_pub      # mosquitto发布客户端
client/mosquitto_sub      # mosquitto订阅客户端

二、基本功能测试

1、基于公网的broker测试

Mosquitto为了方便用户测试,很贴心地提供了一个公网的MQTT broker,网站可直接访问:test.mosquitto.org。测试方法很简单,使用mosquitto_sub订阅一个主题,使用mosquitto_pub向该主题发送一个消息。

# 1、订阅者向test.mosquitto.org订阅主题/test/ttx
./mosquitto_sub -h test.mosquitto.org -t "/test/ttx"
# 2、发布者向test.mosquitto.org发布消息Hello Mosquitto至主题/test/ttx
./mosquitto_pub -h test.mosquitto.org -t "/test/ttx" -m "Hello Mosquitto"
#3、订阅者收到发布者的消息Hello Mosquitto

2、基于编译出的mosquitto broker测试

1、直接到编译出的src目录下运行mosquitto程序,该程序会启动一个mosquitto broker监听localhost的1883端口。

./mosquitto                           (master✱)
1662991355: mosquitto version 2.0.15 starting
1662991355: Using default config.
1662991355: Starting in local only mode. Connections will only be possible from clients running on this machine.
1662991355: Create a configuration file which defines a listener to allow remote access.
1662991355: For more details see https://mosquitto.org/documentation/authentication-methods/
1662991355: Opening ipv4 listen socket on port 1883.
1662991355: Opening ipv6 listen socket on port 1883.
1662991355: mosquitto version 2.0.15 running

2、跟基于公网的broker测试方法类似,只不过这次的broker是localhost

./mosquitto_sub -h localhost -t "/test/ttx"
./mosquitto_pub -h localhost -t "/test/ttx" -m "Hello Mosquitto"
# broker 监听到的信息
1662991508: New connection from 127.0.0.1:52196 on port 1883.
1662991508: New client connected from 127.0.0.1:52196 as auto-766C9637-15FF-A4CE-92DC-ABBA2FBF3E64 (p2, c1, k60).
1662991545: New connection from 127.0.0.1:52206 on port 1883.
1662991545: New client connected from 127.0.0.1:52206 as auto-C72C458F-B18E-7ED8-67A5-E23633F99CA9 (p2, c1, k60).
1662991545: Client auto-C72C458F-B18E-7ED8-67A5-E23633F99CA9 disconnected.

三、Mosquitto broker源码解读

TODO…

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值