mqtt协议实战指南

MQTT(消息队列遥测传输)是ISO 标准(ISO/IEC PRF
20922)下基于发布/订阅范式的消息协议。它工作在TCP/IP协议族上,是为硬件性能低下的远程设备以及网络状况糟糕的情况下而设计的发布/订阅型消息协议,为此,它需要一个消息中间件。

在这里插入图片描述
4.2 MQTT代理
1.4.2.1 mosquitto
1.4.2.2 EMQ
1.4.2.3 HiveMQ
1.4.2.4 ActiveMQ
1.4.2.5 mosca
EMQ
3.1 简介
EMQ 2.0 (Erlang/Enterprise/Elastic MQTT Broker) 是基于 Erlang/OTP 语言平台开发,支持大规模连接和分布式集群,发布订阅模式的开源 MQTT 消息服务器。

3.1.1官网
http://www.emqtt.com/docs/v2/index.html

3.1.2优点
3.3.1 商用比较普及,有完备的运营团队支撑
3.3.2 可视化的管理后台
开放18083端口访问管理后台

3.3.3 能抗高并发
官方的回复是8核心32G的配置能够承载160W台设备的链接

  1. Mosquito
    2.1 简介
    一款实现了消息推送协议 MQTT v3.1 的开源消息代理软件,提供轻量级的,支持可发布/可订阅的的消息推送模式,使设备对设备之间的短消息通信变得简单,比如现在应用广泛的低功耗传感器,手机、嵌入式计算机、微型控制器等移动设备。一个典型的应用案例就是 Andy Stanford-ClarkMosquitto(MQTT协议创始人之一)在家中实现的远程监控和自动化。并在 OggCamp 的演讲上,对MQTT协议进行详细阐述。

1.2 官网
https://mosquitto.org/

1.3 优点
2.3.1使用简单

2.3.2 网络资料丰富

1.4 缺点
2.4.1 没有可视化管理后台

2.4.2 商用实例不多

2.5 安装及踩坑
1.源码包下载:http://mosquitto.org/files/source/

或者wget http://mosquitto.org/files/source/mosquitto-1.4.9.tar.gz

版本:mosquitto-1.4.tar.gz

解压:tar -zxvf mosquitto-1.4.tar.gz

进入目录:cd mosquitto-1.4

2.编译安装

打开配置文件,去掉暂且不需要的功能:

vi config.mk

如:WITH_TLS,WITH_TLS_PSK, WITH_SRV, WITH_WEBSOCKETS, WITH_SOCKS, WITH_UUID等

保存退出:wq

安装mosquitto

make

make install

踩过的坑:

a】编译找不到openssl/ssl.h

安装openssl sudo apt-get install libssl-dev

【b】编译过程找不到ares.h

sudo apt-get install libc-ares-dev

【c】编译过程找不到uuid/uuid.h

sudo apt-get install uuid-dev

【d】使用过程中找不到libmosquitto.so.1

error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory

【解决方法】——修改libmosquitto.so位置

创建链接

sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1

更新动态链接库

sudo ldconfig

【e】make: g++:命令未找到

【解决方法】

安装g++编译器

sudo apt-get install g++

启动 mosquitto broker

mosquitto -c /etc/mosquitto/mosquitto.conf.example &

-c : specify the broker config file.

-d : put the broker into the background after starting.

-h : display this help.

-p : start the broker listening on the specified port.

  Not recommended in conjunction with the -c option.

-v : verbose mode - enable all logging types. This overrides

  any logging options given in the config file.

订阅消息:

./mosquitto_sub -h 127.0.0.1 -p 1883 -t “/sports/wordcup”

发布消息:

./mosquitto_pub -h 127.0.0.1 -p 1883 -t "/sports/wordcup " -m “this is carter hello”

或者

./mosquitto_sub -h 10.129.4.12 -p 1883 -t “/sports/wordcup”

./mosquitto_pub -h 10.129.4.12 -p 1883 -t “/sports/wordcup” -m “this is carter hello 666”

外网地址不行,所以我在本机用paho代码一直报timeOut异常,原因是服务器防火墙未放开端口

【解决办法】

放开防火墙端口:firewall-cmd --add-port=1883/tcp –permanent

重启防火墙:systemctl restart firewalld

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值