mosquitto笔记

MQTT是一套协议,mosquitto是其实现的开源库(常用的)。本文介绍mosquitto在ubuntu 16.04上的使用。

安装

安装服务端:

1
sudo apt-get install mosquitto

成功后,得到mosquitto和mosquitto_passwd两个可执行程序,同时创建/etc/mosquitto/目录,其下有配置文件和认证证书,本文暂未涉及。注意,安装成功后会自动启动,其命令为:

1
/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

安装客户端:

1
sudo apt-get install mosquitto-clients

成功后,得到mosquitto_pub和mosquitto_sub,其中,mosquitto_pub用于发布消息,mosquitto_sub用于订阅消息。

配置

/etc/mosquitto/mosquitto.conf 文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

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

要点:默认开启永久存储,指定日志文件,指定用户新加的配置文件目录,在/etc/mosquitto/conf.d/下,只要以.conf为后缀的都合法。
port.conf:

1
2
3
4
5
6
7
8
port 1883
listener 1884

#listener 1885
#protocol mqtt

#listener 9001
#protocol websockets

注:没有protocol者,默认为mqtt。

修改配置后需要重启:

1
/etc/init.d/mosquitto restart

使用

在同一台机器上测试。订阅消息:

1
2
$ mosquitto_sub -v -t "hello"
hello this is hello world # 收到的消息

-t: 订阅主题,此处为hello,只有发布的消息是hello主题才能接受到。如果希望订阅所有消息,使用“#”。

-v: 打印订阅消息,包括主题和消息体。

-d:如果希望看到更详细的调试信息,使用此选项。

发布消息:

1
2
3
4
5
$ mosquitto_pub -d -t "hello" -m "this is hello world"
Client mosqpub/12383-zhijun sending CONNECT
Client mosqpub/12383-zhijun received CONNACK
Client mosqpub/12383-zhijun sending PUBLISH (d0, q0, r0, m1, 'hello', ... (19 bytes))
Client mosqpub/12383-zhijun sending DISCONNECT

不同机器测试。订阅消息命令相同,发布消息命令如下:

1
2
3
4
5
$ mosquitto_pub -d -h 172.18.18.18 -t "hello" -m "this is hello world"
Client mosqpub/21072-u-master sending CONNECT
Client mosqpub/21072-u-master received CONNACK
Client mosqpub/21072-u-master sending PUBLISH (d0, q0, r0, m1, 'hello', ... (19 bytes))
Client mosqpub/21072-u-master sending DISCONNECT

-d:打印详细的调试信息。

-h: 指定要连接的MQTT服务器,此处为172.18.18.18。

-t: 订阅主题,此处为hello。

-m:消息体。

多端口测试

环境,两台机器,一台为mqtt服务,另一台为客户端,含订阅和发布。

服务端添加多端口:

1
2
3
vim /etc/mosquitto/conf.d/port.conf
port 1883
listener 1884

修改配置后需要重启:

1
/etc/init.d/mosquitto restart

或者手动启动:

1
/usr/sbin/mosquitto -d -c /etc/mosquitto/mosquitto.conf

另一台机器:
订阅消息:

1
mosquitto_sub -h 192.168.0.102 -p 1884 -t "hello"

发布消息:

1
mosquitto_pub -h 192.168.0.102 -p 1884 -t "hello" -m "this is hello world"

消息

主题层级分隔符 / :
用于分割主题层级,/分割后的主题,这是消息主题层级设计中很重要的符号。
如 aaa/bbb 和  aaa/bbb/ccc 和 aaa/bbb/ccc/ddd,这样的消息主题格式,是一个层层递进的关系,可通过多层通配符同时匹配两者,或者单层通配符只匹配一个。
这在现实场景中,可以应用到:公司的部门层级推送、国家城市层级推送等包含层级关系的场景。

单层通配符 +:
单层通配符只能匹配一层主题。如:aa/+ 可以匹配 aaa/bbb ,但是不能匹配aaa/bbb/ccc。单独的+号可以匹配单层的所有推送。$hw/events/device/+/twin/可以匹配$hw/events/device/aaaaa/twin/$hw/events/device/bbbbb/twin/,等等。

多层通配符 #:
多层通配符可以匹配于多层主题。比如: aaa/# 不但可以匹配aaa/bbb,还可以匹配aaa/bbb/ccc/ddd。 也就是说,多层通配符可以匹配符合通配符之前主题层级的所有子集主题。单独的#匹配所有的消息主题.
单层通配符和多层通配符只能用于订阅(subscribe)消息而不能用于发布(publish)消息,层级分隔符两种情况下均可使用。

资料

websocket连接测试页面:
http://www.hivemq.com/demos/websocket-client/

MQTT协议介绍:
http://shaocheng.li/post/blog/2015-08-11

MQTT测试页面:
http://test.mosquitto.org/

订阅所有消息:

1
mosquitto_sub -h test.mosquitto.org -t "#" -v

小结

MQTT协议目前还没有理解得很透,网上例子非常简单,感受不出其优势,但很多资料都说这个协议很好,暂时没有体会到。
2020.3记:KubeEdge 使用 MQTT。