MQTT服务器在Linux和Windows上的搭建稍微有些区别,不过使用第三方开源的项目一般会有比较详细的说明文档。不做过多赘述。笔者搭建环境是windows10。
ActiveMQ
1、windows MQTT服务器下载
https://activemq.apache.org/components/classic/download/
mosquitto
1、下载与安装 http://mosquitto.org/download/
2、在配置文件mosquitto.conf中,任意位置加入下面文本,保存退出(我说的全新安装的情况下,如果是维护已经使用很久的系统,可在配置文件中找到对应的属性进行更改)
#设置不允许匿名登录
allow_anonymous false
#设置账户密码文件位置为C:\MosquittoTest\pwfile.example
password_file /MosquittoTest/pwfile.example
3、重启mosquitto服务配置文件的修改才能生效。为了方便调试和操作,我们后面的操作全部通过命令行模式进行。
4、插入新用户名及密码,输入密码时界面是不会显示的,直接输入后回车就可以,需要连续输入两次。保证pwfile.example的路径和上面的配置一致。下面打开CMD并进入mosquitto根目录输入:
mosquitto_passwd -c /MosquittoTest/pwfile.example FirstUserName (使用-c 参数会导致清空密码文件,重新插入用户)
mosquitto_passwd /MosquittoTest/pwfile.example SecondUserName (不使用-c 表示追加用户,不影响旧用户)
5、创建成功后pwfile.example会出现刚刚添加的用户信息。
6、启动mosquitto 进行测试。
(1)以管理员的方式运行第一个CMD。进入到mosquitto的目录下“,启动服务器
mosquitto.exe -v
(2)以管理员的方式运行第二个CMD,进入到mosquitto的目录下,订阅一个主题:
mosquitto_sub -u UserName -P password -t 'James/topic' -v
(3)以管理员的方式运行第三个(或更多个)CMD,进入到mosquitto的目录下,发布一个主题消息:
mosquitto_pub -u UserName -P password -t 'James/topic' -m '喜欢也没用,没用也喜欢'
mosquitto>mosquitto.exe --help
mosquitto version 1.6.9
mosquitto is an MQTT v3.1.1 broker.
Usage: mosquitto [-c config_file] [-d] [-h] [-p port]
-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.
See http://mosquitto.org/ for more information.
EMQ X
下载地址 https://www.emqx.io/downloads
文档地址 https://docs.emqx.io/broker/v3/cn/getstarted.html
./bin/emqx start
# 检查运行状态
./bin/emqx_ctl status
# 停止
emqx ./bin/emqx stop
总结:
使用过程中,个人觉得还是mouquitto,尤其是EMQX比较好用一些,着重推荐。