MQTT协议是广泛应用的物联网协议,使用测试MQTT协议需要MQTT的代理。有两种方法使用MQTT服务,一是租用现成的MQTT服务器,如阿里云,百度云,华为云等公用的云平台提供的MQTT服务,使用公用的MQTT服务器的好处是省事,但如果仅仅用于测试学习还需要注册帐号,灵活性差些,有的平台还需要付费。另一方法是自己使用开源的MQTT组件来搭建mqtt服务器。
MQTT服务器开源的程序非常多,例如:Mosquitto、apache的ActiveMQ、emtqqd、HiveMQ、Emitter、Mosquitto、Moquette等等,如下图:
本文这里重点介绍的是用轻量级的mosquitto开源项目来搭建一个属于自己的MQTT服务器。
一、用 Mosquitto搭建一个属于自己的MQTT服务器 教程
Mosquitto轻巧,适合在从低功耗单板计算机到完整服务器的所有设备上使用。
Mosquitto项目还提供了一个用于实现MQTT客户端的C库,以及非常流行的mosquitto_pub和mosquitto_sub命令行MQTT客户端。
1、Linux下环境准备:
[root@localhost ~]# yum install libc-ares-dev
[root@localhost ~]# yum install libuuid-devel
[root@localhost ~]# yum install gcc
[root@localhost ~]# yum install gcc-c++
2、Mosquitto下载、安装:
# 下载源代码包
[root@localhost ~]# wget https://mosquitto.org/files/source/mosquitto-2.0.8.tar.gz
# 解压
[root@localhost ~]# tar zxfv mosquitto-2.0.8.tar.gz
# 进入目录
[root@localhost ~]# cd mosquitto-2.0.8
# 编译
[root@localhost ~]# make
# 安装
[root@localhost ~]# make instal
3、启动 mosquitto
这时候mosquitto就会以默认的参数启动。
#打开mosquitto服务端
[root@localhost ~]# mosquitto -v
4、发布与订阅
发布使用mosquitto_pub命令,订阅使用mosquitto_sub命令。常用参数介绍:
参数 | 描述 |
---|---|
-h | 服务器主机,默认localhost |
-t | 指定主题 |
-u | 用户名 |
-P | 密码 |
-i | 客户端id,唯一 |
-m | 发布的消息内容 |
#订阅主题 testtopic
[root@localhost ~]# mosquitto_sub -h 127.0.0.1 -p 1883 -v -t testtopic
#发布消息helloword到主题testtopic
[root@localhost ~]# mosquitto_pub -h 127.0.0.1 -p 1883 -t testtopic -m helloworld
这时候我们的MQTT服务器就算是搭建好了。但还不是很完美,因为Mosquitto服务器大部分都是采用默认的是允许匿名用户登录模式,还有就是权限什么的?因此,需要继续往下看:
二、Mosquitto 配置 详细教程
1、主配置文件:mosquitto.conf
Mosquitto服务器的配置文件路径一般为:/etc/mosquitto/mosquitto.conf,内容一般如下:
pid_file /var/run/mosquitto.pid
# 消息持久存储
persistence true
persistence_location /var/lib/mosquitto/
# 日志文件
log_dest file /var/log/mosquitto/mosquitto.log
# 其他配置
include_dir /etc/mosquitto/conf.d
# 禁止匿名访问
allow_anonymous false
# 认证配置
password_file /etc/mosquitto/pwfile
# 权限配置
acl_file /etc/mosquitto/aclfile
2、认证配置文件:pwfile
如果没有则创建文件,用命令:
touch /etc/mosquitto/pwfile
添加新用户:
服务开启后,输入如下命令,根据提示输入两遍密码:
mosquitto_passwd /etc/mosquitto/pwfile 用户名
3、权限配置文件:aclfile
打开文件
vim /etc/mosquitto/aclfile
编辑内容
# 李雷只能发布以test为前缀的主题,订阅以$SYS开头的主题即系统主题
user lilei
topic write test/#
topic read $SYS/#
# 韩梅梅只能订阅以test为前缀的主题
user hanmeimei
topic read test/#
4、启动
-c:指定特定配置文件启动
-d:后台运行
mosquitto -c /etc/mosquitto/mosquitto.conf -d
注意事项:
以下这些情况可能你会遇到,已经有人踩过的坑,大家就没必要再次去踩了。也给出了相应的解决方法。
1、编译找不到openssl/ssl.h 解决方法:
安装opensslsudo,用命令:apt-get install libssl-dev
2、编译过程找不到ares.h fatal error: ares.h: No such file or directory 解决方法:
命令:sudo apt-get install libc-ares-dev
或者 打开config.mk ,把WITH_SRV:=yes 改成WITH_SRV:=no
3、编译过程找不到uuid/uuid.h 解决方法:
命令:
apt-get install uuid-dev
yum install libuuid-devel
4、使用过程中找不到 libmosquitto.so.1error 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
总结:
MQTT服务器端:我们用的是mosquitto,搭建自己的MQTT服务器已经成功了。
MQTT客户端:以PHP为例子,我们可以用 https://github.com/bluerhinos/phpMQTT,这样就可以通信了。或者参考:PHP+CloudMQTT详解 phpMQTT使用教程