MQTT研究之mosquitto:【环境搭建】

环境信息:

1. Linux Centos7.2 环境,CPU 2核,内存8G.

2. mosquitto版本:mosquitto-1.5.4

官网:http://mosquitto.org/download/
libwebsockets官网: https://libwebsockets.org/网址:yii666.com

本环境搭建,主要用到了mosquitto,么有涉及websocket的内容,所以,这里不做安装介绍。

搭建过程:文章地址https://www.yii666.com/article/764248.html

1. 安装mosquitto-1.5.4

下载解压缩mosquitto-1.5.4.tar.gz。然后执行make

[root@ws2 mosquitto-1.5.]# make
set -e; for d in lib client src; do make -C ${d}; done
make[]: Entering directory `/opt/mosquitto-1.5./lib'
cc -Wall -ggdb -O2 -I. -I.. -I../lib -fPIC -DWITH_TLS -DWITH_TLS_PSK -DWITH_THREADING -DWITH_SOCKS -c mosquitto.c -o mosquitto.o
In file included from mosquitto.c:::
mosquitto_internal.h::: fatal error: openssl/ssl.h: No such file or directory
# include <openssl/ssl.h>
^
compilation terminated.
make[]: *** [mosquitto.o] Error
make[]: Leaving directory `/opt/mosquitto-1.5./lib'
make: *** [mosquitto] Error

上述错误是因为没有安装openssl-devel。安装上openssl-devel即可解决: yum install openssl-devel

