config.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. .. _configuration:
  2. =============
  3. Configuration
  4. =============
  5. Configuration files are under 'etc/' folder, including:
  6. +-------------------+-----------------------------------+
  7. | File | Description |
  8. +-------------------+-----------------------------------+
  9. | etc/vm.args | Erlang VM Arguments |
  10. +-------------------+-----------------------------------+
  11. | etc/emqttd.config | emqttd broker Config |
  12. +-------------------+-----------------------------------+
  13. | etc/acl.config | ACL Config |
  14. +-------------------+-----------------------------------+
  15. | etc/clients.config| ClientId Authentication |
  16. +-------------------+-----------------------------------+
  17. | etc/rewrite.config| Rewrite Rules |
  18. +-------------------+-----------------------------------+
  19. | etc/ssl/* | SSL certificate and key files |
  20. +-------------------+-----------------------------------+
  21. -----------
  22. etc/vm.args
  23. -----------
  24. Configure and Optimize Erlang VM::
  25. ##-------------------------------------------------------------------------
  26. ## Name of the node
  27. ##-------------------------------------------------------------------------
  28. -name emqttd@127.0.0.1
  29. ## Cookie for distributed erlang
  30. -setcookie emqttdsecretcookie
  31. ##-------------------------------------------------------------------------
  32. ## Flags
  33. ##-------------------------------------------------------------------------
  34. ## Heartbeat management; auto-restarts VM if it dies or becomes unresponsive
  35. ## (Disabled by default..use with caution!)
  36. ##-heart
  37. -smp true
  38. ## Enable kernel poll and a few async threads
  39. +K true
  40. ## 12 threads/core.
  41. +A 48
  42. ## max process numbers
  43. +P 8192
  44. ## Sets the maximum number of simultaneously existing ports for this system
  45. +Q 8192
  46. ## max atom number
  47. ## +t
  48. ## Set the distribution buffer busy limit (dist_buf_busy_limit) in kilobytes.
  49. ## Valid range is 1-2097151. Default is 1024.
  50. ## +zdbbl 8192
  51. ## CPU Schedulers
  52. ## +sbt db
  53. ##-------------------------------------------------------------------------
  54. ## Env
  55. ##-------------------------------------------------------------------------
  56. ## Increase number of concurrent ports/sockets, deprecated in R17
  57. -env ERL_MAX_PORTS 8192
  58. -env ERTS_MAX_PORTS 8192
  59. -env ERL_MAX_ETS_TABLES 1024
  60. ## Tweak GC to run more often
  61. -env ERL_FULLSWEEP_AFTER 1000
  62. The two most important parameters in etc/vm.args:
  63. +-------+---------------------------------------------------------------------------+
  64. | +P | Max number of Erlang proccesses. A MQTT client consumes two proccesses. |
  65. | | The value should be larger than max_clients * 2 |
  66. +-------+---------------------------------------------------------------------------+
  67. | +Q | Max number of Erlang Ports. A MQTT client consumes one port. |
  68. | | The value should be larger than max_clients. |
  69. +-------+---------------------------------------------------------------------------+
  70. The name and cookie of Erlang Node should be configured when clustering::
  71. -name emqttd@host_or_ip
  72. ## Cookie for distributed erlang
  73. -setcookie emqttdsecretcookie
  74. -----------------
  75. etc/emqttd.config
  76. -----------------
  77. The main configuration file for emqttd broker.
  78. File Syntax
  79. -----------
  80. The config consists of a list of Erlang Applications and their environments.
  81. .. code:: erlang
  82. [{kernel, [
  83. {start_timer, true},
  84. {start_pg2, true}
  85. ]},
  86. {sasl, [
  87. {sasl_error_logger, {file, "log/emqttd_sasl.log"}}
  88. ]},
  89. ...
  90. {emqttd, [
  91. ...
  92. ]}
  93. ].
  94. The file adopts Erlang Term Syntax:
  95. 1. [ ]: List, seperated by comma
  96. 2. { }: Tuple, Usually {Env, Value}
  97. 3. % : comment
  98. Log Level and File
  99. ------------------
  100. Logger of emqttd broker is implemented by 'lager' application::
  101. {lager, [
  102. ...
  103. ]},
  104. Configure log handlers::
  105. {handlers, [
  106. {lager_console_backend, info},
  107. {lager_file_backend, [
  108. {formatter_config, [time, " ", pid, " [",severity,"] ", message, "\n"]},
  109. {file, "log/emqttd_info.log"},
  110. {level, info},
  111. {size, 104857600},
  112. {date, "$D0"},
  113. {count, 30}
  114. ]},
  115. {lager_file_backend, [
  116. {formatter_config, [time, " ", pid, " [",severity,"] ", message, "\n"]},
  117. {file, "log/emqttd_error.log"},
  118. {level, error},
  119. {size, 104857600},
  120. {date, "$D0"},
  121. {count, 30}
  122. ]}
  123. ]}
  124. emqttd Application
  125. ------------------
  126. The MQTT broker is implemented by erlang 'emqttd' application::
  127. {emqttd, [
  128. %% Authentication and Authorization
  129. {access, [
  130. ...
  131. ]},
  132. %% MQTT Protocol Options
  133. {mqtt, [
  134. ...
  135. ]},
  136. %% Broker Options
  137. {broker, [
  138. ...
  139. ]},
  140. %% Modules
  141. {modules, [
  142. ...
  143. ]},
  144. %% Plugins
  145. {plugins, [
  146. ...
  147. ]},
  148. %% Listeners
  149. {listeners, [
  150. ...
  151. ]},
  152. %% Erlang System Monitor
  153. {sysmon, [
  154. ]}
  155. ]}
  156. Authentication
  157. --------------
  158. emqttd消息服务器认证由一系列认证模块(module)或插件(plugin)提供,系统默认支持用户名、ClientID、LDAP、匿名(anonymouse)认证模块::
  159. %% Authetication. Anonymous Default
  160. {auth, [
  161. %% Authentication with username, password
  162. %% Add users: ./bin/emqttd_ctl users add Username Password
  163. %% {username, [{"test", "public"}]},
  164. %% Authentication with clientid
  165. % {clientid, [{password, no}, {file, "etc/clients.config"}]},
  166. %% Authentication with LDAP
  167. % {ldap, [
  168. % {servers, ["localhost"]},
  169. % {port, 389},
  170. % {timeout, 30},
  171. % {user_dn, "uid=$u,ou=People,dc=example,dc=com"},
  172. % {ssl, fasle},
  173. % {sslopts, [
  174. % {"certfile", "ssl.crt"},
  175. % {"keyfile", "ssl.key"}]}
  176. % ]},
  177. %% Allow all
  178. {anonymous, []}
  179. ]},
  180. 系统默认采用匿名认证(anonymous),通过删除注释可开启其他认证方式。同时开启的多个认证模块组成认证链::
  181. ---------------- ---------------- ------------
  182. Client --> | Username认证 | -ignore-> | ClientID认证 | -ignore-> | 匿名认证 |
  183. ---------------- ---------------- ------------
  184. | | |
  185. \|/ \|/ \|/
  186. allow | deny allow | deny allow | deny
  187. .. NOTE:: emqttd消息服务器还提供了MySQL、PostgreSQL、Redis、MongoDB认证插件,
  188. 认证插件加载后认证模块失效。
  189. 用户名密码认证
  190. ..............
  191. .. code:: erlang
  192. {username, [{test1, "passwd1"}, {test2, "passwd2"}]},
  193. 两种方式添加用户:
  194. 1. 直接在[]中明文配置默认用户::
  195. [{test1, "passwd1"}, {test2, "passwd2"}]
  196. 2. 通过'./bin/emqttd_ctl'管理命令行添加用户::
  197. $ ./bin/emqttd_ctl users add <Username> <Password>
  198. ClientID认证
  199. ............
  200. .. code:: erlang
  201. {clientid, [{password, no}, {file, "etc/clients.config"}]},
  202. etc/clients.config文件中添加ClientID::
  203. testclientid0
  204. testclientid1 127.0.0.1
  205. testclientid2 192.168.0.1/24
  206. LDAP认证
  207. ........
  208. .. code:: erlang
  209. {ldap, [
  210. {servers, ["localhost"]},
  211. {port, 389},
  212. {timeout, 30},
  213. {user_dn, "uid=$u,ou=People,dc=example,dc=com"},
  214. {ssl, fasle},
  215. {sslopts, [
  216. {"certfile", "ssl.crt"},
  217. {"keyfile", "ssl.key"}]}
  218. ]},
  219. 匿名认证
  220. ........
  221. 默认开启。允许任意客户端登录::
  222. {anonymous, []}
  223. access用户访问控制(ACL)
  224. -----------------------
  225. emqttd消息服务器支持基于etc/acl.config文件或MySQL、PostgreSQL插件的访问控制规则。
  226. 默认开启基于etc/acl.config文件的访问控制::
  227. %% ACL config
  228. {acl, [
  229. %% Internal ACL module
  230. {internal, [{file, "etc/acl.config"}, {nomatch, allow}]}
  231. ]}
  232. etc/acl.config访问控制规则定义::
  233. 允许|拒绝 用户|IP地址|ClientID 发布|订阅 主题列表
  234. etc/acl.config默认访问规则设置::
  235. {allow, {user, "dashboard"}, subscribe, ["$SYS/#"]}.
  236. {allow, {ipaddr, "127.0.0.1"}, pubsub, ["$SYS/#", "#"]}.
  237. {deny, all, subscribe, ["$SYS/#", {eq, "#"}]}.
  238. {allow, all}.
  239. .. NOTE:: 默认规则只允许本机用户订阅'$SYS/#'与'#'
  240. emqttd消息服务器接收到MQTT客户端发布(PUBLISH)或订阅(SUBSCRIBE)请求时,会逐条匹配ACL访问控制规则,
  241. 直到匹配成功返回allow或deny。
  242. MQTT报文(Packet)尺寸与ClientID长度限制
  243. --------------------------------------
  244. 'packet'段落设置最大报文尺寸、最大客户端ID长度::
  245. {packet, [
  246. %% ClientID长度, 默认1024
  247. {max_clientid_len, 1024},
  248. %% 最大报文长度,默认64K
  249. {max_packet_size, 65536}
  250. ]},
  251. MQTT客户端(Client)连接闲置时间
  252. ------------------------------
  253. 'client'段落设置客户端最大允许闲置时间(Socket连接建立,但未发送CONNECT报文)::
  254. {client, [
  255. %% 单位: 秒
  256. {idle_timeout, 10}
  257. ]},
  258. MQTT会话(Session)参数设置
  259. -------------------------
  260. 'session'段落设置MQTT会话参数::
  261. {session, [
  262. %% Max number of QoS 1 and 2 messages that can be “in flight” at one time.
  263. %% 0 means no limit
  264. {max_inflight, 100},
  265. %% Retry interval for redelivering QoS1/2 messages.
  266. {unack_retry_interval, 20},
  267. %% Awaiting PUBREL Timeout
  268. {await_rel_timeout, 20},
  269. %% Max Packets that Awaiting PUBREL, 0 means no limit
  270. {max_awaiting_rel, 0},
  271. %% Statistics Collection Interval(seconds)
  272. {collect_interval, 20},
  273. %% Expired after 2 days
  274. {expired_after, 48}
  275. ]},
  276. 会话参数详细说明:
  277. +----------------------+----------------------------------------------------------+
  278. | max_inflight | 飞行窗口。最大允许同时下发的Qos1/2报文数,0表示没有限制。|
  279. | | 窗口值越大,吞吐越高;窗口值越小,消息顺序越严格 |
  280. +----------------------+----------------------------------------------------------+
  281. | unack_retry_interval | 下发QoS1/2消息未收到PUBACK响应的重试间隔 |
  282. +----------------------+----------------------------------------------------------+
  283. | await_rel_timeout | 收到QoS2消息,等待PUBREL报文超时时间 |
  284. +----------------------+----------------------------------------------------------+
  285. | max_awaiting_rel | 最大等待PUBREL的QoS2报文数 |
  286. +----------------------+----------------------------------------------------------+
  287. | collect_interval | 采集会话统计数据间隔,默认0表示关闭统计 |
  288. +----------------------+----------------------------------------------------------+
  289. | expired_after | 持久会话到期时间,从客户端断开算起,单位:小时 |
  290. +----------------------+----------------------------------------------------------+
  291. MQTT会话消息队列(MQueue)设置
  292. ----------------------------
  293. emqttd消息服务器会话通过队列缓存Qos1/Qos2消息:
  294. 1. 持久会话(Session)的离线消息
  295. 2. 飞行窗口满而延迟下发的消息
  296. 队列参数设置::
  297. {queue, [
  298. %% simple | priority
  299. {type, simple},
  300. %% Topic Priority: 0~255, Default is 0
  301. %% {priority, [{"topic/1", 10}, {"topic/2", 8}]},
  302. %% Max queue length. Enqueued messages when persistent client disconnected,
  303. %% or inflight window is full.
  304. {max_length, infinity},
  305. %% Low-water mark of queued messages
  306. {low_watermark, 0.2},
  307. %% High-water mark of queued messages
  308. {high_watermark, 0.6},
  309. %% Queue Qos0 messages?
  310. {queue_qos0, true}
  311. ]}
  312. 队列参数说明:
  313. +----------------------+---------------------------------------------------+
  314. | type | 队列类型。simple: 简单队列,priority: 优先级队列 |
  315. +----------------------+---------------------------------------------------+
  316. | priority | 主题(Topic)队列优先级设置 |
  317. +----------------------+---------------------------------------------------+
  318. | max_length | 队列长度, infinity表示不限制 |
  319. +----------------------+---------------------------------------------------+
  320. | low_watermark | 解除告警水位线 |
  321. +----------------------+---------------------------------------------------+
  322. | high_watermark | 队列满告警水位线 |
  323. +----------------------+---------------------------------------------------+
  324. | queue_qos0 | 是否缓存QoS0消息 |
  325. +----------------------+---------------------------------------------------+
  326. broker消息服务器参数
  327. --------------------
  328. 'broker'段落设置消息服务器内部模块参数。
  329. sys_interval设置系统发布$SYS消息周期::
  330. {sys_interval, 60},
  331. broker retained消息设置
  332. -----------------------
  333. retained设置MQTT retain消息处理参数::
  334. {retained, [
  335. %% retain消息过期时间,单位: 秒
  336. {expired_after, 0},
  337. %% 最大retain消息数量
  338. {max_message_num, 100000},
  339. %% retain消息payload最大尺寸
  340. {max_playload_size, 65536}
  341. ]},
  342. +-----------------+-------------------------------------+
  343. | expired_after | Retained消息过期时间,0表示永不过期 |
  344. +-----------------+-------------------------------------+
  345. | max_message_num | 最大存储的Retained消息数量 |
  346. +-----------------+-------------------------------------+
  347. | max_packet_size | Retained消息payload最大允许尺寸 |
  348. +-----------------+-------------------------------------+
  349. broker pubsub路由设置
  350. -----------------------
  351. 发布/订阅(Pub/Sub)路由模块参数::
  352. {pubsub, [
  353. %% PubSub Erlang进程池
  354. {pool_size, 8},
  355. %% 订阅存储类型,ram: 内存, disc: 磁盘, false: 不保存
  356. {subscription, ram},
  357. %% 路由老化时间
  358. {route_aging, 5}
  359. ]},
  360. broker bridge桥接参数
  361. -----------------------
  362. 桥接参数设置::
  363. {bridge, [
  364. %% 最大缓存桥接消息数
  365. {max_queue_len, 10000},
  366. %% 桥接节点宕机检测周期,单位: 秒
  367. {ping_down_interval, 1}
  368. ]}
  369. modules扩展模块设置
  370. -----------------------
  371. emqtt消息服务器支持简单的扩展模块,用于定制服务器功能。默认支持presence、subscription、rewrite模块。
  372. 'presence'扩展模块会向$SYS主题(Topic)发布客户端上下线消息::
  373. {presence, [{qos, 0}]},
  374. 'subscription'扩展模块支持客户端上线时,自动订阅或恢复订阅某些主题(Topic)::
  375. %% Subscribe topics automatically when client connected
  376. {subscription, [
  377. %% Subscription from stored table
  378. stored,
  379. %% $u will be replaced with username
  380. {"$Q/username/$u", 1},
  381. %% $c will be replaced with clientid
  382. {"$Q/client/$c", 1}
  383. ]}
  384. 'rewrite'扩展模块支持重写主题(Topic)路径, 重写规则定义在etc/rewrite.config文件::
  385. %% Rewrite rules
  386. %% {rewrite, [{file, "etc/rewrite.config"}]}
  387. 关于扩展模块详细介绍,请参考<用户指南>文档。
  388. plugins插件目录设置
  389. -------------------
  390. .. code:: erlang
  391. {plugins, [
  392. %% Plugin App Library Dir
  393. {plugins_dir, "./plugins"},
  394. %% File to store loaded plugin names.
  395. {loaded_file, "./data/loaded_plugins"}
  396. ]},
  397. listeners监听器设置
  398. -----------------------
  399. emqttd消息服务器开启的MQTT协议、HTTP协议服务端,可通过listener设置TCP服务端口、最大允许连接数等参数。
  400. emqttd消息服务器默认开启的TCP服务端口包括:
  401. +-----------+-----------------------------------+
  402. | 1883 | MQTT协议端口 |
  403. +-----------+-----------------------------------+
  404. | 8883 | MQTT(SSL)端口 |
  405. +-----------+-----------------------------------+
  406. | 8083 | MQTT(WebSocket), HTTP API端口 |
  407. +-----------+-----------------------------------+
  408. .. code:: erlang
  409. {listeners, [
  410. {mqtt, 1883, [
  411. %% Size of acceptor pool
  412. {acceptors, 16},
  413. %% Maximum number of concurrent clients
  414. {max_clients, 8192},
  415. %% Socket Access Control
  416. {access, [{allow, all}]},
  417. %% Connection Options
  418. {connopts, [
  419. %% Rate Limit. Format is 'burst, rate', Unit is KB/Sec
  420. %% {rate_limit, "100,10"} %% 100K burst, 10K rate
  421. ]},
  422. %% Socket Options
  423. {sockopts, [
  424. %Set buffer if hight thoughtput
  425. %{recbuf, 4096},
  426. %{sndbuf, 4096},
  427. %{buffer, 4096},
  428. %{nodelay, true},
  429. {backlog, 1024}
  430. ]}
  431. ]},
  432. {mqtts, 8883, [
  433. %% Size of acceptor pool
  434. {acceptors, 4},
  435. %% Maximum number of concurrent clients
  436. {max_clients, 512},
  437. %% Socket Access Control
  438. {access, [{allow, all}]},
  439. %% SSL certificate and key files
  440. {ssl, [{certfile, "etc/ssl/ssl.crt"},
  441. {keyfile, "etc/ssl/ssl.key"}]},
  442. %% Socket Options
  443. {sockopts, [
  444. {backlog, 1024}
  445. %{buffer, 4096},
  446. ]}
  447. ]},
  448. %% WebSocket over HTTPS Listener
  449. %% {https, 8083, [
  450. %% %% Size of acceptor pool
  451. %% {acceptors, 4},
  452. %% %% Maximum number of concurrent clients
  453. %% {max_clients, 512},
  454. %% %% Socket Access Control
  455. %% {access, [{allow, all}]},
  456. %% %% SSL certificate and key files
  457. %% {ssl, [{certfile, "etc/ssl/ssl.crt"},
  458. %% {keyfile, "etc/ssl/ssl.key"}]},
  459. %% %% Socket Options
  460. %% {sockopts, [
  461. %% %{buffer, 4096},
  462. %% {backlog, 1024}
  463. %% ]}
  464. %%]},
  465. %% HTTP and WebSocket Listener
  466. {http, 8083, [
  467. %% Size of acceptor pool
  468. {acceptors, 4},
  469. %% Maximum number of concurrent clients
  470. {max_clients, 64},
  471. %% Socket Access Control
  472. {access, [{allow, all}]},
  473. %% Socket Options
  474. {sockopts, [
  475. {backlog, 1024}
  476. %{buffer, 4096},
  477. ]}
  478. ]}
  479. ]},
  480. listener参数说明:
  481. +-------------+-----------------------------------------------------------+
  482. | acceptors | TCP Acceptor池 |
  483. +-------------+-----------------------------------------------------------+
  484. | max_clients | 最大允许TCP连接数 |
  485. +-------------+-----------------------------------------------------------+
  486. | access | 允许访问的IP地址段设置,例如: [{allow, "192.168.1.0/24"}] |
  487. +-------------+-----------------------------------------------------------+
  488. | connopts | 连接限速配置,例如限速10KB/秒: {rate_limit, "100,10"} |
  489. +-------------+-----------------------------------------------------------+
  490. | sockopts | Socket参数设置 |
  491. +-------------+-----------------------------------------------------------+
  492. .. _config_acl:
  493. --------------
  494. etc/acl.config
  495. --------------
  496. emqttd消息服务器默认访问控制规则配置在etc/acl.config文件。
  497. 访问控制规则采用Erlang元组格式,访问控制模块逐条匹配规则::
  498. --------- --------- ---------
  499. Client -> | Rule1 | --nomatch--> | Rule2 | --nomatch--> | Rule3 | --> Default
  500. --------- --------- ---------
  501. | | |
  502. match match match
  503. \|/ \|/ \|/
  504. allow | deny allow | deny allow | deny
  505. etc/acl.config文件默认规则设置::
  506. %% 允许'dashboard'用户订阅 '$SYS/#'
  507. {allow, {user, "dashboard"}, subscribe, ["$SYS/#"]}.
  508. %% 允许本机用户发布订阅全部主题
  509. {allow, {ipaddr, "127.0.0.1"}, pubsub, ["$SYS/#", "#"]}.
  510. %% 拒绝用户订阅'$SYS#'与'#'主题
  511. {deny, all, subscribe, ["$SYS/#", {eq, "#"}]}.
  512. %% 上述规则无匹配,允许
  513. {allow, all}.
  514. .. _config_rewrite:
  515. ------------------
  516. etc/rewrite.config
  517. ------------------
  518. Rewrite扩展模块的规则配置文件,示例配置::
  519. {topic, "x/#", [
  520. {rewrite, "^x/y/(.+)$", "z/y/$1"},
  521. {rewrite, "^x/(.+)$", "y/$1"}
  522. ]}.
  523. {topic, "y/+/z/#", [
  524. {rewrite, "^y/(.+)/z/(.+)$", "y/z/$2"}
  525. ]}.