简介: Eclipse Mosquitto是一个开源消息代理,实现了MQTT协议版本3.1和3.1.1、5.0。Mosquitto轻量,适用于低功耗单板计算机到完整服务器的所有设备。

其他服务器代理实现:https://github.com/mqtt/mqtt.github.io/wiki/servers
操作系统安装指引:https://mosquitto.org/download/

  1. 下载安装
    以Ubuntu16为例

添加到存储库列表
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
更新软件包
sudo apt-get update
安装
sudo apt-get install mosquitto
安装命令行客户端
sudo apt-get install mosquitto-clients
2. 配置
2.1 主配置文件mosquitto.conf
pid_file /var/run/mosquitto.pid

消息持久存储

persistence true
persistence_location /var/lib/mosquitto/

日志文件

log_dest file /var/log/mosquitto/mosquitto.log

其他配置

include_dir /etc/mosquitto/conf.d

禁止匿名访问

allow_anonymous false

认证配置

password_file /etc/mosquitto/pwfile

权限配置

acl_file /etc/mosquitto/aclfile
2.2 认证配置pwfile
没有则创建文件
touch /etc/mosquitto/pwfile
服务开启后,输入如下命令,根据提示输入两遍密码
mosquitto_passwd /etc/mosquitto/pwfile 用户名
2.3 权限配置aclfile
打开文件
vim /etc/mosquitto/aclfile
编辑内容

李雷只能发布以test为前缀的主题,订阅以$SYS开头的主题即系统主题

user lilei
topic write test/#
topic read $SYS/#

韩梅梅只能订阅以test为前缀的主题

user hanmeimei
topic read test/#
3. 启动
-c:指定特定配置文件启动
-d:后台运行

mosquitto -c /etc/mosquitto/mosquitto.conf -d
4. 测试
发布使用mosquitto_pub命令,订阅使用mosquitto_sub命令。常用参数介绍:

参数 描述
-h 服务器主机,默认localhost
-t 指定主题
-u 用户名
-P 密码
-i 客户端id,唯一
-m 发布的消息内容
订阅

mosquitto_sub -h localhost -t “test/#” -u hanmeimei -P 123456 -i “client1”
订阅系统主题

订阅客户端存活连接数

mosquitto_sub -h localhost –t ‘$SYS/broker/clients/active’ -u lilei -P 123456 -i “client2”
发布

mosquitto_pub -h localhost -t “test/abc” -u lilei -P 123456 -i “client3” -m “How are you?”
链接
项目网站:https://www.eclipse.org/paho
Eclipse项目信息:https://projects.eclipse.org/projects/iot.paho
GitHub:https://github.com/eclipse/paho.mqtt.java
MQTT Java客户端的使用:https://www.jianshu.com/p/65e1748a930c
Spring支持:https://www.jianshu.com/p/6b60858b7d44

mosquitto 常用命令

mosquitto命令参数说明


1. -c  配置文件


从文件中加载配置参数 如果没有指定默认路径为/etc/mosquitto/mosquitto.conf


2. -d   表示以后台守护进程的形式启动




3. -p   指定监听端口 默认为1883


4. -v  监控日志  类似于在配置文件中把log_type 设置为all


mosquitto broker状态监控


客户端可以通过订阅$SYS层级的话题来监控broker的相关信息


1.$SYS/broker/bytes/received  收到的所有字节数


2.$SYS/broker/bytes/sent  发送的所有字节数


3.$SYS/broker/clients/connected 当前在线的客户端数


4.$SYS/broker/clients/expired  超时的连接数


5.$SYS/broker/clients/disconnected 断开的连接数


6.$SYS/broker/clients/maximum  最大并发连接数


7.$SYS/broker/clients/total  所有连接数(活动的和非活动的)


8.$SYS/broker/heap/current size  当前用到的内存


9.$SYS/broker/heap/maximum size  用到的最大内存


$SYS/broker/load/connections/+
The moving average of the number of CONNECT packets received by the broker over different time intervals. The final "+" of the hierarchy can be 1min, 5min or 15min. The value returned represents the number of connections received in 1 minute, averaged over 1, 5 or 15 minutes.


$SYS/broker/load/bytes/received/+
The moving average of the number of bytes received by the broker over different time intervals. The final "+" of the hierarchy can be 1min, 5min or 15min. The value returned represents the number of bytes received in 1 minute, averaged over 1, 5 or 15 minutes.


$SYS/broker/load/bytes/sent/+
The moving average of the number of bytes sent by the broker over different time intervals. The final "+" of the hierarchy can be 1min, 5min or 15min. The value returned represents the number of bytes sent in 1 minute, averaged over 1, 5 or 15 minutes.


$SYS/broker/load/messages/received/+
The moving average of the number of all types of MQTT messages received by the broker over different time intervals. The final "+" of the hierarchy can be 1min, 5min or 15min. The value returned represents the number of messages received in 1 minute, averaged over 1, 5 or 15 minutes.


