一、获取、安装
Mosquitto提供了Windows、Linux以及qnx系统的版本,安装文件可从
http://mosquitto.org/files/binary/获取。Windows系统下的安装过程非常简单,甚至可以把Mosquitto直接安装成为系统服务;但是,在实际应用中更倾向于使用Linux系统的服务器。在Linux系统上安装Mosquitto,建议使用源码安装模式,最新的源码可从
http://mosquitto.org/files/source/获取。解压之后,可以在源码目录里面找到主要的配置文件,其中包含了所有Mosquitto的安装选项,详细的参数说明如下:
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,则需要关闭里面的某些与SSL功能有关的选项(WITH_TLS、WITH_TLS_PSK)。接着,就是运行make install进行安装,完成之后会在系统命令行里发现mosquitto、mosquitto_passwd、mosquitto_pub和 mosquitto_sub四个工具,分别用于启动代理、管理密码、发布消息和订阅消息。
二、配置、运行
安装完成之后,所有配置文件会被放置于/etc/mosquitto/目录下,其中最重要的就是Mosquitto的配置文件,即mosquitto.conf,以下是详细的配置参数说明。
1. # =================================================================
2. # General configuration
3. # =================================================================
4.
5. # 客户端心跳的间隔时间
6. #retry_interval 20
7.
8. # 系统状态的刷新时间
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. # 客户端心跳消息的最大并发数
21. #max_inflight_messages 10
22.
23. # 客户端心跳消息缓存队列
24. #max_queued_messages 100
25.
26. # 用于设置客户端长连接的过期时间,默认永不过期
27. #persistent_client_expiration
28.
29. # =================================================================
30. # Default listener
31. # =================================================================
32.
33. # 服务绑定的IP地址
34. #bind_address
35.
36. # 服务绑定的端口号
37. #port 1883
38.
39. # 允许的最大连接数,-1表示没有限制
40. #max_connections -1
41.
42. # cafile:CA证书文件
43. # capath:CA证书目录
44. # certfile:PEM证书文件
45. # keyfile:PEM密钥文件
46. #cafile
47. #capath
48. #certfile
49. #keyfile
50.
51. # 必须提供证书以保证数据安全性
52. #require_certificate false
53.
54. # 若require_certificate值为true,use_identity_as_username也必须为true
55. #use_identity_as_username false
56.
57. # 启用PSK(Pre-shared-key)支持
58. #psk_hint
59.
60. # SSL/TSL加密算法,可以使用“openssl ciphers”命令获取
61. # as the output of that command.
62. #ciphers
63.
64. # =================================================================
65. # Persistence
66. # =================================================================
67.
68. # 消息自动保存的间隔时间
69. #autosave_interval 1800
70.
71. # 消息自动保存功能的开关
72. #autosave_on_changes false
73.
74. # 持久化功能的开关
75. persistence true
76.
77. # 持久化DB文件
78. #persistence_file mosquitto.db
79.
80. # 持久化DB文件目录
81. #persistence_location /var/lib/mosquitto/
82.
83. # =================================================================
84. # Logging
85. # =================================================================
86.
87. # 4种日志模式:stdout、stderr、syslog、topic
88. # none 则表示不记日志,此配置可以提升些许性能
89. log_dest none
90.
91. # 选择日志的级别(可设置多项)
92. #log_type error
93. #log_type warning
94. #log_type notice
95. #log_type information
96.
97. # 是否记录客户端连接信息
98. #connection_messages true
99.
100. # 是否记录日志时间
101. #log_timestamp true
102.
103. # =================================================================
104. # Security
105. # =================================================================
106.
107. # 客户端ID的前缀限制,可用于保证安全性
108. #clientid_prefixes
109.
110. # 允许匿名用户
111. #allow_anonymous true
112.
113. # 用户/密码文件,默认格式:username:password
114. #password_file
115.
116. # PSK格式密码文件,默认格式:identity:key
117. #psk_file
118.
119. # pattern write sensor/%u/data
120. # ACL权限配置,常用语法如下:
121. # 用户限制:user <username>
122. # 话题限制:topic [read|write] <topic>
123. # 正则限制:pattern write sensor/%u/data
124. #acl_file
125.
126. # =================================================================
127. # Bridges
128. # =================================================================
129.
130. # 允许服务之间使用“桥接”模式(可用于分布式部署)
131. #connection <name>
132. #address <host>[:<port>]
133. #topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]
134.
135. # 设置桥接的客户端ID
136. #clientid
137.
138. # 桥接断开时,是否清除远程服务器中的消息
139. #cleansession false
140.
141. # 是否发布桥接的状态信息
142. #notifications true
143.
144. # 设置桥接模式下,消息将会发布到的话题地址
145. # $SYS/broker/connection/<clientid>/state
146. #notification_topic
147.
148. # 设置桥接的keepalive数值
149. #keepalive_interval 60
150.
151. # 桥接模式,目前有三种:automatic、lazy、once
152. #start_type automatic
153.
154. # 桥接模式automatic的超时时间
155. #restart_timeout 30
156.
157. # 桥接模式lazy的超时时间
158. #idle_timeout 60
159.
160. # 桥接客户端的用户名
161. #username
162.
163. # 桥接客户端的密码
164. #password
165.
166. # bridge_cafile:桥接客户端的CA证书文件
167. # bridge_capath:桥接客户端的CA证书目录
168. # bridge_certfile:桥接客户端的PEM证书文件
169. # bridge_keyfile:桥接客户端的PEM密钥文件
170. #bridge_cafile
171. #bridge_capath
172. #bridge_certfile
173. #bridge_keyfile
174.
175. # 自己的配置可以放到以下目录中
176. include_dir /etc/mosquitto/conf.d 启动Mosquitto服务很简单,直接运行命令行“
mosquitto -c /etc/mosquitto/mosquitto.conf -d”即可。另外,Mosquitto是个纯异步IO框架,经测试可以轻松处理20000个以上的客户端连接。当然,实际的最大承载量还和业务的复杂度有比较大的关系。测试的时候不要忘记调整系统的最大连接数和栈大小,比如Linux上可用ulimit -n20000 -s512命令设置你需要的系统参数。
















