{"id":1074,"date":"2025-01-19T13:53:04","date_gmt":"2025-01-19T05:53:04","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1074"},"modified":"2025-01-19T13:53:04","modified_gmt":"2025-01-19T05:53:04","slug":"swoole-%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%e4%b8%8e%e8%b0%83%e4%bc%98%e6%8a%80%e5%b7%a7%ef%bc%9a%e6%8f%90%e5%8d%87%e5%b9%b6%e5%8f%91%e5%a4%84%e7%90%86%e8%83%bd%e5%8a%9b%e4%b8%8e%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/19\/swoole-%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%e4%b8%8e%e8%b0%83%e4%bc%98%e6%8a%80%e5%b7%a7%ef%bc%9a%e6%8f%90%e5%8d%87%e5%b9%b6%e5%8f%91%e5%a4%84%e7%90%86%e8%83%bd%e5%8a%9b%e4%b8%8e%e7%b3%bb%e7%bb%9f\/","title":{"rendered":"Swoole \u6027\u80fd\u4f18\u5316\u4e0e\u8c03\u4f18\u6280\u5de7\uff1a\u63d0\u5347\u5e76\u53d1\u5904\u7406\u80fd\u529b\u4e0e\u7cfb\u7edf\u6548\u7387"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Swoole \u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5b83\u7684\u534f\u7a0b\u673a\u5236\u548c\u5f02\u6b65\u7279\u6027\u4f7f\u5f97 PHP \u7684\u6027\u80fd\u5f97\u5230\u4e86\u663e\u8457\u63d0\u5347\u3002\u7136\u800c\uff0c\u8981\u4f7f Swoole \u7684\u6027\u80fd\u8fbe\u5230\u6700\u4f18\u72b6\u6001\uff0c\u4ecd\u7136\u9700\u8981\u4ece\u591a\u4e2a\u65b9\u9762\u8fdb\u884c\u8c03\u4f18\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u6027\u80fd\u4f18\u5316\u7b56\u7565\u548c\u6280\u5de7\uff0c\u5e2e\u52a9\u4f60\u5728\u4f7f\u7528 Swoole \u65f6\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>\u5185\u5b58\u7ba1\u7406\u4f18\u5316<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u7684\u6027\u80fd\u53d7\u5185\u5b58\u7ba1\u7406\u7684\u5f71\u54cd\u8f83\u5927\uff0c\u7279\u522b\u662f\u5728\u9ad8\u5e76\u53d1\u7684\u60c5\u51b5\u4e0b\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u7684\u7b56\u7565\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 <strong>\u534f\u7a0b\u6808\u5185\u5b58\u4f18\u5316<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u9ed8\u8ba4\u6bcf\u4e2a\u534f\u7a0b\u7684\u6808\u5927\u5c0f\u4e3a 1MB\u3002\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\uff0c\u8fc7\u591a\u7684\u534f\u7a0b\u4f1a\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8c03\u6574\u6808\u5927\u5c0f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u8bbe\u7f6e\u534f\u7a0b\u6808\u5927\u5c0f\u4e3a 512KB\nSwoole\\Runtime::setCoroutineStackSize(1024 * 512); <\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u5316\u5efa\u8bae<\/strong>\uff1a\u6839\u636e\u534f\u7a0b\u7684\u5b9e\u9645\u4f7f\u7528\u60c5\u51b5\uff0c\u8bbe\u7f6e\u5408\u9002\u7684\u6808\u5927\u5c0f\u3002\u5982\u679c\u4f60\u53ea\u8fdb\u884c\u7b80\u5355\u7684 I\/O \u64cd\u4f5c\uff0c\u8f83\u5c0f\u7684\u6808\u5927\u5c0f\u4f1a\u8282\u7701\u5185\u5b58\u8d44\u6e90\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 <strong>\u4f7f\u7528\u5171\u4eab\u5185\u5b58<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u9ad8\u5e76\u53d1\u73af\u5883\u4e0b\uff0c\u591a\u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u5171\u4eab\u975e\u5e38\u91cd\u8981\u3002Swoole \u63d0\u4f9b\u4e86\u5171\u4eab\u5185\u5b58\u548c <code>table<\/code> \u529f\u80fd\uff0c\u7528\u4e8e\u9ad8\u6548\u5730\u8de8\u8fdb\u7a0b\u5171\u4eab\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$table = new Swoole\\Table(1024);  \/\/ \u521b\u5efa\u4e00\u4e2a 1024 \u884c\u7684\u5171\u4eab\u5185\u5b58\u8868\n$table-&gt;column('value', Swoole\\Table::TYPE_INT, 8);\n$table-&gt;create();\n$table-&gt;set('key1', &#91;'value' =&gt; 100]);  \/\/ \u8bbe\u7f6e\u6570\u636e<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u5316\u5efa\u8bae<\/strong>\uff1a\u5bf9\u4e8e\u9700\u8981\u9891\u7e41\u8de8\u8fdb\u7a0b\u5171\u4eab\u7684\u6570\u636e\uff0c\u4f7f\u7528 <code>Swoole\\Table<\/code> \u6765\u63d0\u9ad8\u6548\u7387\uff0c\u907f\u514d\u9891\u7e41\u7684 IPC\uff08\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff09\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>\u534f\u7a0b\u4f18\u5316<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u534f\u7a0b\u662f Swoole \u7684\u6838\u5fc3\u7279\u6027\uff0c\u5b83\u901a\u8fc7\u975e\u963b\u585e I\/O \u64cd\u4f5c\u63d0\u9ad8\u4e86\u5e76\u53d1\u80fd\u529b\u3002\u8981\u4f18\u5316\u534f\u7a0b\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u4ece\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u5165\u624b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 <strong>\u51cf\u5c11\u534f\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\u7684\u6210\u672c<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u534f\u7a0b\u7684\u521b\u5efa\u548c\u9500\u6bc1\u662f\u6709\u4e00\u5b9a\u6210\u672c\u7684\uff0c\u56e0\u6b64\u9700\u8981\u5c3d\u91cf\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u534f\u7a0b\u521b\u5efa\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u4f18\u5316\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528\u8fde\u63a5\u6c60<\/strong>\uff1a\u5bf9\u4e8e\u9891\u7e41\u8bbf\u95ee\u7684\u5916\u90e8\u8d44\u6e90\uff08\u5982\u6570\u636e\u5e93\u3001\u7f13\u5b58\u7b49\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fde\u63a5\u6c60\u6765\u590d\u7528\u534f\u7a0b\u8fde\u63a5\uff0c\u51cf\u5c11\u534f\u7a0b\u7684\u521b\u5efa\u4e0e\u9500\u6bc1\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$mysql = new Swoole\\Coroutine\\Mysql();\n$mysql-&gt;connect($config);  \/\/ \u91cd\u7528\u8fde\u63a5<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u51cf\u5c11\u534f\u7a0b\u6570\u91cf<\/strong>\uff1a\u5c3d\u91cf\u907f\u514d\u4e00\u6b21\u6027\u521b\u5efa\u8fc7\u591a\u534f\u7a0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u9650\u5236\u5e76\u53d1\u91cf\u6216\u4f18\u5316\u4efb\u52a1\u7684\u5206\u914d\u6765\u63a7\u5236\u534f\u7a0b\u6570\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 <strong>\u907f\u514d\u534f\u7a0b\u963b\u585e<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u534f\u7a0b\u4e2d\uff0c\u907f\u514d\u4f7f\u7528\u4f1a\u963b\u585e\u5f53\u524d\u534f\u7a0b\u7684\u64cd\u4f5c\u3002\u901a\u5e38\u7684\u963b\u585e\u64cd\u4f5c\uff08\u5982 <code>sleep<\/code>\u3001<code>usleep<\/code>\u3001<code>fread<\/code> \u7b49\uff09\u4f1a\u5bfc\u81f4\u534f\u7a0b\u65e0\u6cd5\u6709\u6548\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u5316\u5efa\u8bae<\/strong>\uff1a\u4f7f\u7528\u5f02\u6b65\u975e\u963b\u585e\u7684\u65b9\u5f0f\u5904\u7406\u6240\u6709 I\/O \u64cd\u4f5c\uff0c\u5982\u4f7f\u7528 <code>Swoole\\Coroutine\\Http\\Client<\/code> \u6216 <code>Swoole\\Coroutine\\Redis<\/code> \u7b49\u7c7b\u6765\u66ff\u4ee3\u4f20\u7edf\u7684\u540c\u6b65\u963b\u585e\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. <strong>\u8fdb\u7a0b\u7ba1\u7406\u4e0e\u8d1f\u8f7d\u5747\u8861<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u652f\u6301\u591a\u8fdb\u7a0b\u6a21\u578b\uff0c\u901a\u8fc7\u8fdb\u7a0b\u95f4\u7684\u534f\u4f5c\u63d0\u5347\u6027\u80fd\u3002\u5408\u7406\u7684\u8fdb\u7a0b\u7ba1\u7406\u548c\u8d1f\u8f7d\u5747\u8861\u662f\u63d0\u9ad8\u6027\u80fd\u7684\u5173\u952e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 <strong>\u8c03\u6574 Worker \u8fdb\u7a0b\u6570\u91cf<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u7684 <code>worker_num<\/code> \u4ee3\u8868\u4e86\u542f\u52a8\u7684\u5de5\u4f5c\u8fdb\u7a0b\u6570\u91cf\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cSwoole \u4f1a\u6839\u636e CPU \u6838\u6570\u81ea\u52a8\u8bbe\u7f6e\u5de5\u4f5c\u8fdb\u7a0b\u6570\uff0c\u4f46\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u624b\u52a8\u8c03\u6574\u8be5\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$server-&gt;set(&#91;\n    'worker_num' =&gt; swoole_cpu_num() * 2,  \/\/ \u8bbe\u7f6e\u4e3a CPU \u6838\u6570\u7684\u4e24\u500d\n]);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u5316\u5efa\u8bae<\/strong>\uff1a\u6839\u636e\u670d\u52a1\u5668\u7684\u5b9e\u9645\u786c\u4ef6\u914d\u7f6e\uff0c\u8c03\u6574 <code>worker_num<\/code> \u548c <code>task_worker_num<\/code> \u7684\u6570\u91cf\uff0c\u4ee5\u4fbf\u5145\u5206\u5229\u7528 CPU \u8d44\u6e90\uff0c\u63d0\u9ad8\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 <strong>\u4f7f\u7528\u8d1f\u8f7d\u5747\u8861<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u8d1f\u8f7d\u5747\u8861\u662f\u63d0\u9ad8\u6027\u80fd\u7684\u5173\u952e\u3002Swoole \u652f\u6301\u591a\u79cd\u8d1f\u8f7d\u5747\u8861\u7b56\u7565\uff0c\u4f8b\u5982\u8f6e\u8be2\u3001\u52a0\u6743\u3001\u6700\u5c11\u8fde\u63a5\u7b49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$server-&gt;set(&#91;\n    'dispatch_mode' =&gt; 2,  \/\/ \u8bbe\u7f6e\u8d1f\u8f7d\u5747\u8861\u6a21\u5f0f\uff0c2 \u4e3a\u8f6e\u8be2\u6a21\u5f0f\n]);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u5316\u5efa\u8bae<\/strong>\uff1a\u6839\u636e\u4e1a\u52a1\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u8d1f\u8f7d\u5747\u8861\u6a21\u5f0f\uff0c\u786e\u4fdd\u6d41\u91cf\u5206\u914d\u5747\u5300\uff0c\u907f\u514d\u90e8\u5206\u8fdb\u7a0b\u8d1f\u8f7d\u8fc7\u91cd\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 <strong>\u8fdb\u7a0b\u6c60\u4e0e\u4efb\u52a1\u7ba1\u7406<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u63d0\u4f9b\u4e86\u8fdb\u7a0b\u6c60\u548c\u4efb\u52a1\u6c60\u6765\u7ba1\u7406\u5b50\u8fdb\u7a0b\u7684\u8c03\u5ea6\u548c\u4efb\u52a1\u5206\u914d\u3002\u5408\u7406\u4f7f\u7528\u8fdb\u7a0b\u6c60\u53ef\u4ee5\u6709\u6548\u5206\u62c5\u4e3b\u8fdb\u7a0b\u7684\u538b\u529b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$taskWorker = new Swoole\\Process(function ($worker) {\n    \/\/ \u5b50\u8fdb\u7a0b\u7684\u4efb\u52a1\u903b\u8f91\n});\n$taskWorker-&gt;start();<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u5316\u5efa\u8bae<\/strong>\uff1a\u4f7f\u7528\u8fdb\u7a0b\u6c60\u7ba1\u7406\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u907f\u514d\u8ba9\u5de5\u4f5c\u8fdb\u7a0b\u88ab\u957f\u65f6\u95f4\u963b\u585e\uff0c\u5f71\u54cd\u5176\u4ed6\u8bf7\u6c42\u7684\u5904\u7406\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. <strong>\u7f51\u7edc\u4f18\u5316<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u6846\u67b6\uff0c\u7f51\u7edc I\/O \u64cd\u4f5c\u7684\u4f18\u5316\u81f3\u5173\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u7f51\u7edc\u6027\u80fd\u7684\u5efa\u8bae\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 <strong>\u542f\u7528\u5185\u5b58\u7f13\u5b58<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u652f\u6301\u5185\u5b58\u7f13\u5b58\u6765\u5b58\u50a8\u5e38\u7528\u6570\u636e\uff0c\u51cf\u5c11\u6570\u636e\u5e93\u7684\u8bbf\u95ee\u538b\u529b\u3002\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u6216 Redis \u7b49\u7f13\u5b58\u89e3\u51b3\u65b9\u6848\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7cfb\u7edf\u7684\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$cache = new Swoole\\Coroutine\\Redis();\n$cache-&gt;connect('127.0.0.1', 6379);\n$cache-&gt;set('key', 'value');  \/\/ \u5b58\u50a8\u5230\u7f13\u5b58\u4e2d<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u5316\u5efa\u8bae<\/strong>\uff1a\u4f7f\u7528\u7f13\u5b58\u5c42\uff0c\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\u548c\u6570\u636e\u5e93\u8bbf\u95ee\uff0c\u907f\u514d\u9ad8\u5e76\u53d1\u4e0b\u7684\u6027\u80fd\u74f6\u9888\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 <strong>TCP\/UDP \u4f18\u5316<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u652f\u6301 TCP \u548c UDP \u534f\u8bae\u7684\u9ad8\u6548\u5904\u7406\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u534f\u8bae\u7c7b\u578b\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>TCP \u4f18\u5316<\/strong>\uff1aTCP \u8fde\u63a5\u9700\u8981\u5efa\u7acb\u4e09\u6b21\u63e1\u624b\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7a33\u5b9a\u8fde\u63a5\u7684\u573a\u666f\uff08\u5982 Web \u670d\u52a1\uff09\u3002\u901a\u8fc7\u542f\u7528 <code>TCP_NODELAY<\/code> \u548c\u8c03\u6574 <code>SO_RCVBUF<\/code>\u3001<code>SO_SNDBUF<\/code> \u7b49\u5957\u63a5\u5b57\u7f13\u51b2\u533a\u5927\u5c0f\uff0c\u6765\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$server-&gt;set(&#91;\n    'open_tcp_nodelay' =&gt; true,  \/\/ \u542f\u7528 Nagle \u7b97\u6cd5\n    'socket_buffer_size' =&gt; 1024 * 1024,  \/\/ \u8c03\u6574\u5957\u63a5\u5b57\u7f13\u51b2\u533a\u5927\u5c0f\n]);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>UDP \u4f18\u5316<\/strong>\uff1aUDP \u534f\u8bae\u9002\u7528\u4e8e\u6570\u636e\u91cf\u8f83\u5c0f\u3001\u5bf9\u4e22\u5305\u5bb9\u5fcd\u5ea6\u8f83\u9ad8\u7684\u573a\u666f\u3002\u4f7f\u7528 UDP \u65f6\u53ef\u4ee5\u542f\u7528 <code>open_udp<\/code> \u548c\u8c03\u6574\u63a5\u6536\u7f13\u51b2\u533a\u5927\u5c0f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 <strong>\u542f\u7528 TLS\/SSL<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u9700\u8981\u52a0\u5bc6\u4f20\u8f93\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u542f\u7528 Swoole \u7684 TLS\/SSL \u652f\u6301\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\u4f20\u8f93\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$server-&gt;set(&#91;\n    'ssl_cert_file' =&gt; '\/path\/to\/cert.pem',\n    'ssl_key_file' =&gt; '\/path\/to\/key.pem',\n]);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u5316\u5efa\u8bae<\/strong>\uff1a\u5bf9\u4e8e\u9ad8\u5e76\u53d1\u7684 HTTPS \u670d\u52a1\uff0c\u6ce8\u610f\u4f7f\u7528\u786c\u4ef6\u52a0\u901f\uff08\u5982 OpenSSL\u3001\u786c\u4ef6\u52a0\u5bc6\u5361\uff09\u6765\u63d0\u5347\u52a0\u5bc6\u89e3\u5bc6\u6548\u7387\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. <strong>\u65e5\u5fd7\u4e0e\u76d1\u63a7<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6027\u80fd\u4f18\u5316\u4e0d\u4ec5\u4ec5\u662f\u4f18\u5316\u4ee3\u7801\u548c\u914d\u7f6e\uff0c\u65e5\u5fd7\u4e0e\u76d1\u63a7\u7cfb\u7edf\u540c\u6837\u91cd\u8981\u3002\u901a\u8fc7\u5b9e\u65f6\u76d1\u63a7\uff0c\u53ca\u65f6\u53d1\u73b0\u6027\u80fd\u74f6\u9888\u5e76\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.1 <strong>\u542f\u7528\u5f02\u6b65\u65e5\u5fd7<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u652f\u6301\u5f02\u6b65\u65e5\u5fd7\u5199\u5165\uff0c\u907f\u514d\u65e5\u5fd7\u5199\u5165\u963b\u585e\u4e3b\u8fdb\u7a0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$server-&gt;set(&#91;\n    'log_file' =&gt; '\/var\/log\/swoole.log',\n    'log_level' =&gt; 2,  \/\/ \u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\n]);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u5316\u5efa\u8bae<\/strong>\uff1a\u901a\u8fc7\u5f02\u6b65\u65e5\u5fd7\u673a\u5236\uff0c\u907f\u514d\u540c\u6b65\u5199\u5165\u65e5\u5fd7\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5.2 <strong>\u6027\u80fd\u76d1\u63a7\u5de5\u5177<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684\u6027\u80fd\u76d1\u63a7\u63a5\u53e3\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b9e\u65f6\u67e5\u770b\u5f53\u524d\u8fdb\u7a0b\u7684 CPU \u4f7f\u7528\u7387\u3001\u5185\u5b58\u4f7f\u7528\u91cf\u7b49\u6307\u6807\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Swoole\\Runtime::enableCoroutine();  \/\/ \u542f\u7528\u534f\u7a0b\u76d1\u63a7<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u5316\u5efa\u8bae<\/strong>\uff1a\u7ed3\u5408\u7b2c\u4e09\u65b9\u76d1\u63a7\u5de5\u5177\uff08\u5982 Prometheus\u3001Grafana\uff09\u76d1\u63a7 Swoole \u670d\u52a1\u5668\u7684\u6027\u80fd\uff0c\u53ca\u65f6\u53d1\u73b0\u6f5c\u5728\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. <strong>\u603b\u7ed3<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Swoole \u662f\u4e00\u4e2a\u9ad8\u5ea6\u53ef\u5b9a\u5236\u7684\u6846\u67b6\uff0c\u901a\u8fc7\u5408\u7406\u7684\u914d\u7f6e\u548c\u4f18\u5316\uff0c\u80fd\u591f\u6781\u5927\u5730\u63d0\u5347 PHP \u5e94\u7528\u7684\u6027\u80fd\u3002\u901a\u8fc7\u4f18\u5316\u5185\u5b58\u7ba1\u7406\u3001\u5408\u7406\u4f7f\u7528\u534f\u7a0b\u3001\u8c03\u4f18\u8fdb\u7a0b\u4e0e\u8d1f\u8f7d\u5747\u8861\u3001\u7f51\u7edc\u4f18\u5316\u4ee5\u53ca\u589e\u5f3a\u65e5\u5fd7\u4e0e\u76d1\u63a7\uff0c\u60a8\u53ef\u4ee5\u6700\u5927\u5316 Swoole \u7684\u6027\u80fd\uff0c\u6253\u9020\u9ad8\u5e76\u53d1\u3001\u9ad8\u53ef\u6269\u5c55\u6027\u7684\u7cfb\u7edf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swoole \u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u7f51\u7edc\u901a\u4fe1\u6846\u67b6\uff0c\u5b83\u7684\u534f\u7a0b\u673a\u5236\u548c\u5f02\u6b65\u7279\u6027\u4f7f\u5f97 PHP \u7684\u6027\u80fd\u5f97\u5230\u4e86\u663e\u8457\u63d0\u5347\u3002\u7136\u800c\uff0c\u8981\u4f7f  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64],"tags":[],"class_list":["post-1074","post","type-post","status-publish","format-standard","hentry","category-swoole"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1074","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1074"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1074\/revisions"}],"predecessor-version":[{"id":1075,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1074\/revisions\/1075"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}