{"id":510,"date":"2026-01-27T02:36:12","date_gmt":"2026-01-27T02:36:12","guid":{"rendered":"https:\/\/blog.gpst.net.cn:4008\/?p=510"},"modified":"2026-01-28T06:24:12","modified_gmt":"2026-01-28T06:24:12","slug":"mqtt%ef%bc%88emqx%ef%bc%89%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%e4%b8%8e%e5%8e%8b%e5%8a%9b%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/opshub.com.cn\/?p=510","title":{"rendered":"mqtt\uff08emqx\uff09\u6027\u80fd\u4f18\u5316\u4e0e\u538b\u529b\u6d4b\u8bd5"},"content":{"rendered":"\n<p>\u4e00\u3001\u7cfb\u7edf\u8c03\u4f18<\/p>\n\n\n\n<p>1.\u5173\u95ed\u4ea4\u6362\u5206\u533a<\/p>\n\n\n\n<p>Linux \u4ea4\u6362\u5206\u533a\u53ef\u80fd\u4f1a\u5bfc\u81f4 Erlang \u865a\u62df\u673a\u51fa\u73b0\u4e0d\u786e\u5b9a\u7684\u5185\u5b58\u5ef6\u8fdf\uff0c\u4e25\u91cd\u5f71\u54cd\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u3002 \u5efa\u8bae\u6c38\u4e45\u5173\u95ed\u4ea4\u6362\u5206\u533a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>swapoff -a<\/code><\/pre>\n\n\n\n<p>\u5728 \/etc\/fstab \u6587\u4ef6\u4e2d\u6ce8\u91ca\u6389 swap \u884c\u3002<\/p>\n\n\n\n<p>2.linux\u64cd\u4f5c\u7cfb\u7edf\u53c2\u6570<\/p>\n\n\n\n<p>\u7cfb\u7edf\u5168\u5c40\u5141\u8bb8\u5206\u914d\u7684\u6700\u5927\u6587\u4ef6\u53e5\u67c4\u6570:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># 2 millions system-wide\nsysctl -w fs.file-max=2097152\nsysctl -w fs.nr_open=2097152\necho 2097152 &gt; \/proc\/sys\/fs\/nr_open<\/code><\/pre>\n\n\n\n<p>\u5141\u8bb8\u5f53\u524d\u4f1a\u8bdd \/ \u8fdb\u7a0b\u6253\u5f00\u6587\u4ef6\u53e5\u67c4\u6570:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ulimit -SHn 1048576<\/code><\/pre>\n\n\n\n<p>\/etc\/security\/limits.conf \u6301\u4e45\u5316\u8bbe\u7f6e\u5141\u8bb8\u7528\u6237 \/ \u8fdb\u7a0b\u6253\u5f00\u6587\u4ef6\u53e5\u67c4\u6570:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*      soft   nofile      1048576\n*      hard   nofile      1048576<\/code><\/pre>\n\n\n\n<p>\/etc\/systemd\/system.conf \u8bbe\u7f6e\u670d\u52a1\u6700\u5927\u6587\u4ef6\u53e5\u67c4\u6570:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DefaultLimitNOFILE=1048576<\/code><\/pre>\n\n\n\n<p>3.tcp\u534f\u8bae\u6808\u7f51\u7edc\u53c2\u6570<\/p>\n\n\n\n<p>optimize.sh\u811a\u672c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\nsysctl -w fs.file-max=2097152\nsysctl -w fs.nr_open=2097152\nsysctl -w net.ipv4.ip_forward=1 \n#\u5e76\u53d1\u8fde\u63a5 backlog \u8bbe\u7f6e: \nsysctl -w net.core.somaxconn=32768\nsysctl -w net.ipv4.tcp_max_syn_backlog=16384\nsysctl -w net.core.netdev_max_backlog=16384\n#\u53ef\u7528\u77e5\u540d\u7aef\u53e3\u8303\u56f4:\nsysctl -w net.ipv4.ip_local_port_range='1024 65535'\n#TCP Socket \u8bfb\u5199 Buffer \u8bbe\u7f6e:\nsysctl -w net.core.rmem_default=262144\nsysctl -w net.core.wmem_default=262144\nsysctl -w net.core.rmem_max=16777216\nsysctl -w net.core.wmem_max=16777216\nsysctl -w net.core.optmem_max=16777216   \nsysctl -w net.ipv4.tcp_rmem='1024 4096 16777216'\nsysctl -w net.ipv4.tcp_wmem='1024 4096 16777216'\n#TCP \u8fde\u63a5\u8ffd\u8e2a\u8bbe\u7f6e:\nsysctl -w net.nf_conntrack_max=1000000\nsysctl -w net.netfilter.nf_conntrack_max=1000000\nsysctl -w net.netfilter.nf_conntrack_tcp_timeout_time_wait=30\n#TIME-WAIT Socket \u6700\u5927\u6570\u91cf\u3001\u56de\u6536\u4e0e\u91cd\u7528\u8bbe\u7f6e:\nsysctl -w net.ipv4.tcp_max_tw_buckets=1048576\n#FIN-WAIT-2 Socket \u8d85\u65f6\u8bbe\u7f6e:\nsysctl -w net.ipv4.tcp_fin_timeout=15<\/code><\/pre>\n\n\n\n<p>\u4e8c\u3001erlang\u865a\u62df\u673a\u53c2\u6570<\/p>\n\n\n\n<p>\u4f18\u5316\u8bbe\u7f6e Erlang \u865a\u62df\u673a\u542f\u52a8\u53c2\u6570\uff0c\u914d\u7f6e\u6587\u4ef6 etc\/emqx.conf:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>## \u8bbe\u7f6e Erlang \u7cfb\u7edf\u540c\u65f6\u5b58\u5728\u7684\u6700\u5927\u7aef\u53e3\u6570\nnode.max_ports = 2097152<\/code><\/pre>\n\n\n\n<p>\u4e09\u3001emqx\u6d88\u606f\u670d\u52a1\u5668\u53c2\u6570<\/p>\n\n\n\n<p>\u8bbe\u7f6e TCP \u76d1\u542c\u5668\u7684 Acceptor \u6c60\u5927\u5c0f\uff0c\u6700\u5927\u5141\u8bb8\u8fde\u63a5\u6570\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0cTCP \u76d1\u542c\u5668\u53ef\u4f7f\u7528\u5982\u4e0b\u914d\u7f6e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>## TCP Listener\nlisteners.tcp.$name.acceptors = 64\nlisteners.tcp.$name.max_connections = 1024000<\/code><\/pre>\n\n\n\n<p>emqx.conf\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>## NOTE:\n## This config file overrides data\/configs\/cluster.hocon,\n## and is merged with environment variables which start with 'EMQX_' prefix.\n##\n## Config changes made from EMQX dashboard UI, management HTTP API, or CLI\n## are stored in data\/configs\/cluster.hocon.\n## To avoid confusion, please do not store the same configs in both files.\n##\n## See https:\/\/www.emqx.io\/docs\/en\/v5.0\/configuration\/configuration.html for more details.\n## Configuration full example can be found in etc\/examples\nnode {\n  name = \"emqx@127.0.0.1\"\n  cookie = \"emqxsecretcookie\"\n  data_dir = \"data\"\n  max_ports = 2097152\n}\n\n\n\n\ncluster {\n  name = emqxcl\n  discovery_strategy = manual\n}\n\n\n\n\ndashboard {\n    listeners.http {\n        bind = 18083\n    }\n}\nmqtt {\n    max_mqueue_len = 100000\n}\nforce_shutdown {\n    enable = true\n    max_mailbox_size = 100000\n    max_heap_size = 2GB\n}\nlisteners.tcp.default {\n    bind = 1883\n    acceptors = 64\n    max_connections = 1024000\n}<\/code><\/pre>\n\n\n\n<p>\u56db\u3001\u6d4b\u8bd5\u5ba2\u6237\u7aef\u8bbe\u7f6e<\/p>\n\n\n\n<p>\u6d4b\u8bd5\u5ba2\u6237\u7aef\u670d\u52a1\u5668\u5728\u4e00\u4e2a\u63a5\u53e3\u4e0a\uff0c\u6700\u591a\u53ea\u80fd\u521b\u5efa 65000 \u8fde\u63a5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sysctl -w net.ipv4.ip_local_port_range=\"500 65535\"\necho 1000000 &gt; \/proc\/sys\/fs\/nr_open\nulimit -n 100000<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">emqtt_bench<\/h3>\n\n\n\n<p>\u5e76\u53d1\u8fde\u63a5\u6d4b\u8bd5\u5de5\u5177\uff1a<a href=\"http:\/\/github.com\/emqx\/emqtt_bench\">emqtt_bench<\/a><\/p>\n\n\n\n<div class=\"wp-block-file\"><a id=\"wp-block-file--media-b3dd52c7-fdcc-418a-a2b4-726885fd763c\" href=\"https:\/\/opshub.com.cn\/wp-content\/uploads\/2026\/01\/emqtt-bench-0.4.25.tar.gz\">emqtt-bench-0.4.25.tar<\/a><a href=\"https:\/\/opshub.com.cn\/wp-content\/uploads\/2026\/01\/emqtt-bench-0.4.25.tar.gz\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-b3dd52c7-fdcc-418a-a2b4-726885fd763c\">Download<\/a><\/div>\n\n\n\n<p>emqtt-bench requires libatomic<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># centos 7\nsudo yum install libatomic\n# ubuntu 20.04\nsudo apt install libatomic1<\/code><\/pre>\n\n\n\n<p>make<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/emqx\/emqtt-bench.git\ncd emqtt-bench\nmake\n#Optional, you could disable QUIC support if you have problem with compiling\nBUILD_WITHOUT_QUIC=1 make<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Connect Benchmark<\/h2>\n\n\n\n<p>For example, create 50K concurrent connections at the arrival rate of 100\/sec:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/emqtt_bench conn -c 50000 -i 10<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Sub Benchmark<\/h2>\n\n\n\n<p>For example, create 50K concurrent connections at the arrival rate of 100\/sec:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/emqtt_bench sub -c 50000 -i 10 -t bench\/%i -q 2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Pub Benchmark<\/h2>\n\n\n\n<p>For example, create 100 connections and each publishes messages at the rate of 100 msg\/sec.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/emqtt_bench pub -c 100 -I 10 -t bench\/%i -s 256<\/code><\/pre>\n\n\n\n<p>TLS\/SSL (cliet certificate is not required by server)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/emqtt_bench sub -c 100 -i 10 -t bench\/%i -p 8883 --ssl\n.\/emqtt_bench pub -c 100 -I 10 -t bench\/%i -p 8883 -s 256 --ssl<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u7cfb\u7edf\u8c03\u4f18 1.\u5173\u95ed\u4ea4\u6362\u5206\u533a Linux \u4ea4\u6362\u5206\u533a\u53ef\u80fd\u4f1a\u5bfc\u81f4 Erlang \u865a\u62df\u673a\u51fa\u73b0\u4e0d\u786e\u5b9a\u7684\u5185\u5b58\u5ef6\u8fdf\uff0c\u4e25\u91cd\u5f71\u54cd\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u3002 \u5efa\u8bae\u6c38\u4e45\u5173\u95ed\u4ea4\u6362\u5206\u533a\u3002 \u5728 \/etc\/fstab \u6587\u4ef6\u4e2d\u6ce8\u91ca\u6389 swap \u884c\u3002 2.linux\u64cd\u4f5c\u7cfb\u7edf\u53c2\u6570 \u7cfb\u7edf\u5168\u5c40\u5141\u8bb8\u5206\u914d\u7684\u6700\u5927\u6587\u4ef6\u53e5\u67c4\u6570: \u5141\u8bb8\u5f53\u524d\u4f1a\u8bdd \/ \u8fdb\u7a0b\u6253\u5f00\u6587\u4ef6\u53e5\u67c4\u6570: \/etc\/security\/limits.conf \u6301\u4e45\u5316\u8bbe\u7f6e\u5141\u8bb8\u7528\u6237 \/ \u8fdb\u7a0b\u6253\u5f00\u6587\u4ef6\u53e5\u67c4\u6570: \/etc\/systemd\/system.conf \u8bbe\u7f6e\u670d\u52a1\u6700\u5927\u6587\u4ef6\u53e5\u67c4\u6570: 3.tcp\u534f\u8bae\u6808\u7f51\u7edc\u53c2\u6570 optimize.sh\u811a\u672c\uff1a \u4e8c\u3001erlang\u865a\u62df\u673a\u53c2\u6570 \u4f18\u5316\u8bbe\u7f6e Erlang \u865a\u62df\u673a\u542f\u52a8\u53c2\u6570\uff0c\u914d\u7f6e\u6587\u4ef6 etc\/emqx.conf: \u4e09\u3001emqx\u6d88\u606f\u670d\u52a1\u5668\u53c2\u6570 \u8bbe\u7f6e TCP \u76d1\u542c\u5668\u7684 Acceptor \u6c60\u5927\u5c0f\uff0c\u6700\u5927\u5141\u8bb8\u8fde\u63a5\u6570\u3002 \u4f8b\u5982\uff0cTCP \u76d1\u542c\u5668\u53ef\u4f7f\u7528\u5982\u4e0b\u914d\u7f6e\u3002 emqx.conf\u5185\u5bb9\uff1a \u56db\u3001\u6d4b\u8bd5\u5ba2\u6237\u7aef\u8bbe\u7f6e \u6d4b\u8bd5\u5ba2\u6237\u7aef\u670d\u52a1\u5668\u5728\u4e00\u4e2a\u63a5\u53e3\u4e0a\uff0c\u6700\u591a\u53ea\u80fd\u521b\u5efa 65000 \u8fde\u63a5: emqtt_bench \u5e76\u53d1\u8fde\u63a5\u6d4b\u8bd5\u5de5\u5177\uff1aemqtt_bench emqtt-bench requires libatomic make Connect Benchmark For example, create 50K [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-510","post","type-post","status-publish","format-standard","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/510","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=510"}],"version-history":[{"count":2,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/510\/revisions"}],"predecessor-version":[{"id":603,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/510\/revisions\/603"}],"wp:attachment":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}