Mosquitto服务的安装和使用

本文的主旨在于记录Mosquitto服务的安装和使用,以备日后查阅。

1、获取&安装

Mosquitto提供了Windows、Linux以及qnx系统的版本,安装文件可从 http://mosquitto.org/files/binary/ 地址中获取(建议使用最新的1.1.x版本)。Windows系统下的安装过程非常简单,我们甚至可以把Mosquitto直接安装成为系统服务;但是,在实际应用中,我们更倾向于使用Linux系统的服务器,接下来我们就将重点介绍Linux版Mosquitto的安装方法。

在Linux系统上安装Mosquitto,本人建议大家使用源码安装模式,最新的源码可从 http://mosquitto.org/files/source/ 地址中获取。解压之后,我们可以在源码目录里面找到主要的配置文件config.mk,其中包含了所有Mosquitto的安装选项,详细的参数说明如下:
[html]  view plain copy
  1. # 是否支持tcpd/libwrap功能.  
  2. #WITH_WRAP:=yes  
  3.   
  4. # 是否开启SSL/TLS支持  
  5. #WITH_TLS:=yes  
  6.   
  7. # 是否开启TLS/PSK支持  
  8. #WITH_TLS_PSK:=yes  
  9.   
  10. # Comment out to disable client client threading support.  
  11. #WITH_THREADING:=yes  
  12.   
  13. # 是否使用严格的协议版本(老版本兼容会有点问题)  
  14. #WITH_STRICT_PROTOCOL:=yes  
  15.   
  16. # 是否开启桥接模式  
  17. #WITH_BRIDGE:=yes  
  18.   
  19. # 是否开启持久化功能  
  20. #WITH_PERSISTENCE:=yes  
  21.   
  22. # 是否监控运行状态  
  23. #WITH_MEMORY_TRACKING:=yes  
这里需要注意的是,默认情况下Mosquitto的安装需要OpenSSL的支持;如果不需要SSL,则需要关闭config.mk里面的某些与SSL功能有关的选项(WITH_TLS、WITH_TLS_PSK)。接着,就是运行make install进行安装,完成之后会在系统命令行里发现mosquitto、mosquitto_passwd、mosquitto_pub和mosquitto_sub四个工具(截图如下),分别用于启动代理、管理密码、发布消息和订阅消息。



2、配置&运行