make -C cpp
make[]: Entering directory `/opt/mosquitto-1.5./lib/cpp'
g++ -Wall -ggdb -O2 -I. -I.. -I../lib -fPIC -c mosquittopp.cpp -o mosquittopp.o
make[]: g++: Command not found
make[]: *** [mosquittopp.o] Error
make[]: Leaving directory `/opt/mosquitto-1.5./lib/cpp'
make[]: *** [all] Error
make[]: Leaving directory `/opt/mosquitto-1.5./lib'

上述错误是因为没有安装gcc-c++. 安装上gcc-c++即可解决: yum install gcc-c++

cc -I.. -Wall -ggdb -O2  -c mosquitto_passwd.c -o mosquitto_passwd.o
cc mosquitto_passwd.o -o mosquitto_passwd -lcrypto
make[]: Leaving directory `/opt/mosquitto-1.5./src'
set -e; for d in man; do make -C ${d}; done
make[]: Entering directory `/opt/mosquitto-1.5./man'
make[]: Nothing to be done for `all'.
make[]: Leaving directory `/opt/mosquitto-1.5./man'

上述错误是因为没有安装c-ares-devel. 安装上c-ares-devel即可解决: yum install c-ares-devel

解决上面的问题,可以一次安装下面的所有的依赖:yum -y install gcc gcc-c++ openssl-devel c-ares-devel libuuid-devel wget cmake文章来源地址https://www.yii666.com/article/764248.html

[root@ws2 mosquitto-1.5.]# yum -y install gcc gcc-c++ openssl-devel c-ares-devel libuuid-devel wget cmake
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Package gcc-4.8.-.el7.x86_64 already installed and latest version
Package gcc-c++-4.8.-.el7.x86_64 already installed and latest version
Package :openssl-devel-1.0.2k-.el7.x86_64 already installed and latest version
Package c-ares-devel-1.10.-.el7.x86_64 already installed and latest version
Package libuuid-devel-2.23.-.el7.x86_64 already installed and latest version
Package wget-1.14-.el7_4..x86_64 already installed and latest version
Package cmake-2.8.12.2-.el7.x86_64 already installed and latest version

2. 配置mosquitto

进入/etc/mosquitto目录

[root@ws2 mosquitto]# cp mosquitto.conf.example mosquitto.conf
[root@ws2 mosquitto]# cp pwfile.example pwfile

vi mosquitto.conf,做下面配置

allow_anonymous true
password_file /etc/mosquitto/pwfile

然后,在文件最末尾补充(mosquitto服务监听的端口):

port 1883

listener 9090

protocol websockets

3. 启动mosquitto

[root@ws2 mosquitto]# mosquitto -c /etc/mosquitto/mosquitto.conf
Error: Websockets support not available.
Error found at /etc/mosquitto/mosquitto.conf:.
Error: Unable to open configuration file.

这个错误,是因为我没有安装websockets模块,另外,config.mk里面的websocket没有配置为YES。所以将配置中websocket的注释掉即可。

#listener
#protocol websockets

再次启动:

[root@ws2 mosquitto]# mosquitto -c /etc/mosquitto/mosquitto.conf
: mosquitto version 1.5. starting
: Config loaded from /etc/mosquitto/mosquitto.conf.
: Opening ipv4 listen socket on port .
: Opening ipv6 listen socket on port .
: Error: Invalid user 'mosquitto'.

上述错误显示没有mosquitto用户,创建用户并添加组网址:yii666.com<

[root@ws2 mosquitto]# groupadd mosquitto
[root@ws2 mosquitto]# useradd -g mosquitto mosquitto

最后再次启动:

[root@ws2 mosquitto]# mosquitto -c /etc/mosquitto/mosquitto.conf
: mosquitto version 1.5. starting
: Config loaded from /etc/mosquitto/mosquitto.conf.
: Opening ipv4 listen socket on port .
: Opening ipv6 listen socket on port .

此时,mosquitto以前台服务的方式在运行。。。如何使用,请看帮助信息:

[root@ws2 mosquitto]# mosquitto --help
mosquitto version 1.5. 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.

4. 测试验证

[root@ws2 ~]# mosquitto_sub -t rulee
mosquitto_sub: error while loading shared libraries: libmosquitto.so.: cannot open shared object file: No such file or directory

此处错误,表明动态库没有配合好路径,参照下面的操作(主要是红色的两行指令):

[root@ws2 ~]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
[root@ws2 ~]#
[root@ws2 ~]# echo "/usr/local/lib" >> /etc/ld.so.conf
[root@ws2 ~]#
[root@ws2 ~]# vi /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/local/lib
[root@ws2 ~]# ldconfig

然后再次订阅一个rulee的topic:

MQTT研究之mosquitto:【环境搭建】

然后,再启动一个终端,进行发布消息操作:

[root@ws2 ~]# mosquitto_pub -h localhost -t rulee -m "hello rule engine"

MQTT研究之mosquitto:【环境搭建】

在上面的订阅窗口已经收到这个发布的消息

[root@ws2 ~]# mosquitto_sub -t rulee
hello rule engine

MQTT研究之mosquitto:【环境搭建】

安全控制:

上面的操作,是针对无安全控制的,下面,进行带安全控制的配置,进入/etc/mosquitto目录:

[root@ws2 mosquitto]# cp mosquitto.conf.example mosquitto.conf
[root@ws2 mosquitto]# cp pwfile.example pwfile
[root@ws2 mosquitto]# cp aclfile.example aclfile

vi mosquitto.conf,做下面配置文章来源地址:https://www.yii666.com/article/764248.html

allow_anonymous false
password_file /etc/mosquitto/pwfile
acl_file /etc/mosquitto/aclfile

然后,在文件最末尾补充监听端口(若没有配置的话): port 1883

添加安全用户信息:

[root@ws2 mosquitto]# mosquitto_passwd -c /etc/mosquitto/pwfile shihuc
Password:
Reenter password:

重新启动mosquitto服务:

[root@ws2 mosquitto]# mosquitto -d -c /etc/mosquitto/mosquitto.conf

订阅:

[root@ws2 ~]# mosquitto_sub -t rulee
hello rule engine
Connection Refused: not authorised.
[root@ws2 ~]#
[root@ws2 ~]#
[root@ws2 ~]# mosquitto_sub -t /taikang/rulee -u shihuc -P shihuc

发布:

[root@ws2 ~]# mosquitto_pub -h localhost -t /taikang/rulee -m "hello rule engine with auth"
Connection Refused: not authorised.
Error: The connection was refused.
[root@ws2 ~]#
[root@ws2 ~]#
[root@ws2 ~]# mosquitto_pub -u shihuc -P shihuc -h localhost -t /taikang/rulee -m "hello rule engine with auth"
[root@ws2 ~]#

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

MQTT研究之mosquitto:【环境搭建】-相关文章

  1. mosquitto broker 安装服务后启动失败

  2. MQTT的学习研究(二)moquette-mqtt 的使用之mqtt broker的启动

  3. ActiveMQ学习笔记(5)----Broker的启动方式

  4. MQTT研究之mosquitto:【环境搭建】

  5. mosquitto 常用命令

  6. RocketMQ原理解析-Broker

    broker 1. broker的启动brker的启动Broker向namesrv注册Topic在broker文件上的存储json格式Namesrv接收Broker注册的topic信息, namesrv只存内存,但是broker有任务定时推送1.      接收数据向RouteInfoManager注册。Broker初始化加载本地配置,配置信息是以json格式存储在本地, rocketmq强依赖fastjs

  7. Mosquitto搭建Android推送服务(二)Mosquitto简介及搭建

    文章钢要:1、了解Mosquitto服务器2、在Liunx中搭建Mosquitto服务器3、设置Mosquitto集群一、Mosquitto简介一款实现了消息推送协议 MQTT v3.1 的开源消息代理软件,提供轻量级的,支持可发布/可订阅的的消息推送模式,使设备对设备之间的短消息通信变得简单,比如现在应用广泛的低功

  8. QMQ去哪儿网-mq中间件(启动失败)

    简介去哪儿网近日宣布开源其内部广泛使用的消息中间件 QMQ 。QMQ 自 2012 年诞生以来在去哪儿网所有业务场景中广泛的应用,包括跟交易息息相关的订单场景; 也包括报价搜索等高吞吐量场景。目前在公司内部日常消息 qps 在 60W 左右,生产上承载将近 4W+ 消息 topic ,消息的端

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png