Ubuntu環境下基於MQTT協議的mosquitto以及集羣和鑑權插件的安裝和簡單使用

本文就衆多MQTT-Server中的mosquitto的安裝進行講解。

一、下載。

對於Ubuntu系統,可以使用sudo apt-get 來安裝mosquitto,但是這種方法雖然簡單,但是對於配置文件的修改和管理比較麻煩,配置文件需要自己寫好然後啓動時載入,因此不太推薦。

本人更推薦的是第二種方法,也就是本文重點要講的,下載tar.gz安裝包,進行安裝。

首先,到mosquitto的官方網站(http://mosquitto.org/files/source/)下載,tar.gz安裝包。本人使用的是1.4.15版本。如果你使用的是服務器版本,你可以使用wget  http://mosquitto.org/files/source/mosquitto-1.4.15.tar.gz 這個命令直接下載。

二、解壓

如果你習慣 控制檯命令或是在server版編程的話,使用 tar -xvf mosquitto-1.4.15.tar.gz 命令來進行解壓,解藥後會產生相應的文件夾,如果你更習慣於圖形化安裝,使用exact進行提取安裝也是不錯的選擇。

三、安裝 

重點關注一下config.mk這個文件,這個文件會提供一些基礎 配置。

下面我們進行安裝。安裝使用make命令,但是往往不可以一次make成功,所以,就安裝部分進行重點講解。

如果,使用make 時,提示沒有make,首先使用sudo apt-get install make 來執行。

如果,有了可以正常使用make命令,你輸入make 會發現,並不能成功make 而是會報錯,一般會出現四到五種錯誤。

1)提示:缺少ssl.h

set -e; for d in lib client src; do make -C ${d}; done
make[1]: Entering directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/lib'
cc -Wall -ggdb -O2  -I. -I.. -I../lib -fPIC -DWITH_TLS -DWITH_TLS_PSK -DWITH_THREADING -DWITH_SOCKS -DWITH_SRV -c mosquitto.c -o mosquitto.o
In file included from mosquitto.c:33:0:
./mosquitto_internal.h:27: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 `/home/jia/Desktop/mqtt/mosquitto-1.4.15/lib'
make: *** [mosquitto] Error 2

解決辦法:使用 sudo apt-get install libssl-dev

2)提示:缺少ares.h

set -e; for d in lib client src; do make -C ${d}; done
make[1]: Entering directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/lib'
cc -Wall -ggdb -O2  -I. -I.. -I../lib -fPIC -DWITH_TLS -DWITH_TLS_PSK -DWITH_THREADING -DWITH_SOCKS -DWITH_SRV -c mosquitto.c -o mosquitto.o
In file included from mosquitto.c:33:0:
./mosquitto_internal.h:40:20: fatal error: ares.h: No such file or directory
 #  include <ares.h>
                    ^
compilation terminated.
make[1]: *** [mosquitto.o] Error 1
make[1]: Leaving directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/lib'
make: *** [mosquitto] Error 2

解決辦法:使用 sudo apt-get install libc-ares-dev

3)提示:缺少g++

make[2]: Entering directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/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 `/home/jia/Desktop/mqtt/mosquitto-1.4.15/lib/cpp'
make[1]: *** [all] Error 2
make[1]: Leaving directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/lib'
make: *** [mosquitto] Error 2

解決辦法 :使用 sudo apt-get install g++

4)提示:沒有UUID

read_handle_server.c:31:25: fatal error: uuid/uuid.h: No such file or directory
 #  include <uuid/uuid.h>
                         ^
compilation terminated.
make[1]: *** [read_handle_server.o] Error 1
make[1]: Leaving directory `/home/jia/Desktop/mqtt/mosquitto-1.4.15/src'
make: *** [mosquitto] Error 2

解決辦法: 使用  sudo apt-get install uuid-dev

5)這裏說xsltproc命令找不到

make[1]: Leaving directory `/home/lxb/mosquitto-1.4.14/src'
set -e; for d in man; do make -C ${d}; done
make[1]: Entering directory `/home/lxb/mosquitto-1.4.14/man'
xsltproc mosquitto.8.xml
make[1]: xsltproc: Command not found
make[1]: *** [mosquitto.8] Error 127
make[1]: Leaving directory `/home/lxb/mosquitto-1.4.14/man'
make: *** [docs] Error 2

解決辦法:sudo apt-get install xsltproc

6)然後就是mosquitt.8錯誤,看錯誤提示,根源還是找不到docbook.xsl 

