Install:
apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
apt update
apt install mosquitto
service mosquitto status
service mosquitto start

Client install:
apt install mosquitto-clients

【基本配置】
vim /etc/mosquitto/mosquitto.conf
allow_anonymous false #不允许匿名
password_file /etc/mosquitto/passwd #配置用户密码文件
acl_file /etc/mosquitto/acl #配置topic和用户 (这个就是配置特权用户访问某主题,如果不配置则使用password_file文件中的用户密码进行访问)

用htpasswd配置passwd文件
mosquitto_passwd -c /etc/mosquitto/passwd pub_client password1
mosquitto_passwd /etc/mosquitto/passwd sub_client password2

[root@sparkVM mosquitto]# cat /etc/mosquitto/passwd
sub_client:$6$lHiPm6dLpaqsdfQb$SETYv2TthcgK388atPA7jNTSQYlWZzz8HxRzOVeZMx5iVNAAViuHhIgYzayl5BmzjNo8C0Cf4CH6ss6LdWtW8Q==
pub_client:$6$NDYKXj+h1wb5rIsz$Mf1Hq+EEsmXXy1Y377Rt8S4oVfm3S06R6Km3rqzzOQYIKCIDz8z5vVFh8CHGx4zPnBRMWObNnFvOYVjnOe2Sdw==

配置acl,topic和用户的关系
[root@sparkVM mosquitto]# cat /etc/mosquitto/acl

pub_client 用户可读可写

user pub_client
topic bell/#

sub_client 只可读

user sub_client
topic read bell/#

【测试验证】
服务端:
Linux代码 收藏代码
[root@sparkVM mosquitto]# mosquitto_pub -t mtopic -m "test"
1416301592: New connection from ::1 on port 1883.
Connection Refused: not authorised.

[root@sparkVM mosquitto]# mosquitto_pub -t mtopic -u pub_client -P test -m "test"
1416301643: New connection from ::1 on port 1883.
1416301643: New client connected from ::1 as mosqpub/4113-sparkVM (c1, k60, upub_client).

客户端:
Linux代码 收藏代码
[root@pandaVM html]# mosquitto_sub -h 192.168.197.128 -t mtopic
Connection Refused: not authorised.

[root@pandaVM html]# mosquitto_sub -h 192.168.197.128 -t mtopic -u sub_client -P sub_client
test

客户端:
https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.ui.app/1.1.1/org.eclipse.paho.ui.app-1.1.1-win32.win32.x86_64.zip

  1. MQTT中的QoS等级
    MQTT设计了一套保证消息稳定传输的机制,包括消息应答、存储和重传。在这套机制下,提供了三种不同层次QoS(Quality of Service):

QoS0,At most once,至多一次;
QoS1,At least once,至少一次;
QoS2,Exactly once,确保只有一次。

标签: none

添加新评论