安装完成之后,所有配置文件会被放置于/etc/mosquitto/目录下,其中最重要的就是Mosquitto的配置文件,即mosquitto.conf,以下是详细的配置参数说明。
[html]  view plain copy
  1. # =================================================================  
  2. # General configuration  
  3. # =================================================================  
  4.   
  5. 当QoS为1或2的消息已经被发送后,mosquitto在一段时间内仍未接收到客户端的反馈消息,将重新发送消息。 默认为20秒  
  6. #retry_interval 20  
  7.   
  8. 每隔一段时间将更新$SYS层级话题的状态,其中包含着proker的状态信息。 默认为10秒
  9. #sys_interval 10  
  10.   
  11. # 系统资源的回收时间,0表示尽快处理  
  12. #store_clean_interval 10  
  13.   
  14. # 服务进程的PID  
  15. #pid_file /var/run/mosquitto.pid  
  16.   
  17. # 服务进程的系统用户  
  18. #user mosquitto  
  19.   
  20. 表示允许多大数量的QoS为1或2消息被同时进行传输处理。这些消息包括正在进行握手的消息和进行重新发送的消息。默认为20个,
    如果设置为0,表示不设限制;如果为1,则会确保消息被顺序处理。
  21. #max_inflight_messages 10  
  22.   
  23. 表示允许多大数量的QoS为1或2消息在队列中进行排队。 默认为100个
  24. #max_queued_messages 100  
  25.  
  26. #设置最大的连接数  -1表示不限制 
  27. #max_connections

  28. # 用于设置客户端长连接的过期时间,默认永不过期  
  29. #persistent_client_expiration  
  30.   
  31. # =================================================================  
  32. # Default listener  
  33. # =================================================================  
  34.   
  35. # 服务绑定的IP地址  
  36. #bind_address  
  37.   
  38. # 服务绑定的端口号  
  39. #port 1883  
  40.   
  41. # 允许的最大连接数,-1表示没有限制  
  42. #max_connections -1  
  43.   
  44. # cafile:CA证书文件  
  45. # capath:CA证书目录  
  46. # certfile:PEM证书文件  
  47. # keyfile:PEM密钥文件  
  48. #cafile  
  49. #capath  
  50. #certfile  
  51. #keyfile  
  52.   
  53. # 必须提供证书以保证数据安全性  
  54. #require_certificate false  
  55.   
  56. # 若require_certificate值为true,use_identity_as_username也必须为true  
  57. #use_identity_as_username false  
  58.   
  59. # 启用PSK(Pre-shared-key)支持  
  60. #psk_hint  
  61.   
  62. # SSL/TSL加密算法,可以使用“openssl ciphers”命令获取  
  63. # as the output of that command.  
  64. #ciphers  
  65.   
  66. # =================================================================  
  67. # Persistence  
  68. # =================================================================  
  69.   
  70. # 消息自动保存的间隔时间  
  71. #autosave_interval 1800  
  72.   
  73. # 消息自动保存功能的开关  
  74. #autosave_on_changes false  
  75.   
  76. # 持久化功能的开关  
  77. persistence true  
  78.   
  79. # 持久化DB文件  
  80. #persistence_file mosquitto.db  
  81.   
  82. # 持久化DB文件目录  
  83. #persistence_location /var/lib/mosquitto/  
  84.   
  85. # =================================================================  
  86. # Logging  
  87. # =================================================================  
  88.   
  89. # 4种日志模式:stdout、stderr、syslog、topic  
  90. # none 则表示不记日志,此配置可以提升些许性能  
  91. log_dest none  
  92.   
  93. # 选择日志的级别(可设置多项)  
  94. #log_type error  
  95. #log_type warning  
  96. #log_type notice  
  97. #log_type information  
  98.   
  99. # 是否记录客户端连接信息  
  100. #connection_messages true  
  101.   
  102. # 是否记录日志时间  
  103. #log_timestamp true  
  104.   
  105. # =================================================================  
  106. # Security  
  107. # =================================================================  
  108.   
  109. # 客户端ID的前缀限制,可用于保证安全性  
  110. #clientid_prefixes  
  111.   
  112. # 允许匿名用户  
  113. #allow_anonymous true  
  114.   
  115. # 用户/密码文件,默认格式:username:password  
  116. #password_file  
  117.   
  118. # PSK格式密码文件,默认格式:identity:key  
  119. #psk_file  
  120.   
  121. # pattern write sensor/%u/data  
  122. # ACL权限配置,常用语法如下:  
  123. # 用户限制:user <username>  
  124. # 话题限制:topic [read|write] <topic>  
  125. # 正则限制:pattern write sensor/%u/data  
  126. #acl_file  
  127.   
  128. # =================================================================  
  129. # Bridges  
  130. # =================================================================  
  131.   
  132. # 允许服务之间使用“桥接”模式(可用于分布式部署)  
  133. #connection <name>  
  134. #address <host>[:<port>]  
  135. #topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]  
  136.   
  137. # 设置桥接的客户端ID  
  138. #clientid  
  139.   
  140. # 桥接断开时,是否清除远程服务器中的消息  
  141. #cleansession false  
  142.   
  143. # 是否发布桥接的状态信息  
  144. #notifications true  
  145.   
  146. # 设置桥接模式下,消息将会发布到的话题地址  
  147. # $SYS/broker/connection/<clientid>/state  
  148. #notification_topic  
  149.   
  150. # 设置桥接的keepalive数值  
  151. #keepalive_interval 60  
  152.   
  153. # 桥接模式,目前有三种:automatic、lazy、once  
  154. #start_type automatic  
  155.   
  156. # 桥接模式automatic的超时时间  
  157. #restart_timeout 30  
  158.   
  159. # 桥接模式lazy的超时时间  
  160. #idle_timeout 60  
  161.   
  162. # 桥接客户端的用户名  
  163. #username  
  164.   
  165. # 桥接客户端的密码  
  166. #password  
  167.   
  168. # bridge_cafile:桥接客户端的CA证书文件  
  169. # bridge_capath:桥接客户端的CA证书目录  
  170. # bridge_certfile:桥接客户端的PEM证书文件  
  171. # bridge_keyfile:桥接客户端的PEM密钥文件  
  172. #bridge_cafile  
  173. #bridge_capath  
  174. #bridge_certfile  
  175. #bridge_keyfile  
  176.   
  177. # 自己的配置可以放到以下目录中  
  178. include_dir /etc/mosquitto/conf.d  

