mqtt简介 -尊龙游戏旗舰厅官网
mq 遥测传输 (mqtt) 是轻量级基于代理的发布-订阅的消息传输协议,设计思想是开放、简单、轻量、易于实现。这些特点使它适用于受限环境。例如,但不仅限于此:
服务器的配置文件mosquitto.conf如下 安装完成之后,所有配置文件会被放置于/etc/mosquitto/目录下,其中最重要的就是mosquitto的配置文件,即mosquitto.conf,但文件夹里并没有mosquitto.conf,而是有一个mosquitto.conf.example,我们复制一下即可,为了服务器能够使用,我们需要做一些配置 # ================================================================= # general configuration # ================================================================= # 客户端心跳的间隔时间 #retry_interval 20 # 系统状态的刷新时间 #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 # ================================================================= # default listener # ================================================================= # 服务绑定的ip地址 #bind_address # 服务绑定的端口号 #port 1883 # 允许的最大连接数,-1表示没有限制 #max_connections -1 # cafile:ca证书文件 # capath:ca证书目录 # certfile:pem证书文件 # keyfile:pem密钥文件 # 必须提供证书以保证数据安全性 #require_certificate false # 若require_certificate值为true,use_identity_as_username也必须为true #use_identity_as_username false # 启用psk(pre-shared-key)支持 #psk_hint # ssl/tsl加密算法,可以使用“openssl ciphers”命令获取 # as the output of that command. #ciphers # ================================================================= # persistence # ================================================================= # 消息自动保存的间隔时间 #autosave_interval 1800 # 消息自动保存功能的开关 #autosave_on_changes false # 持久化功能的开关 persistence true # 持久化db文件 #persistence_file mosquitto.db # 持久化db文件目录 #persistence_location /var/lib/mosquitto/
# ================================================================= # logging # ================================================================= # 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 # ================================================================= # security # ================================================================= # 客户端id的前缀限制,可用于保证安全性 #clientid_prefixes # 允许匿名用户 #allow_anonymous true # 用户/密码文件,默认格式:username:password #password_file # psk格式密码文件,默认格式:identity:key #psk_file # pattern write sensor/%u/data # acl权限配置,常用语法如下: # 用户限制:user # 话题限制:topic [read|write] # 正则限制:pattern write sensor/%u/data #acl_file # ================================================================= # bridges # ================================================================= # 允许服务之间使用“桥接”模式(可用于分布式部署) #connection #address [:] #topic [[[out | in | both] qos-level] local-prefix remote-prefix] # 设置桥接的客户端id #clientid # 桥接断开时,是否清除远程服务器中的消息 #cleansession false # 是否发布桥接的状态信息 #notifications true # 设置桥接模式下,消息将会发布到的话题地址 # $sys/broker/connection//state #notification_topic # 设置桥接的keepalive数值 #keepalive_interval 60 # 桥接模式,目前有三种:automatic、lazy、once #start_type automatic # 桥接模式automatic的超时时间 #restart_timeout 30 # 桥接模式lazy的超时时间 #idle_timeout 60 # 桥接客户端的用户名 #username # 桥接客户端的密码 #password # bridge_cafile:桥接客户端的ca证书文件 # bridge_capath:桥接客户端的ca证书目录 # bridge_certfile:桥接客户端的pem证书文件 # bridge_keyfile:桥接客户端的pem密钥文件 # 自己的配置可以放到以下目录中 include_dir /etc/mosquitto/conf.d 一般我们只修改监听端口,carfile
可能遇到的一些问题
- 网络代价昂贵,带宽低、不可靠。
- 在嵌入设备中运行,处理器和内存资源有限。
- 使用发布/订阅消息模式,提供一对多的消息发布,解除应用程序耦合。
- 对负载内容屏蔽的消息传输。
- 使用 tcp/ip 提供网络连接。
- 小型传输,开销很小(固定长度的头部是 2 字节),协议交换最小化,以降低网络流量。
- 使用 last will 和 testament 特性通知有关各方客户端异常中断的机制。
- 有三种消息发布服务质量:
- “至多一次”,消息发布完全依赖底层 tcp/ip 网络。会发生消息丢失或重复。这一级别可用于如下情况,环境传感器数据,丢失一次读记录无所谓,因为不久后还会有第二次发送。
- “至少一次”,确保消息到达,但消息重复可能会发生。
- “只有一次”,确保消息到达一次。这一级别可用于如下情况,在计费系统中,消息重复或丢失会导致不正确的结果。
- bin: 与这个实例相关联的执行脚本
- etc: 包含实例的配置文件
- data: 用于存储持久消息的数据文件
- log: 日志文件
- tmp: 在代理间可以安全删除的临时文件
- 下载源代码包
- 解压
- 进入目录
- 编译
- 安装
服务器的配置文件mosquitto.conf如下 安装完成之后,所有配置文件会被放置于/etc/mosquitto/目录下,其中最重要的就是mosquitto的配置文件,即mosquitto.conf,但文件夹里并没有mosquitto.conf,而是有一个mosquitto.conf.example,我们复制一下即可,为了服务器能够使用,我们需要做一些配置 # ================================================================= # general configuration # ================================================================= # 客户端心跳的间隔时间 #retry_interval 20 # 系统状态的刷新时间 #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 # ================================================================= # default listener # ================================================================= # 服务绑定的ip地址 #bind_address # 服务绑定的端口号 #port 1883 # 允许的最大连接数,-1表示没有限制 #max_connections -1 # cafile:ca证书文件 # capath:ca证书目录 # certfile:pem证书文件 # keyfile:pem密钥文件 # 必须提供证书以保证数据安全性 #require_certificate false # 若require_certificate值为true,use_identity_as_username也必须为true #use_identity_as_username false # 启用psk(pre-shared-key)支持 #psk_hint # ssl/tsl加密算法,可以使用“openssl ciphers”命令获取 # as the output of that command. #ciphers # ================================================================= # persistence # ================================================================= # 消息自动保存的间隔时间 #autosave_interval 1800 # 消息自动保存功能的开关 #autosave_on_changes false # 持久化功能的开关 persistence true # 持久化db文件 #persistence_file mosquitto.db # 持久化db文件目录 #persistence_location /var/lib/mosquitto/
# ================================================================= # logging # ================================================================= # 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 # ================================================================= # security # ================================================================= # 客户端id的前缀限制,可用于保证安全性 #clientid_prefixes # 允许匿名用户 #allow_anonymous true # 用户/密码文件,默认格式:username:password #password_file # psk格式密码文件,默认格式:identity:key #psk_file # pattern write sensor/%u/data # acl权限配置,常用语法如下: # 用户限制:user
可能遇到的一些问题
- 编译找不到openssl/ssl.h
- 编译过程找不到ares.h
- 编译过程找不到uuid/uuid.h
- 使用过程中找不到libmosquitto.so.1
- make: g :命令未找到
- invalid user 'mosquitto'.
- address already in use
- 订阅主题,开启一个新的终端,输入如下
- 发布内容,开启一个新的终端,输入如下
未完待续
参考:
http://blog.csdn.net/yannanxiu/article/details/52703946 http://blog.csdn.net/djun100/article/details/25752491 http://blog.csdn.net/xukai871105/article/details/39252653 http://blog.csdn.net/shagoo/article/details/7910598 http://www.tuicool.com/articles/rjyuebj https://my.oschina.net/u/1396253/blog/175773 尊龙游戏旗舰厅官网:http://mqtt.org/documentation http://www.ibm.com/developerworks/websphere/library/techarticles/0508_oconnell/0508_oconnell.html总结
- 上一篇: asp.net 服务器控件的 id,cl
- 下一篇: 给apk签名