An article takes you to get started with MQTT message queue telemetry transmission

An article takes you to get started with MQTT message queue telemetry transmission
Author | Adon
source | IT's Jiege trip (ID: Jake_Internet)
reprint please contact an authorized (micro letter ID: Hc220066)
the MQTT entry
concept
mqtt Message Queue Telemetry Transport means, is an instant messaging protocol developed by IBM. Because it maintains a long connection and is known for its lightweight and low consumption, it is often used in the development of mobile message push services.
Protocol format The format of the
mqtt protocol control message consists of three parts:

With fixed header, variable header, and payload, the fixed header is common to all control messages, and the variable header and payload are part of the control message.

mqtt is a binary protocol, and the control field is accurate to the Bit level. This alone is enough to occupy a place in the Internet of Things field. mqtt does not support mechanisms such as sub-packaging, and is not suitable for some application scenarios with particularly large data packets.

The mqtt feature
uses the publish/subscribe message model to provide one-to-many message publishing;

Message transmission that shields the content of the load;

Use TCP/IP for network connection;

There are three message publishing service quality options:

1. "At most once", this mode is usually used for app push, that is, if the mobile device is not connected to the Internet when the message is pushed, then it will not receive a notification if it is connected again;

2. "At least once", you can ensure that the message is received, but the message may be repeated;

3. "Only once", to ensure that the message arrives once, such as the billing system, if the message is repeated or lost, the system result will be incorrect.

Small-scale transmission, low overhead, and minimized protocol exchange to reduce network traffic;

The mechanism for notifying the parties concerned about abnormal client interruption.

mqtt protocol implementation


发布者----发布消息---->代理-------推送消息----->订阅者
发布者----发布消息---->代理<-----订阅消息-------订阅者

There are three identities in the mqtt protocol:

Publisher (publish): The publisher is a client and can publish messages

Broker: Broker refers to the server, the more famous one is emqtt, currently other mature frameworks can be used to build mqtt services

Subscriber (subscribe): refers to the client, but the publisher can also be a subscriber.

Functions implemented by the mqtt server and the client. The
mqtt server
accepts network connections from the client;

Accept application information released by customers;

Handle topic subscription and unsubscription requests from the client;

Forward application messages to subscribed clients

The mqtt client
publishes subscribed messages to other clients;

Subscribe to messages published by other clients;

Unsubscribe and subscribe to topics;

Disconnect the server

The method
connect in the mqtt protocol : wait for the server to establish a connection;

disconnect: Wait for the client to complete the work done and disconnect the TCP/IP session with the server;

subscribe: topic subscription;

unsubscribe: topic unsubscribe;

publish: send a message

mqtt installation

# sudo yum install epel-release
# sudo yum install mosquitto mosquitto-clients
# sudo systemctl start mosquitto
mqtt默认是以1883端口运行的

mqtt simply uses mosquitto
's configuration file as /etc/mosquitto/mosquitto.conf/
1. Add password configuration and do not allow anonymous users to log in

# sudo vim /etc/mosquitto/mosquitto.conf
allow_anonymous false  #不允许匿名登录
password_file /etc/mosquitto/pwfile  #配置用户密码文件
acl_file /etc/mosquitto/aclfile  # 配置topic和用户

2.添加用户信息
# mosquitto_passwd -c /etc/mosquitto/pwfile ceshi
# mosquitto_passwd /etc/mosquitto/pwfile ceshi2
分别添加用户ceshi和ceshi2

3.添加topic和用户的关系(权限配置)
# sudo vim /etc/mosquitto/aclfile
# ceshi只能发布V222为前缀的主题,订阅V333开头的主题
user ceshi
topic write V222/#
topic read V333/#
# ceshi2只能订阅以V222为前缀的主题
user ceshi2
topic read V222/#

- write:发布订阅
- read:接受订阅

4.启动
-c :指定配置文件启动
-d: 后台运行
mosquitto -c /etc/mosquitto/mosquitto.conf -d 

5.测试
发布订阅:mosquitto_pub
接受订阅:mosquitto_sub
参数:
-h :服务器主机
-t :指定主题
-u :用户名
-P : 密码
-i :客户端id
-m :发布的消息内容

# mosquitto_sub -h localhost -t "V222" -u ceshi2 -P 123456

# mosquitto_pub -h localhost -t "V222" -m "Hello world" -u ceshi -P 123455

Configuration file analysis

# 系统状态的刷新时间
# 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

# 服务绑定的IP地址
# bind_address

# 服务绑定的端口
# port 1883

# 消息自动保存的间隔时间
# autosave_interval 1800

# 消息自动保存功能的开关
# autosave_on_changes false

# 持久化功能的开关
# persistence true

# 持久化DB文件
# persistence_file mosquitto.db

# 持久化DB文件目录
# persistence_location /var/lib/mosquitto/

# 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

# 允许匿名用户
# allow_anonymous false

# 用户/密码文件,默认格式为:user/passwd
# password_file

/etc/mosquitto/passwd

Guess you like

Origin blog.51cto.com/15067236/2606324