$SYS/broker/load/messages/sent/+
The moving average of the number of all types of MQTT messages sent by the broker over different time intervals. The final "+" of the hierarchy can be 1min, 5min or 15min. The value returned represents the number of messages send in 1 minute, averaged over 1, 5 or 15 minutes.


$SYS/broker/load/publish/dropped/+
The moving average of the number of publish messages dropped by the broker over different time intervals. This shows the rate at which durable clients that are disconnected are losing messages. The final "+" of the hierarchy can be 1min, 5min or 15min. The value returned represents the number of messages dropped in 1 minute, averaged over 1, 5 or 15 minutes.


$SYS/broker/load/publish/received/+
The moving average of the number of publish messages received by the broker over different time intervals. The final "+" of the hierarchy can be 1min, 5min or 15min. The value returned represents the number of publish messages received in 1 minute, averaged over 1, 5 or 15 minutes.


$SYS/broker/load/publish/sent/+
The moving average of the number of publish messages sent by the broker over different time intervals. The final "+" of the hierarchy can be 1min, 5min or 15min. The value returned represents the number of publish messages sent in 1 minute, averaged over 1, 5 or 15 minutes.


$SYS/broker/load/sockets/+
The moving average of the number of socket connections opened to the broker over different time intervals. The final "+" of the hierarchy can be 1min, 5min or 15min. The value returned represents the number of socket connections in 1 minute, averaged over 1, 5 or 15 minutes.


$SYS/broker/messages/inflight
The number of messages with QoS>0 that are awaiting acknowledgments.


$SYS/broker/messages/received
The total number of messages of any type received since the broker started.


$SYS/broker/messages/sent
The total number of messages of any type sent since the broker started.


$SYS/broker/messages/stored
The number of messages currently held in the message store. This includes retained messages and messages queued for durable clients.


$SYS/broker/publish/messages/dropped
The total number of publish messages that have been dropped due to inflight/queuing limits. See the max_inflight_messages and max_queued_messages options in mosquitto.conf(5) for more information.


$SYS/broker/publish/messages/received
The total number of PUBLISH messages received since the broker started.


$SYS/broker/publish/messages/sent
The total number of PUBLISH messages sent since the broker started.


$SYS/broker/retained messages/count
The total number of retained messages active on the broker.


$SYS/broker/subscriptions/count
The total number of subscriptions active on the broker.


$SYS/broker/timestamp
The timestamp at which this particular build of the broker was made. Static.


$SYS/broker/uptime
The amount of time in seconds the broker has been online.


$SYS/broker/version
The version of the broker. Static.


Wildcard Topic Subscriptions


In addition to allowing clients to subscribe to specific topics, mosquitto also allows the use of two wildcards in subscriptions. + is the wildcard used to match a single level of hierarchy. For example, for a topic of "a/b/c/d", the following example subscriptions will match:


a/b/c/d


+/b/c/d


a/+/c/d


a/+/+/d


+/+/+/+


The following subscriptions will not match:


a/b/c


b/+/c/d


+/+/+


The second wildcard is # and is used to match all subsequent levels of hierarchy. With a topic of "a/b/c/d", the following example subscriptions will match:


a/b/c/d


#


a/#


a/b/#


a/b/c/#


+/b/c/#


The $SYS hierarchy does not match a subscription of "#". If you want to observe the entire $SYS hierarchy, subscribe to $SYS/#.


Note that the wildcards must be only ever used on their own, so a subscription of "a/b+/c" is not valid use of a wildcard. The # wildcard must only ever be used as the final character of a subscription.


Bridges


Multiple brokers can be connected together with the bridging functionality. This is useful where it is desirable to share information between locations, but where not all of the information needs to be shared. An example could be where a number of users are running a broker to help record power usage and for a number of other reasons. The power usage could be shared through bridging all of the user brokers to a common broker, allowing the power usage of all users to be collected and compared. The other information would remain local to each broker.


For information on configuring bridges, see mosquitto.conf(5).


Signals


SIGHUP
Upon receiving the SIGHUP signal, mosquitto will attempt to reload configuration file data, assuming that the -c argument was provided when mosquitto was started. Not all configuration parameters can be reloaded without restarting. See mosquitto.conf(5) for details.


SIGUSR1
Upon receiving the SIGUSR1 signal, mosquitto will write the persistence database to disk. This signal is only acted upon if persistence is enabled.


SIGUSR2
The SIGUSR2 signal causes mosquitto to print out the current subscription tree, along with information about where retained messages exist. This is intended as a testing feature only and may be removed at any time.


Files


/etc/mosquitto/mosquitto.conf
Configuration file. See mosquitto.conf(5).


/var/lib/mosquitto/mosquitto.db
Persistent message data storage location if persist enabled.


/etc/hosts.allow, /etc/hosts.deny
显示相关的终端状态



mosquitto_sub -v -t \$SYS/#



查看Mqtt订阅者运行状况
mosquitto_sub -v -t \$SYS/#
或者细化为其中一个命令
mosquitto_sub -v -t ‘$SYS/broker/clients/active’
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