虽然有免费的公开Broker服务可以使用,当初步理解了MQTT协议之后,就有必要自己动手搭建一台Broker了。这样才能更深入的学习MQTT的更多内容。开源的Broker有很多,目前主流的Broker有以下3个:
[list]
[*]Mosquitto:https://mosquitto.org/
[*]VerneMQ:https://vernemq.com/
[*]EMQTT:http://emqtt.io/[/list]
其他更多的可以参考:https://github.com/mqtt/mqtt.github.io/wiki/servers
Mosquitto 是当前用户最多的一款产品,用C开发的。其他两个(VerneMQ、EMQTT)是用Erlang语言开发的。
[color=blue][b]CentOS7 里安装Mosquitto Broker[/b][/color]
[b](1)安装mosquitto[/b]
启动服务
测试确认(开启2个Terminal分别执行以下2个命令)
执行完mosquitto_pub命令后,在mosquitto_sub窗口能看到"hello world"消息的显示。
[b](2)设置密码[/b]
[b]密码确认[/b]
执行完mosquitto_pub命令后,在mosquitto_sub窗口能看到"hello world2"消息的显示。
配置文件mosquitto.conf的举例内容可以参考官网说明:
https://mosquitto.org/man/mosquitto-conf-5.html
[b](3)客户端工具确认[/b]
使用Eclipse Paho MQTT Utility连接搭建的服务器测试确认。
[b]设置用户名和密码[/b]
[img]http://dl2.iteye.com/upload/attachment/0128/3850/f1ef3801-9435-389c-884f-c51dbd09867a.png[/img]
[b]连接、订阅、发布确认[/b]
[img]http://dl2.iteye.com/upload/attachment/0128/3852/afdf02a8-f282-35f5-93b6-d0d504af6f2e.png[/img]
[color=blue][b]Windows 7里安装Mosquitto Broker[/b][/color]
(1) 从 https://mosquitto.org/download/ 下载安装文件mosquitto-1.4.14-install-win32.exe
(2) 执行安装文件会提示下载OpenSSL 和 pThreads。
[img]http://dl2.iteye.com/upload/attachment/0128/4071/d9550b49-c1f7-34b8-bef6-2d4d68c0b824.png[/img]
(3) 继续执行完安装后,会提示SSLEAY21.dll找不到。
(4) 下载 Win32OpenSSL_Light-1_0_2n.exe 并安装。(注意选择把OpenSSL DLLS复制到 OpenSSL的安装目录下)
OpenSSL: http://slproweb.com/products/Win32OpenSSL.html
[img]http://dl2.iteye.com/upload/attachment/0128/4073/9fffa942-2a56-37c4-aa6e-b32e7bb735ee.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0128/4075/c869eb24-61de-312f-8768-4fcb4d8ef9db.png[/img]
(5) 下载 pthreadVC2.dll
pThreads: ftp://sources.redhat.com/pub/pthreads-win32/dll-latest/dll/x86/
[img]http://dl2.iteye.com/upload/attachment/0128/4077/0a40759f-65d4-3718-95ed-1a6e086cdb4f.png[/img]
(6) 把下边三个文件复制到 C:\Program Files (x86)\mosquitto 里。
C:\Program Files (x86)\OpenSSL-Win32\libeay32.dll
C:\Program Files (x86)\OpenSSL-Win32\ssleay32.dll
pthreadVC2.dll
(7) 再次执行 安装文件 mosquitto-1.4.14-install-win32.exe 就可安装完成。
(8) 通过控制面板>管理工具>服务,启 Mosquitto Broker 服务后在命令行工具里执行 netstat -an 查看端口是否开启。
[img]http://dl2.iteye.com/upload/attachment/0128/4079/7a157223-3562-3177-b740-074ff69fb154.png[/img]
参考:
https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-centos-7
[list]
[*]Mosquitto:https://mosquitto.org/
[*]VerneMQ:https://vernemq.com/
[*]EMQTT:http://emqtt.io/[/list]
其他更多的可以参考:https://github.com/mqtt/mqtt.github.io/wiki/servers
Mosquitto 是当前用户最多的一款产品,用C开发的。其他两个(VerneMQ、EMQTT)是用Erlang语言开发的。
[color=blue][b]CentOS7 里安装Mosquitto Broker[/b][/color]
[b](1)安装mosquitto[/b]
# yum -y install epel-release
# yum -y install mosquitto
启动服务
# systemctl start mosquitto
# netstat -nlp | grep 1883
测试确认(开启2个Terminal分别执行以下2个命令)
# mosquitto_sub -h localhost -t test/rensanning/mosquitto/t1
# mosquitto_pub -h localhost -t test/rensanning/mosquitto/t1 -m "hello world"
执行完mosquitto_pub命令后,在mosquitto_sub窗口能看到"hello world"消息的显示。
[b](2)设置密码[/b]
# mosquitto_passwd -c /etc/mosquitto/passwd rensanning
Password:123456
Reenter password:123456
# mv /etc/mosquitto/mosquitto.conf /etc/mosquitto/mosquitto.conf.bak
# vi /etc/mosquitto/mosquitto.conf
allow_anonymous false
password_file /etc/mosquitto/passwd
# systemctl restart mosquitto
[b]密码确认[/b]
# mosquitto_pub -h localhost -t test/rensanning/mosquitto/t2 -m "hello world2"
Connection Refused: not authorised.
Error: The connection was refused.
# mosquitto_sub -h localhost -u "rensanning" -P "123456" -t test/rensanning/mosquitto/t2
# mosquitto_pub -h localhost -u "rensanning" -P "123456" -t test/rensanning/mosquitto/t2 -m "hello world2"
执行完mosquitto_pub命令后,在mosquitto_sub窗口能看到"hello world2"消息的显示。
配置文件mosquitto.conf的举例内容可以参考官网说明:
https://mosquitto.org/man/mosquitto-conf-5.html
[b](3)客户端工具确认[/b]
使用Eclipse Paho MQTT Utility连接搭建的服务器测试确认。
[b]设置用户名和密码[/b]
[img]http://dl2.iteye.com/upload/attachment/0128/3850/f1ef3801-9435-389c-884f-c51dbd09867a.png[/img]
[b]连接、订阅、发布确认[/b]
[img]http://dl2.iteye.com/upload/attachment/0128/3852/afdf02a8-f282-35f5-93b6-d0d504af6f2e.png[/img]
[color=blue][b]Windows 7里安装Mosquitto Broker[/b][/color]
(1) 从 https://mosquitto.org/download/ 下载安装文件mosquitto-1.4.14-install-win32.exe
(2) 执行安装文件会提示下载OpenSSL 和 pThreads。
[img]http://dl2.iteye.com/upload/attachment/0128/4071/d9550b49-c1f7-34b8-bef6-2d4d68c0b824.png[/img]
(3) 继续执行完安装后,会提示SSLEAY21.dll找不到。
(4) 下载 Win32OpenSSL_Light-1_0_2n.exe 并安装。(注意选择把OpenSSL DLLS复制到 OpenSSL的安装目录下)
OpenSSL: http://slproweb.com/products/Win32OpenSSL.html
[img]http://dl2.iteye.com/upload/attachment/0128/4073/9fffa942-2a56-37c4-aa6e-b32e7bb735ee.png[/img]
[img]http://dl2.iteye.com/upload/attachment/0128/4075/c869eb24-61de-312f-8768-4fcb4d8ef9db.png[/img]
(5) 下载 pthreadVC2.dll
pThreads: ftp://sources.redhat.com/pub/pthreads-win32/dll-latest/dll/x86/
[img]http://dl2.iteye.com/upload/attachment/0128/4077/0a40759f-65d4-3718-95ed-1a6e086cdb4f.png[/img]
(6) 把下边三个文件复制到 C:\Program Files (x86)\mosquitto 里。
C:\Program Files (x86)\OpenSSL-Win32\libeay32.dll
C:\Program Files (x86)\OpenSSL-Win32\ssleay32.dll
pthreadVC2.dll
(7) 再次执行 安装文件 mosquitto-1.4.14-install-win32.exe 就可安装完成。
(8) 通过控制面板>管理工具>服务,启 Mosquitto Broker 服务后在命令行工具里执行 netstat -an 查看端口是否开启。
[img]http://dl2.iteye.com/upload/attachment/0128/4079/7a157223-3562-3177-b740-074ff69fb154.png[/img]
参考:
https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-centos-7