centos 7 上源码安装mosquitto-2.0.8,用户密码及权限设置,共享订阅

centos 7上安装mosquitto-2.0.8,源码模式安装。

源码下载地址:http://mosquitto.org/files/source/ 

1、安装依赖(必须确保每个安装成功):

1 yum install gcc gcc-c++ libstdc++-devel
2 yum install openssl-devel -y
3 yum install c-ares-devel -y
4 yum install uuid-devel -y
5 yum install libuuid-devel -y

 

2、下载包:

 1 wget https://mosquitto.org/files/source/mosquitto-2.0.8.tar.gz
 2 tar -zxvf mosquitto-2.0.8.tar.gz
 3 cd mosquitto-2.0.8
 4 make && make install
 5 cd /etc/mosquitto
 6 cp mosquitto.conf.example mosquitto.conf
 7 # 测试启动服务
 8 mosquitto -c /etc/mosquitto/mosquitto.conf
 9 #测试pub
10 mosquitto_pub --help

 



安装注意点

【1】编译时出现fatal error: cjson/cJSON.h: No such file or directory

【解决方法】——需要安装cJSON

 1 yum install cmake
 2 git clone git@github.com:DaveGamble/cJSON.git
 3 cd cJSON/
 4 mkdir build
 5 cd build/
 6 cmake ..
 7 make
 8 make install
 9 echo "/usr/local/lib64" >> /etc/ld.so.conf
10 /sbin/ldconfig


【2】运行mosquitto_pub 时出现问题:

 1 mosquitto_pub: error while loading shared libraries: libcjson.so.1: cannot open shared object file: No such file or directory。 

【解决方法】cJSON安装不正确。

 

【3】使用mosquitto_pub 过程中找不到libmosquitto.so.1

1 ./mosquitto_pub : error while loading shared libraries:
2 libmosquitto.so.1: cannot open shared object file: No such file or
3 directory

【解决方法】

1 # 创建链接
2 sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
3 # 更新动态链接库
4 sudo ldconfig

 



【4】新增用户

修改mosquitto.conf 配置文件

 3 # into mosquitto (it is recommended that TLS support should be included) then
 4 # plain text passwords are used, in which case the file should be a text file
 5 # with lines in the format:
 6 # username:password
 7 # The password (and colon) may be omitted if desired, although this
 8 # offers very little in the way of security.
 9 #
10 # See the TLS client require_certificate and use_identity_as_username options
11 # for alternative authentication options. If an auth_plugin is used as well as
12 # password_file, the auth_plugin check will be made first.
13 #打开password_file
password_file /etc/mosquitto/pwfile

创建用户密码

1 mosquitto_passwd -c /etc/mosquitto/pwfile testa 
2 
3 mosquitto_passwd -c /etc/mosquitto/pwfile testb 

重启生效。

 

 

【5】设置用户权限,testa只能订阅/req/#主题、发布/res/#主题,testb正好相反

修改mosquitto.conf如下配置:

# The form is the same as for the topic keyword, but using pattern as the
# keyword.
# Pattern ACLs apply to all users even if the "user" keyword has previously
# been given.
#
# If using bridges with usernames and ACLs, connection messages can be allowed
# with the following pattern:
# pattern write $SYS/broker/connection/%c/state
#
# pattern [read|write|readwrite] <topic>
#
# Example:
#
# pattern write sensor/%u/data
#
# If an auth_plugin is used as well as acl_file, the auth_plugin check will be
# made first.
acl_file /etc/mosquitto/aclfile
 

修改aclfile:

cd /etc/mosquitto
cp aclfile.example aclfile
vi aclfile
 1 # This affects access control for clients with no username.
 2 topic read $SYS/#
 3 
 4 # This only affects clients with username "roger".
 5 user roger
 6 topic foo/bar
 7 
 8 
 9 # This affects all clients.
10 pattern write $SYS/broker/connection/%c/state
11 
12 
13 user testa
14 topic write /req/#
15 topic read /res/#
16 
17 user testb
18 topic read /req/#
19 topic write /res/#

重启生效。

 

 


【6】共享订阅,解决多机同时订阅相同主题时,多次消费的问题。

订阅端订阅主题:$share/ topic/#

 

注意:共享订阅为mqtt5新特性

订阅端需支持mqtt5协议,java 可使用org.eclipse.paho.client.mqttv5 或者hivemq-mqtt-client

posted @ 2022-06-29 18:47  脸探书丛  阅读(1080)  评论(0)    收藏  举报