config.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. .. _configuration:
  2. =============
  3. Configuration
  4. =============
  5. Configuration files of the broker 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. Bridge Parameters
  361. -----------------
  362. {bridge, [
  363. %% 最大缓存桥接消息数
  364. {max_queue_len, 10000},
  365. %% 桥接节点宕机检测周期,单位: 秒
  366. {ping_down_interval, 1}
  367. ]}
  368. Enable Modules
  369. --------------
  370. 'presence' module will publish presence message to $SYS topic when a client connected or disconnected::
  371. {presence, [{qos, 0}]},
  372. 'subscription' module forces the client to subscribe some topics when connected to the broker::
  373. %% Subscribe topics automatically when client connected
  374. {subscription, [
  375. %% Subscription from stored table
  376. stored,
  377. %% $u will be replaced with username
  378. {"$Q/username/$u", 1},
  379. %% $c will be replaced with clientid
  380. {"$Q/client/$c", 1}
  381. ]}
  382. 'rewrite' module supports to rewrite the topic path::
  383. %% Rewrite rules
  384. {rewrite, [{file, "etc/rewrite.config"}]}
  385. Plugins Folder
  386. --------------
  387. .. code:: erlang
  388. {plugins, [
  389. %% Plugin App Library Dir
  390. {plugins_dir, "./plugins"},
  391. %% File to store loaded plugin names.
  392. {loaded_file, "./data/loaded_plugins"}
  393. ]},
  394. TCP Listeners
  395. -------------
  396. Congfigure the TCP listener for MQTT, MQTT(SSL) and HTTP Protocols.
  397. The most important parameter is 'max_clients' - max concurrent clients allowed.
  398. The TCP Ports occupied by emqttd broker by default:
  399. +-----------+-----------------------------------+
  400. | 1883 | MQTT Port |
  401. +-----------+-----------------------------------+
  402. | 8883 | MQTT(SSL) Port |
  403. +-----------+-----------------------------------+
  404. | 8083 | MQTT(WebSocket), HTTP API Port |
  405. +-----------+-----------------------------------+
  406. .. code:: erlang
  407. {listeners, [
  408. {mqtt, 1883, [
  409. %% Size of acceptor pool
  410. {acceptors, 16},
  411. %% Maximum number of concurrent clients
  412. {max_clients, 8192},
  413. %% Socket Access Control
  414. {access, [{allow, all}]},
  415. %% Connection Options
  416. {connopts, [
  417. %% Rate Limit. Format is 'burst, rate', Unit is KB/Sec
  418. %% {rate_limit, "100,10"} %% 100K burst, 10K rate
  419. ]},
  420. %% Socket Options
  421. {sockopts, [
  422. %Set buffer if hight thoughtput
  423. %{recbuf, 4096},
  424. %{sndbuf, 4096},
  425. %{buffer, 4096},
  426. %{nodelay, true},
  427. {backlog, 1024}
  428. ]}
  429. ]},
  430. {mqtts, 8883, [
  431. %% Size of acceptor pool
  432. {acceptors, 4},
  433. %% Maximum number of concurrent clients
  434. {max_clients, 512},
  435. %% Socket Access Control
  436. {access, [{allow, all}]},
  437. %% SSL certificate and key files
  438. {ssl, [{certfile, "etc/ssl/ssl.crt"},
  439. {keyfile, "etc/ssl/ssl.key"}]},
  440. %% Socket Options
  441. {sockopts, [
  442. {backlog, 1024}
  443. %{buffer, 4096},
  444. ]}
  445. ]},
  446. %% WebSocket over HTTPS Listener
  447. %% {https, 8083, [
  448. %% %% Size of acceptor pool
  449. %% {acceptors, 4},
  450. %% %% Maximum number of concurrent clients
  451. %% {max_clients, 512},
  452. %% %% Socket Access Control
  453. %% {access, [{allow, all}]},
  454. %% %% SSL certificate and key files
  455. %% {ssl, [{certfile, "etc/ssl/ssl.crt"},
  456. %% {keyfile, "etc/ssl/ssl.key"}]},
  457. %% %% Socket Options
  458. %% {sockopts, [
  459. %% %{buffer, 4096},
  460. %% {backlog, 1024}
  461. %% ]}
  462. %%]},
  463. %% HTTP and WebSocket Listener
  464. {http, 8083, [
  465. %% Size of acceptor pool
  466. {acceptors, 4},
  467. %% Maximum number of concurrent clients
  468. {max_clients, 64},
  469. %% Socket Access Control
  470. {access, [{allow, all}]},
  471. %% Socket Options
  472. {sockopts, [
  473. {backlog, 1024}
  474. %{buffer, 4096},
  475. ]}
  476. ]}
  477. ]},
  478. Listener Parameters:
  479. +-------------+----------------------------------------------------------------+
  480. | acceptors | TCP Acceptor Pool |
  481. +-------------+----------------------------------------------------------------+
  482. | max_clients | Maximum number of concurrent TCP connections allowed |
  483. +-------------+----------------------------------------------------------------+
  484. | access | Access Control by IP, for example: [{allow, "192.168.1.0/24"}] |
  485. +-------------+----------------------------------------------------------------+
  486. | connopts | Rate Limit Control, for example: {rate_limit, "100,10"} |
  487. +-------------+----------------------------------------------------------------+
  488. | sockopts | TCP Socket parameters |
  489. +-------------+----------------------------------------------------------------+
  490. .. _config_acl:
  491. --------------
  492. etc/acl.config
  493. --------------
  494. The 'etc/acl.config' is the default ACL config for emqttd broker. The rules by default::
  495. %% Allow 'dashboard' to subscribe '$SYS/#'
  496. {allow, {user, "dashboard"}, subscribe, ["$SYS/#"]}.
  497. %% Allow clients from localhost to subscribe any topics
  498. {allow, {ipaddr, "127.0.0.1"}, pubsub, ["$SYS/#", "#"]}.
  499. %% Deny clients to subscribe '$SYS#' and '#'
  500. {deny, all, subscribe, ["$SYS/#", {eq, "#"}]}.
  501. %% Allow all by default
  502. {allow, all}.
  503. An ACL rule is an Erlang tuple. The Access control module of emqttd broker matches the rule one by one from top to bottom::
  504. --------- --------- ---------
  505. Client -> | Rule1 | --nomatch--> | Rule2 | --nomatch--> | Rule3 | --> Default
  506. --------- --------- ---------
  507. | | |
  508. match match match
  509. \|/ \|/ \|/
  510. allow | deny allow | deny allow | deny
  511. .. _config_rewrite:
  512. ------------------
  513. etc/rewrite.config
  514. ------------------
  515. The Rewrite Rules for emqttd_mod_rewrite::
  516. {topic, "x/#", [
  517. {rewrite, "^x/y/(.+)$", "z/y/$1"},
  518. {rewrite, "^x/(.+)$", "y/$1"}
  519. ]}.
  520. {topic, "y/+/z/#", [
  521. {rewrite, "^y/(.+)/z/(.+)$", "y/z/$2"}
  522. ]}.