再加一份说明:

1.retry_interval 
当QoS为1或2的消息已经被发送后,mosquitto在一段时间内仍未接收到客户端的反馈消息,将重新发送消息。 默认为20秒
2.sys_interval 
每隔一段时间将更新$SYS层级话题的状态,其中包含着proker的状态信息。 默认为10秒
3.store_clean_interval
表示间隔多长时间将不再被使用的消息销毁掉。该值越小,使用的内存就会越小但会需要更多的处理时间。 如果设置为0,表示不被
使用的消息将会及时销毁。 默认为10秒
4. pid_file  默认为/var/run/mosquitto.pid
5.user   设置mosquitto启动用户
6.max_inflight_messages 
表示允许多大数量的QoS为1或2消息被同时进行传输处理。这些消息包括正在进行握手的消息和进行重新发送的消息。默认为20个,
如果设置为0,表示不设限制;如果为1,则会确保消息被顺序处理。
7.max_queued_messages
表示允许多大数量的QoS为1或2消息在队列中进行排队。 默认为100个
8.max_connections
设置最大的连接数  -1表示不限制
9.autosave_interval
表示当开启持久化设置时,间隔多少时间mosquitto会把内存中的消息保存到磁盘中。默认为30分钟,当设置为0时,只有mosquitto
关闭的时候才会写的磁盘中。
10.persistence
设置为true时,所有的连接,订阅和消息数据都会被保存到磁盘的mosquitto.db文件中。当mosquitto重启的时候,它会从mosquitto.db文件中
重新加载数据。
11.persistence_location  默认为/var/lib/mosquitto/
12.log_dest  
设置日志的输出目的地 可以是:stdout stderr syslog topic   如果输出到某个文件的话可以这样设置log_dest file /var/log/mosquitto.log  要赋予对mosquitto.log文件的读写权限。
13.log_type  日志类型:debug, error, warning, notice, information, subscribe, unsubscribe, websockets, none, all
14.log_timestamp   是否记录日志时间
15.clientid_prefixes
设置只有clientId以某个前缀开始的客户端才允许连接到mosquitto broker.
16.allow_duplicate_messages
如果一个客户端订阅了多个topic时,设置是否允许接收重复的消息。比如订阅了foo/# 和 foo/+/baz。
17.autosave_on_changes 
If true, mosquitto will count the number of subscription changes, retained messages received and queued messages and if the total exceeds autosave_interval then the in-memory database will be saved to disk. If false, mosquitto will save the in-memory database to disk by treating autosave_interval as a time in seconds
18.persistent_client_expiration
持久订阅的过期设置。 对于将clean session设置为false的持久订阅客户端,如果在一定的时间段里面没有重新连接mosquitto将会被移除。
这并不是一个标准的配置项,因为对于MQTT协议来说所有的持久订阅应该是永远有效的。
如: persistent_client_expiration 2m  persistent_client_expiration 14d persistent_client_expiration 1y 
h:小时  d:天  m:月  y:年
19.queue_qos0_messages 是否将QoS为0的消息计算到max_queued_messages参数中

最后,启动Mosquitto服务很简单,直接运行命令行“mosquitto -c /etc/mosquitto/mosquitto.conf -d”即可开启服务。接下来,就让我们尽情体验Mosquitto的强大功能吧!当然,有了Mosquitto,我们就可以安心地抛弃“简陋”的rsmb了,有兴趣的话,大家还可以尝试把Mosquitto服务运用到上一篇的Android推送服务中。


另外,Mosquitto是个异步IO框架,经测试可以轻松处理20000个以上的客户端连接。当然,实际的最大承载量还和业务的复杂度还有比较大的关系。下图是本人在一台普通Linux机器上进行的压力测试结果,大家可以参考。



友情提醒: 测试的时候不要忘记调整系统的最大连接数和栈大小,比如:Linux上可用ulimit -n20000 -s512命令设置你需要的系统参数。
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值