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即可。

版权声明:本文为博主原创文章,未经博主允许不得转载。   
 

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