set -e; for d in lib client src; do make -C ${d}; done
make[1]: Entering directory '/home/ctbri/mosquitto-cluster/lib'
make -C cpp
make[2]: Entering directory '/home/ctbri/mosquitto-cluster/lib/cpp'
make[2]: Nothing to be done for 'all'.
make[2]: Leaving directory '/home/ctbri/mosquitto-cluster/lib/cpp'
make[1]: Leaving directory '/home/ctbri/mosquitto-cluster/lib'
make[1]: Entering directory '/home/ctbri/mosquitto-cluster/client'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/ctbri/mosquitto-cluster/client'
make[1]: Entering directory '/home/ctbri/mosquitto-cluster/src'
make[1]: Nothing to be done for 'all'.
make[1]: Leaving directory '/home/ctbri/mosquitto-cluster/src'
set -e; for d in man; do make -C ${d}; done
make[1]: Entering directory '/home/ctbri/mosquitto-cluster/man'
xsltproc mosquitto.8.xml
warning: failed to load external entity "/usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl"
compilation error: file manpage.xsl line 3 element import
xsl:import : unable to load /usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl
compilation error: file mosquitto.8.xml line 4 element refentry
xsltParseStylesheetProcess : document is not a stylesheet
Makefile:48: recipe for target 'mosquitto.8' failed
make[1]: *** [mosquitto.8] Error 5
make[1]: Leaving directory '/home/ctbri/mosquitto-cluster/man'
Makefile:17: recipe for target 'docs' failed
make: *** [docs] Error 2

解決辦法:sudo apt-get install docbook-xsl 

7)有可能出現找不到libmosquitto.so.1的情況

解決辦法:sudo cp /usr/local/lib/libmosquitto.so.1 /usr/lib/cp /usr/local/lib/libmosquitto.so.1 /usr/lib/

或者sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1

通常大家安裝會遇到上述的情況,解決完上述問題,就可以正常使用make命令,make後,使用sudo make install 安裝即可

四、使用

首先要注意的是,安裝完後的mosquitto的配置文件在根目錄下的etc下的mosquitto,使用 cd /etc/mosquitto 就可以找到相應的位置。

之後,使用  cp mosquitto.conf.example mosquitto.conf 命令,複製一份新的conf配置文件。

使用sudo vi  mosquitto.conf  或者 sudo gedit  mosquitto.conf  均可,總之,注意加權限。

入門的話,首先修改這幾個地方:

1) user :默認是mosquitto,要更換成你當前的用戶

2)port:打開1883端口,如果使用ssl或tls打開8883端口

3)protocol:mqtt

修改好後保存,回到安裝目錄。

五 、啓動

使用, mosquitto -c /etc/mosquitto/mosquitto.conf  開啓服務器。 

在新建一個端口,使用, netstat -tunlp|grep 1883 查看端口是否被監聽

之後,使用 mosquitto_sub -t 'topic' 請求一個訂閱 

再新建一個終端,使用 mosquitto_pub -t 'topic' -m 'msg1'發佈一個訂閱,這時候即可查看到訂閱消息。

 

六、鑑權軟件安裝

 對於mosquitto的使用,我們往往需要用到鑑權的功能,mosquitto的第三方的插件提供了相應的功能。

具體參考:https://github.com/jpmens/mosquitto-auth-plug

這裏只講解如何安裝和基本配置。

1)環境部署:

安裝前,先使用:sudo apt-get install libcurl4-openssl-dev這個命令,防止出現:

be-http.c:39:23: fatal error: curl/curl.h: No such file or directory

這樣的問題。

2)下載:

wget https://github.com/jpmens/mosquitto-auth-plug/archive/0.1.2.tar.gz

然後解壓,進入目錄,重新拷貝一份mk文件,cp config.mk.in config.mk

3)修改配置文件

修改新的mk文件,對配置項做如下修改:

BACKEND_MYSQL ?= no

#作者主要使用httpserver來進行鑑權,因此,將http改爲yes

BACKEND_HTTP ?= yes

#編譯需要src,這個src地址就是之前mosquitto安裝路徑下,src的地址,可以使用pwd去查看。

MOSQUITTO_SRC = /usr/local/mosquitto-env/mosquitto-1.4.9/src

4)編譯

make

5)安裝使用

mosquitto目錄下創建plugin目錄

mkdir plugin

將插件中的.so文件複製到plugin目錄下

cp auth-plug.so mosquitto/plugin

6)修改mosquitto配置文件(vi mosquitto.conf),打開插件

找到 allow_anonymous false

在這一行下新增

 auth_plugin /usr/local/mqtt/mosquitto-1.4.9/plugin/auth-plug.so(auth-plug.so路徑)

#http 驗證使用這個

auth_opt_backends http

auth_opt_http_ip 192.168.1.1

auth_opt_http_port 5555

#下面三個必填,後面的路徑自行配置

auth_opt_http_getuser_uri /mqtt/auth

auth_opt_http_superuser_uri /mqtt/superuser

auth_opt_http_aclcheck_uri /mqtt/acl

7)啓動消息代理服務器

mosquitto -c mosquitto.conf即可

 

 

七、附錄

