{"id":24977,"date":"2024-11-21T08:37:28","date_gmt":"2024-11-21T00:37:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24977\/"},"modified":"2024-11-21T08:37:28","modified_gmt":"2024-11-21T00:37:28","slug":"swoole%e5%bc%80%e5%8f%91%e6%8a%80%e5%b7%a7%ef%bc%9a%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e5%a4%a7%e9%87%8f%e7%9a%84%e5%b9%b6%e5%8f%91%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24977\/","title":{"rendered":"Swoole\u5f00\u53d1\u6280\u5de7\uff1a\u5982\u4f55\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169933214729228.jpg\" class=\"aligncenter\" title=\"Swoole\u5f00\u53d1\u6280\u5de7\uff1a\u5982\u4f55\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u63d2\u56fe\" alt=\"Swoole\u5f00\u53d1\u6280\u5de7\uff1a\u5982\u4f55\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u63d2\u56fe\" \/><\/p>\n<p>Swoole\u5f00\u53d1\u6280\u5de7\uff1a\u5982\u4f55\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\uff0c\u9700\u8981\u5177\u4f53\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u968f\u7740\u4e92\u8054\u7f51\u5e94\u7528\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u5df2\u7ecf\u6210\u4e3a\u4e86\u5f88\u591a\u5f00\u53d1\u8005\u9762\u4e34\u7684\u6838\u5fc3\u95ee\u9898\u3002\u5728\u4f20\u7edf\u7684 PHP \u5f00\u53d1\u4e2d\uff0c\u7531\u4e8e PHP \u7684\u7ebf\u7a0b\u6a21\u578b\u9650\u5236\uff0c\u5f80\u5f80\u65e0\u6cd5\u505a\u5230\u771f\u6b63\u7684\u5e76\u53d1\u5904\u7406\u3002\u7136\u800c\uff0c\u968f\u7740 Swoole \u7684\u51fa\u73b0\uff0cPHP \u5f00\u53d1\u8005\u7ec8\u4e8e\u53ef\u4ee5\u501f\u52a9\u5b83\u5f3a\u5927\u7684\u5f02\u6b65\u6846\u67b6\u6765\u9ad8\u6548\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u4e86\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Swoole \u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u4ec0\u4e48\u662f Swoole\uff1f<br \/>Swoole \u662f\u4e00\u6b3e\u57fa\u4e8e C++ \u5b9e\u73b0\u7684 PHP \u5f02\u6b65\u3001\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u5f15\u64ce\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u540c\u6b65\u3001\u5f02\u6b65\u7f51\u7edc\u901a\u4fe1\u7ec4\u4ef6\uff0c\u80fd\u591f\u5feb\u901f\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e94\u7528\uff0c\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u3002Swoole \u5145\u5206\u5229\u7528\u4e86\u5e95\u5c42\u64cd\u4f5c\u7cfb\u7edf\u7684\u7279\u6027\uff0c\u91c7\u7528 Reactor \u6a21\u5f0f\u548c\u591a\u8fdb\u7a0b\u6a21\u578b\uff0c\u4f7f\u5f97 PHP \u5f00\u53d1\u5177\u5907\u4e86\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u80fd\u529b\u3002<\/p>\n<p>\u4e8c\u3001\u4f7f\u7528 Swoole \u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u7684\u6280\u5de7<\/p>\n<ol>\n<li>\u4f7f\u7528\u5f02\u6b65\u670d\u52a1\u5668<br \/>\u7531\u4e8e Swoole \u7684\u5f02\u6b65\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Swoole \u7684\u5f02\u6b65\u670d\u52a1\u5668\u6765\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u3002\u4f7f\u7528\u5f02\u6b65\u670d\u52a1\u5668\u53ef\u4ee5\u8ba9\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u5728\u72ec\u7acb\u7684\u5de5\u4f5c\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u4e0d\u4f1a\u9020\u6210\u963b\u585e\u548c\u8d44\u6e90\u6d6a\u8d39\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 Swoole \u5f02\u6b65\u670d\u52a1\u5668\u5904\u7406 HTTP \u8bf7\u6c42\u7684\u7b80\u5355\u793a\u4f8b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>$server = new swoole_http_server(\"0.0.0.0\", 9501);\n\n$server-&amp;gt;on('request', function ($request, $response) {\n    \/\/ \u6267\u884c\u8017\u65f6\u64cd\u4f5c\uff0c\u4f8b\u5982\u6570\u636e\u5e93\u67e5\u8be2\u7b49\n    $result = doSomething();\n\n    \/\/ \u8fd4\u56de\u7ed3\u679c\n    $response-&amp;gt;header(\"Content-Type\", \"text\/plain\");\n    $response-&amp;gt;end($result);\n});\n\n$server-&amp;gt;start();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u4f7f\u7528\u534f\u7a0b<br \/>Swoole \u5f15\u5165\u4e86\u534f\u7a0b\u7684\u6982\u5ff5\uff0c\u53ef\u4ee5\u5728\u5f02\u6b65\u4efb\u52a1\u4e2d\u65b9\u4fbf\u5730\u4f7f\u7528\u540c\u6b65\u7684\u7f16\u7a0b\u65b9\u5f0f\u3002\u4f7f\u7528\u534f\u7a0b\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u903b\u8f91\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 Swoole \u534f\u7a0b\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>$server = new swoole_http_server(\"0.0.0.0\", 9501);\n\n$server-&amp;gt;on('request', function ($request, $response) {\n    go(function () use ($response) {\n        \/\/ \u6267\u884c\u8017\u65f6\u64cd\u4f5c\uff0c\u4f8b\u5982\u6570\u636e\u5e93\u67e5\u8be2\u7b49\n        $result = doSomething();\n\n        \/\/ \u8fd4\u56de\u7ed3\u679c\n        $response-&amp;gt;header(\"Content-Type\", \"text\/plain\");\n        $response-&amp;gt;end($result);\n    });\n});\n\n$server-&amp;gt;start();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u4f7f\u7528\u8fde\u63a5\u6c60<br \/>\u5728\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u65f6\uff0c\u6570\u636e\u5e93\u8fde\u63a5\u5f80\u5f80\u6210\u4e3a\u74f6\u9888\u3002\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fde\u63a5\u6c60\u6765\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u3002Swoole \u63d0\u4f9b\u4e86 easySwoole \u7684\u7ec4\u4ef6\u5e93\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7684\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 easySwoole \u6570\u636e\u5e93\u8fde\u63a5\u6c60\u5904\u7406\u5e76\u53d1\u8bf7\u6c42\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>\/\/ \u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u6c60\n$dbConfig = [\n    'host' =&amp;gt; 'localhost',\n    'port' =&amp;gt; 3306,\n    'user' =&amp;gt; 'root',\n    'password' =&amp;gt; 'root',\n    'database' =&amp;gt; 'test',\n];\n\n\/\/ \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\u6c60\n$dbPool = new EasySwoolePoolManager(AppPoolConfig::class);\n$dbPool-&amp;gt;registerPool('mysql', new EasySwoolePoolConfig($dbConfig));\n\n$server = new swoole_http_server(\"0.0.0.0\", 9501);\n\n$server-&amp;gt;on('request', function ($request, $response) use ($dbPool) {\n    go(function () use ($response, $dbPool) {\n        \/\/ \u4ece\u8fde\u63a5\u6c60\u4e2d\u83b7\u53d6\u8fde\u63a5\n        $db = $dbPool-&amp;gt;get('mysql')-&amp;gt;getObj();\n\n        \/\/ \u6267\u884c\u8017\u65f6\u64cd\u4f5c\uff0c\u4f8b\u5982\u6570\u636e\u5e93\u67e5\u8be2\u7b49\n        $result = $db-&amp;gt;query('SELECT * FROM users');\n\n        \/\/ \u91ca\u653e\u8fde\u63a5\u5230\u8fde\u63a5\u6c60\n        $dbPool-&amp;gt;get('mysql')-&amp;gt;free($db);\n\n        \/\/ \u8fd4\u56de\u7ed3\u679c\n        $response-&amp;gt;header(\"Content-Type\", \"text\/plain\");\n        $response-&amp;gt;end($result);\n    });\n});\n\n$server-&amp;gt;start();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e09\u3001\u603b\u7ed3<br \/>\u901a\u8fc7\u4f7f\u7528 Swoole\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\uff0c\u5145\u5206\u5229\u7528\u7cfb\u7edf\u7684\u6027\u80fd\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u4e09\u79cd\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u7684\u6280\u5de7\uff1a\u4f7f\u7528\u5f02\u6b65\u670d\u52a1\u5668\u3001\u4f7f\u7528\u534f\u7a0b\u548c\u4f7f\u7528\u8fde\u63a5\u6c60\u3002\u901a\u8fc7\u5408\u7406\u5730\u4f7f\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e94\u7528\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u6280\u5de7\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u5f00\u53d1\u6280\u5de7\uff1a\u5982\u4f55\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swoole\u5f00\u53d1\u6280\u5de7\uff1a\u5982\u4f55\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\uff0c\u9700\u8981\u5177\u4f53\u4ee3\u7801\u793a\u4f8b \u5f15\u8a00\uff1a\u968f\u7740\u4e92\u8054\u7f51\u5e94\u7528\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u5df2\u7ecf\u6210\u4e3a\u4e86\u5f88\u591a\u5f00\u53d1\u8005\u9762\u4e34\u7684\u6838\u5fc3\u95ee\u9898\u3002\u5728\u4f20\u7edf\u7684 PHP \u5f00\u53d1\u4e2d\uff0c\u7531\u4e8e PHP \u7684\u7ebf\u7a0b\u6a21\u578b\u9650\u5236\uff0c\u5f80\u5f80\u65e0\u6cd5\u505a\u5230\u771f\u6b63\u7684\u5e76\u53d1\u5904\u7406\u3002\u7136\u800c\uff0c\u968f\u7740 Swoole \u7684\u51fa\u73b0\uff0cPHP \u5f00\u53d1\u8005\u7ec8\u4e8e\u53ef\u4ee5\u501f\u52a9\u5b83\u5f3a\u5927\u7684\u5f02\u6b65\u6846\u67b6\u6765\u9ad8\u6548\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u4e86\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 Swoole \u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u4ec0\u4e48\u662f Swoole\uff1fSwoole \u662f\u4e00\u6b3e\u57fa\u4e8e C++ \u5b9e\u73b0\u7684 PHP \u5f02\u6b65\u3001\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7f51\u7edc\u901a\u4fe1\u5f15\u64ce\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u540c\u6b65\u3001\u5f02\u6b65\u7f51\u7edc\u901a\u4fe1\u7ec4\u4ef6\uff0c\u80fd\u591f\u5feb\u901f\u6784\u5efa\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u5e94\u7528\uff0c\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u3002Swoole \u5145\u5206\u5229\u7528\u4e86\u5e95\u5c42\u64cd\u4f5c\u7cfb\u7edf\u7684\u7279\u6027\uff0c\u91c7\u7528 Reactor \u6a21\u5f0f\u548c\u591a\u8fdb\u7a0b\u6a21\u578b\uff0c\u4f7f\u5f97 PHP \u5f00\u53d1\u5177\u5907\u4e86\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u80fd\u529b\u3002 \u4e8c\u3001\u4f7f\u7528 Swoole \u5904\u7406\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\u7684\u6280\u5de7 \u4f7f\u7528\u5f02\u6b65\u670d\u52a1\u5668\u7531\u4e8e Swoole \u7684\u5f02\u6b65\u7279\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Swoole \u7684\u5f02\u6b65\u670d\u52a1\u5668\u6765\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u3002\u4f7f\u7528\u5f02\u6b65\u670d\u52a1\u5668\u53ef\u4ee5\u8ba9\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u5728\u72ec\u7acb\u7684\u5de5\u4f5c\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u4e0d\u4f1a\u9020\u6210\u963b\u585e\u548c\u8d44\u6e90\u6d6a\u8d39\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 Swoole \u5f02\u6b65\u670d\u52a1\u5668\u5904\u7406 HTTP \u8bf7\u6c42\u7684\u7b80\u5355\u793a\u4f8b\u4ee3\u7801\uff1a $server = new swoole_http_server(&#8220;0.0.0.0&#8221;, 9501); $server-&amp;gt;on(&#8216;request&#8217;, function ($request, $response) { \/\/ \u6267\u884c\u8017\u65f6\u64cd\u4f5c\uff0c\u4f8b\u5982\u6570\u636e\u5e93\u67e5\u8be2\u7b49 $result = doSomething(); \/\/ \u8fd4\u56de\u7ed3\u679c $response-&amp;gt;header(&#8220;Content-Type&#8221;, &#8220;text\/plain&#8221;); $response-&amp;gt;end($result); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24977","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24977"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24977\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}