Initial mqtt Service

Getting MQTT

concept

mqtt Message Queue Telemetry Transport means, is an instant messaging protocol developed by IBM. Because of its long connection to maintain a lightweight low consumption known, so commonly used in the mobile end push messaging service development.

Protocol format

mqtt protocol control packet format consists of three parts:
a fixed header and a variable header and a payload, wherein the packet header is fixed for all control packets are of variable header and payload are part of the control packets.
mqtt is a binary protocol, the control field is accurate to Bit-level, this alone is enough for a place in the field of things. mqtt is not supported by subcontracting and other mechanisms, not suitable for a large number of packets particular application scenarios.

mqtt properties

  • Using a publish / subscribe messaging model, providing many news release;
  • Load the content of the message transmission mask;
  • Using TCP / IP network connection;
  • Quality of Service news release has three options:
  • 1. "at most once", usually app push using this model, that is, if the mobile device when no network news push, then the network again will not receive a notification;
  • 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 reaches once, such as billing systems, if the message is lost or cause the system to repeat the result is not the right questions.
  • Small transmission overhead is small, minimizing the protocol exchange, in order to reduce network traffic;
  • Notification mechanism parties concerned client aborted.

mqtt protocol implementation

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

There are three status in mqtt agreement:

  • Publisher (publish): A publisher is a client, you can post messages
  • Agent (broker): proxy server refers to the more famous is emqtt, the current can be other mature framework to build mqtt Service
  • Subscribers (subscribe): refers to the client, but the publisher but can also be subscribers.

mqtt server and client functions implemented

mqtt server
  • Receiving network connection from a client;
  • Customer acceptance of the application of information released;
  • Processing end theme subscribe and unsubscribe requests from clients;
  • Forwarding application messages to subscribed clients

    mqtt client
  • Other clients subscribe to the news release;
  • Subscribe to news published by other clients;
  • Subscribe and unsubscribe topics;
  • Disconnect from a server

Method mqtt protocol

  • connect: waiting for the server to establish a connection;
  • disconnect: waiting for the client to complete the work, and TCP / IP session with the server is disconnected;
  • subscribe: subscribe topic;
  • unsubscribe: unsubscribe theme;
  • publish: send a message

mqtt installation

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

mqtt simple to use

mosquitto的配置文件为/etc/mosquitto/mosquitto.conf/
1.添加密码配置并且不允许匿名用户登录
# 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 parsing
# 系统状态的刷新时间
# 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 www.cnblogs.com/l-mac/p/11486539.html