最後在附一份他的配置文件中文版:

# =================================================================
# General configuration
# =================================================================

# 客戶端心跳的間隔時間
#retry_interval 20

# 系統狀態的刷新時間
#sys_interval 10

# 系統資源的回收時間,0表示儘快處理
#store_clean_interval 10

# 服務進程的PID
#pid_file /var/run/mosquitto.pid

# 服務進程的系統用戶
#user mosquitto

# 客戶端心跳消息的最大併發數
#max_inflight_messages 10

# 客戶端心跳消息緩存隊列
#max_queued_messages 100

# 用於設置客戶端長連接的過期時間,默認永不過期
#persistent_client_expiration

# =================================================================
# Default listener
# =================================================================

# 服務綁定的IP地址
#bind_address

# 服務綁定的端口號
#port 1883

# 允許的最大連接數,-1表示沒有限制
#max_connections -1

# cafile:CA證書文件
# capath:CA證書目錄
# certfile:PEM證書文件
# keyfile:PEM密鑰文件
#cafile
#capath
#certfile
#keyfile

# 必須提供證書以保證數據安全性
#require_certificate false

# 若require_certificate值爲true,use_identity_as_username也必須爲true
#use_identity_as_username false

# 啓用PSK(Pre-shared-key)支持
#psk_hint

# SSL/TSL加密算法,可以使用“openssl ciphers”命令獲取
# as the output of that command.
#ciphers

# =================================================================
# Persistence
# =================================================================

# 消息自動保存的間隔時間
#autosave_interval 1800

# 消息自動保存功能的開關
#autosave_on_changes false

# 持久化功能的開關
persistence true

# 持久化DB文件
#persistence_file mosquitto.db

# 持久化DB文件目錄
#persistence_location /var/lib/mosquitto/

# =================================================================
# Logging
# =================================================================

# 4種日誌模式:stdout、stderr、syslog、topic
# none 則表示不記日誌,此配置可以提升些許性能
log_dest none

# 選擇日誌的級別(可設置多項)
#log_type error
#log_type warning
#log_type notice
#log_type information

# 是否記錄客戶端連接信息
#connection_messages true

# 是否記錄日誌時間
#log_timestamp true

# =================================================================
# Security
# =================================================================

# 客戶端ID的前綴限制,可用於保證安全性
#clientid_prefixes

# 允許匿名用戶
#allow_anonymous true

# 用戶/密碼文件,默認格式:username:password
#password_file

# PSK格式密碼文件,默認格式:identity:key
#psk_file

# pattern write sensor/%u/data
# ACL權限配置,常用語法如下:
# 用戶限制:user <username>
# 話題限制:topic [read|write] <topic>
# 正則限制:pattern write sensor/%u/data
#acl_file

# =================================================================
# Bridges
# =================================================================

# 允許服務之間使用“橋接”模式(可用於分佈式部署)
#connection <name>
#address <host>[:<port>]
#topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]

# 設置橋接的客戶端ID
#clientid

# 橋接斷開時,是否清除遠程服務器中的消息
#cleansession false

# 是否發佈橋接的狀態信息
#notifications true

# 設置橋接模式下,消息將會發布到的話題地址
# $SYS/broker/connection/<clientid>/state
#notification_topic

# 設置橋接的keepalive數值
#keepalive_interval 60

# 橋接模式,目前有三種:automatic、lazy、once
#start_type automatic

# 橋接模式automatic的超時時間
#restart_timeout 30

# 橋接模式lazy的超時時間
#idle_timeout 60

# 橋接客戶端的用戶名
#username

# 橋接客戶端的密碼
#password

# bridge_cafile:橋接客戶端的CA證書文件
# bridge_capath:橋接客戶端的CA證書目錄
# bridge_certfile:橋接客戶端的PEM證書文件
# bridge_keyfile:橋接客戶端的PEM密鑰文件
#bridge_cafile
#bridge_capath
#bridge_certfile
#bridge_keyfile

 

 

下面說一下mosquitto的集羣的安裝:

mqtt集羣較單機有很多的不同,我們採用在git上的一個開源mqtt集羣https://github.com/hui6075/mosquitto-cluster/blob/master/readme-zh_cn.md#mosquitto%E9%9B%86%E7%BE%A4%E7%89%B9%E6%80%A7

安裝起來很單機差不多,主要區別於安裝的配置文件,在/etc/mosquitto/mosquitto.conf這個文件,加入集羣的配置即可

  

node_name node1
node_address 192.168.1.101:1883

node_name node2
node_address 192.168.1.102:1883
 

node_name node3
node_address 192.168.1.103:1883

注意:在make之前編譯mosquitto時把config.mk中的WITH_BRIDGE註釋掉,把WITH_CLUSTER:=yes取消註釋,然後make && make install安裝mosquitto即可。

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。   
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章