MQTT入门(5)- 服务器Servers/Brokers

虽然有免费的公开Broker服务可以使用,当初步理解了MQTT协议之后,就有必要自己动手搭建一台Broker了。这样才能更深入的学习MQTT的更多内容。开源的Broker有很多,目前主流的Broker有以下3个:
  • Mosquitto:https://mosquitto.org/
  • VerneMQ:https://vernemq.com/
  • EMQTT:http://emqtt.io/
其他更多的可以参考:https://github.com/mqtt/mqtt.github.io/wiki/servers

Mosquitto 是当前用户最多的一款产品,用C开发的。其他两个(VerneMQ、EMQTT)是用Erlang语言开发的。

CentOS7 里安装Mosquitto Broker

(1)安装mosquitto
# 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"消息的显示。

(2)设置密码
# 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


密码确认
# 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

(3)客户端工具确认
使用Eclipse Paho MQTT Utility连接搭建的服务器测试确认。

设置用户名和密码


连接、订阅、发布确认


Windows 7里安装Mosquitto Broker

(1) 从 https://mosquitto.org/download/ 下载安装文件mosquitto-1.4.14-install-win32.exe

(2) 执行安装文件会提示下载OpenSSL 和 pThreads。


(3) 继续执行完安装后,会提示SSLEAY21.dll找不到。

(4) 下载 Win32OpenSSL_Light-1_0_2n.exe 并安装。(注意选择把OpenSSL DLLS复制到 OpenSSL的安装目录下)
OpenSSL: http://slproweb.com/products/Win32OpenSSL.html



(5) 下载 pthreadVC2.dll
pThreads: ftp://sources.redhat.com/pub/pthreads-win32/dll-latest/dll/x86/


(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 查看端口是否开启。


参考:
https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-centos-7

猜你喜欢

转载自rensanning.iteye.com/blog/2406635