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

環境信息:c++

1. Linux Centos7.2 環境,CPU 2核,內存8G.web

2. mosquitto版本:mosquitto-1.5.4安全

官網:http://mosquitto.org/download/
libwebsockets官網: https://libwebsockets.org/websocket

本環境搭建,主要用到了mosquitto,麼有涉及websocket的內容,因此,這裏不作安裝介紹。socket

 

搭建過程:ide

1. 安裝mosquitto-1.5.4測試

下載解壓縮mosquitto-1.5.4.tar.gz。而後執行makeui

[root@ws2 mosquitto-1.5.4]# make
set -e; for d in lib client src; do make -C ${d}; done
make[1]: Entering directory `/opt/mosquitto-1.5.4/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:27:0:
mosquitto_internal.h:28:27: fatal error: openssl/ssl.h: No such file or directory
 #  include <openssl/ssl.h>
                           ^
compilation terminated.
make[1]: *** [mosquitto.o] Error 1
make[1]: Leaving directory `/opt/mosquitto-1.5.4/lib'
make: *** [mosquitto] Error 2

上述錯誤是由於沒有安裝openssl-devel。安裝上openssl-devel便可解決: yum install openssl-develthis

 

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

上述錯誤是由於沒有安裝gcc-c++. 安裝上gcc-c++便可解決: yum install gcc-c++spa

 

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

[root@ws2 mosquitto-1.5.4]# 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.5-28.el7.x86_64 already installed and latest version
Package gcc-c++-4.8.5-28.el7.x86_64 already installed and latest version
Package 1:openssl-devel-1.0.2k-12.el7.x86_64 already installed and latest version
Package c-ares-devel-1.10.0-3.el7.x86_64 already installed and latest version
Package libuuid-devel-2.23.2-52.el7.x86_64 already installed and latest version
Package wget-1.14-15.el7_4.1.x86_64 already installed and latest version
Package cmake-2.8.12.2-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:874.
Error: Unable to open configuration file.

這個錯誤,是由於我沒有安裝websockets模塊,另外,config.mk裏面的websocket沒有配置爲YES。因此將配置中websocket的註釋掉便可。

#listener 9090
#protocol websockets

再次啓動:

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

上述錯誤顯示沒有mosquitto用戶,建立用戶並添加組

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

最後再次啓動:

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

此時,mosquitto之前臺服務的方式在運行。。。如何使用,請看幫助信息:

[root@ws2 mosquitto]# mosquitto --help
mosquitto version 1.5.4

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.1: 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:

 

而後,再啓動一個終端,進行發佈消息操做:

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

 

在上面的訂閱窗口已經收到這個發佈的消息

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

 

安全控制:

上面的操做,是針對無安全控制的,下面,進行帶安全控制的配置,進入/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,作下面配置

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 ~]# 
相關文章
相關標籤/搜索