{"id":25026,"date":"2024-11-21T08:58:56","date_gmt":"2024-11-21T00:58:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/25026\/"},"modified":"2024-11-21T08:58:56","modified_gmt":"2024-11-21T00:58:56","slug":"swoole%e5%bc%80%e5%8f%91%e5%85%a5%e9%97%a8%ef%bc%9a%e5%a6%82%e4%bd%95%e5%bf%ab%e9%80%9f%e6%90%ad%e5%bb%ba%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e7%9a%84web%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/25026\/","title":{"rendered":"Swoole\u5f00\u53d1\u5165\u95e8\uff1a\u5982\u4f55\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169940904582513.jpg\" class=\"aligncenter\" title=\"Swoole\u5f00\u53d1\u5165\u95e8\uff1a\u5982\u4f55\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668\u63d2\u56fe\" alt=\"Swoole\u5f00\u53d1\u5165\u95e8\uff1a\u5982\u4f55\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668\u63d2\u56fe\" \/><\/p>\n<p>Swoole\u5f00\u53d1\u5165\u95e8\uff1a\u5982\u4f55\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>Swoole\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684PHP\u6269\u5c55\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f02\u6b65\u3001\u5e76\u53d1\u7684\u7f51\u7edc\u901a\u4fe1\u80fd\u529b\uff0c\u4f7f\u5f97PHP\u7a0b\u5e8f\u80fd\u591f\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Swoole\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668\uff0c\u5e76\u63d0\u4f9b\u5176\u4e2d\u7684\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u5b89\u88c5Swoole\u6269\u5c55<br \/>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Swoole\u6269\u5c55\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<pre># \u5b89\u88c5swoole\u6269\u5c55\npecl install swoole<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728php.ini\u6587\u4ef6\u4e2d\u6dfb\u52a0\u6269\u5c55\u914d\u7f6e\uff1a<\/p>\n<pre>extension=swoole<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e8c\u3001\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668<br \/>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668\u4e86\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aPHP\u6587\u4ef6\uff08\u4f8b\u5982server.php\uff09\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php $http = new SwooleHttpServer('0.0.0.0', 8000);\n\n$http-&gt;on('request', function ($request, $response) {\n    $response-&amp;gt;header('Content-Type', 'text\/plain');\n    $response-&amp;gt;end('Hello, Swoole!');\n});\n\n$http-&amp;gt;start();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2aSwoole\u7684Http\u670d\u52a1\u5668\u5b9e\u4f8b\uff0c\u5e76\u7ed1\u5b9a\u57288000\u7aef\u53e3\u3002\u7136\u540e\uff0c\u6211\u4eec\u901a\u8fc7\u76d1\u542crequest\u4e8b\u4ef6\u6765\u5904\u7406\u6bcf\u6b21\u7684HTTP\u8bf7\u6c42\u3002\u5728\u6bcf\u6b21\u8bf7\u6c42\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u54cd\u5e94\u7684Content-Type\u4e3atext\/plain\uff0c\u5e76\u901a\u8fc7\u8c03\u7528$response-&gt;end()\u65b9\u6cd5\u8fd4\u56de\u54cd\u5e94\u5185\u5bb9\u3002<\/p>\n<p>\u4e09\u3001\u8fd0\u884cWeb\u670d\u52a1\u5668<br \/>\u4fdd\u5b58\u4e0a\u8ff0\u4ee3\u7801\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u8fd0\u884c\u8be5Web\u670d\u52a1\u5668\uff1a<\/p>\n<pre>php server.php<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6837\uff0c\u6211\u4eec\u7684Web\u670d\u52a1\u5668\u5c31\u5df2\u7ecf\u8fd0\u884c\u8d77\u6765\u4e86\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95eehttp:\/\/localhost:8000\uff0c\u4f1a\u770b\u5230\u4e00\u4e2a\u7b80\u5355\u7684\u9875\u9762\u663e\u793aHello, Swoole!\u3002<\/p>\n<p>\u56db\u3001\u4f18\u5316Web\u670d\u52a1\u5668\u6027\u80fd<br \/>\u4e3a\u4e86\u8fdb\u4e00\u6b65\u63d0\u5347Web\u670d\u52a1\u5668\u7684\u6027\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e9b\u4f18\u5316\u914d\u7f6e\u3002\u4fee\u6539server.php\u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>&lt;?php $http = new SwooleHttpServer('0.0.0.0', 8000);\n\n$http-&gt;set([\n    'worker_num' =&amp;gt; 2,      \/\/ \u8bbe\u7f6e\u5de5\u4f5c\u8fdb\u7a0b\u6570\u4e3a2\n    'max_request' =&amp;gt; 1000,  \/\/ \u8bbe\u7f6e\u6bcf\u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u7684\u6700\u5927\u8bf7\u6c42\u6570\u4e3a1000\n]);\n\n$http-&amp;gt;on('request', function ($request, $response) {\n    $response-&amp;gt;header('Content-Type', 'text\/plain');\n    $response-&amp;gt;end('Hello, Swoole!');\n});\n\n$http-&amp;gt;start();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u901a\u8fc7\u8c03\u7528$http-&gt;set()\u65b9\u6cd5\u8bbe\u7f6e\u4e86\u4e00\u4e9b\u4f18\u5316\u53c2\u6570\u3002\u6211\u4eec\u5c06\u5de5\u4f5c\u8fdb\u7a0b\u6570\u8bbe\u7f6e\u4e3a2\uff0c\u8fd9\u6837\u53ef\u4ee5\u5229\u7528\u591a\u6838CPU\u7684\u4f18\u52bf\u3002\u6211\u4eec\u8fd8\u8bbe\u7f6e\u4e86\u6bcf\u4e2a\u5de5\u4f5c\u8fdb\u7a0b\u7684\u6700\u5927\u8bf7\u6c42\u6570\u4e3a1000\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u957f\u65f6\u95f4\u8fd0\u884c\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p>\u4e94\u3001\u5904\u7406\u8def\u7531<br \/>\u9664\u4e86\u7b80\u5355\u7684\u54cd\u5e94\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u8def\u7531\u5904\u7406\u6765\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002\u4fee\u6539server.php\u6587\u4ef6\uff0c\u4f7f\u7528Swoole\u7684\u8def\u7531\u5668\u7ec4\u4ef6\u6765\u5904\u7406\u4e0d\u540c\u7684URL\u8bf7\u6c42\uff1a<\/p>\n<pre>&lt;?php $http = new SwooleHttpServer('0.0.0.0', 8000);\n\n$http-&gt;set([\n    'worker_num' =&amp;gt; 2,\n    'max_request' =&amp;gt; 1000,\n]);\n\n$http-&amp;gt;on('request', function ($request, $response) {\n    $response-&amp;gt;header('Content-Type', 'text\/plain');\n    \n    $router = new SwooleHttpServerRouter();\n\n    $router-&amp;gt;get('\/', function () use ($response) {\n        $response-&amp;gt;end('Hello, Swoole!');\n    });\n\n    $router-&amp;gt;get('\/about', function () use ($response) {\n        $response-&amp;gt;end('This is about page.');\n    });\n\n    $router-&amp;gt;get('\/contact', function () use ($response) {\n        $response-&amp;gt;end('This is contact page.');\n    });\n\n    $router-&amp;gt;dispatch($request-&amp;gt;server['request_uri']);\n});\n\n$http-&amp;gt;start();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2aSwooleHttpServerRouter\u5b9e\u4f8b\u6765\u5904\u7406\u4e0d\u540cURL\u7684\u8bf7\u6c42\u3002\u6211\u4eec\u6dfb\u52a0\u4e86\u4e09\u4e2a\u8def\u7531\uff0c\u5206\u522b\u662f\u6839\u8def\u5f84\/\u3001\u5173\u4e8e\u9875\u9762\/about\u548c\u8054\u7cfb\u9875\u9762\/contact\u3002\u6839\u636e\u8bf7\u6c42\u7684URL\uff0c\u6211\u4eec\u901a\u8fc7\u8c03\u7528\u76f8\u5e94\u7684\u5904\u7406\u51fd\u6570\u6765\u8fd4\u56de\u4e0d\u540c\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u516d\u3001\u603b\u7ed3<br \/>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u6210\u529f\u5730\u642d\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668\uff0c\u5e76\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528Swoole\u5f00\u53d1\u7f51\u7edc\u5e94\u7528\u3002\u4ece\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u51fa\u53d1\uff0c\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63a2\u7d22Swoole\u7684\u5404\u79cd\u529f\u80fd\u548c\u9ad8\u7ea7\u7279\u6027\uff0c\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u7f51\u7edc\u5e94\u7528\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u4e86\u89e3\u548c\u5165\u95e8Swoole\u5f00\u53d1\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fSwoole\u5f00\u53d1\u5165\u95e8\uff1a\u5982\u4f55\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668\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\u5165\u95e8\uff1a\u5982\u4f55\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668 \u5f15\u8a00\uff1aSwoole\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684PHP\u6269\u5c55\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f02\u6b65\u3001\u5e76\u53d1\u7684\u7f51\u7edc\u901a\u4fe1\u80fd\u529b\uff0c\u4f7f\u5f97PHP\u7a0b\u5e8f\u80fd\u591f\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8bf7\u6c42\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Swoole\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668\uff0c\u5e76\u63d0\u4f9b\u5176\u4e2d\u7684\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u5b89\u88c5Swoole\u6269\u5c55\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Swoole\u6269\u5c55\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u5b89\u88c5\uff1a # \u5b89\u88c5swoole\u6269\u5c55 pecl install swoole \u767b\u5f55\u540e\u590d\u5236 \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728php.ini\u6587\u4ef6\u4e2d\u6dfb\u52a0\u6269\u5c55\u914d\u7f6e\uff1a extension=swoole \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Web\u670d\u52a1\u5668\u4e86\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aPHP\u6587\u4ef6\uff08\u4f8b\u5982server.php\uff09\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a &lt;?php $http = new SwooleHttpServer(&#8216;0.0.0.0&#8217;, 8000); $http-&gt;on(&#8216;request&#8217;, function ($request, $response) { $response-&amp;gt;header(&#8216;Content-Type&#8217;, &#8216;text\/plain&#8217;); $response-&amp;gt;end(&#8216;Hello, Swoole!&#8217;); }); $http-&amp;gt;start(); \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2aSwoole\u7684Http\u670d\u52a1\u5668\u5b9e\u4f8b\uff0c\u5e76\u7ed1\u5b9a\u57288000\u7aef\u53e3\u3002\u7136\u540e\uff0c\u6211\u4eec\u901a\u8fc7\u76d1\u542crequest\u4e8b\u4ef6\u6765\u5904\u7406\u6bcf\u6b21\u7684HTTP\u8bf7\u6c42\u3002\u5728\u6bcf\u6b21\u8bf7\u6c42\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u54cd\u5e94\u7684Content-Type\u4e3atext\/plain\uff0c\u5e76\u901a\u8fc7\u8c03\u7528$response-&gt;end()\u65b9\u6cd5\u8fd4\u56de\u54cd\u5e94\u5185\u5bb9\u3002 \u4e09\u3001\u8fd0\u884cWeb\u670d\u52a1\u5668\u4fdd\u5b58\u4e0a\u8ff0\u4ee3\u7801\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u8fd0\u884c\u8be5Web\u670d\u52a1\u5668\uff1a php server.php \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6837\uff0c\u6211\u4eec\u7684Web\u670d\u52a1\u5668\u5c31\u5df2\u7ecf\u8fd0\u884c\u8d77\u6765\u4e86\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95eehttp:\/\/localhost:8000\uff0c\u4f1a\u770b\u5230\u4e00\u4e2a\u7b80\u5355\u7684\u9875\u9762\u663e\u793aHello, Swoole!\u3002 \u56db\u3001\u4f18\u5316Web\u670d\u52a1\u5668\u6027\u80fd\u4e3a\u4e86\u8fdb\u4e00\u6b65\u63d0\u5347Web\u670d\u52a1\u5668\u7684\u6027\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e9b\u4f18\u5316\u914d\u7f6e\u3002\u4fee\u6539server.php\u6587\u4ef6\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a &lt;?php $http = new SwooleHttpServer(&#8216;0.0.0.0&#8217;, 8000); $http-&gt;set([ &#8216;worker_num&#8217; =&amp;gt; 2, \/\/ \u8bbe\u7f6e\u5de5\u4f5c\u8fdb\u7a0b\u6570\u4e3a2 &#8216;max_request&#8217; =&amp;gt; 1000, \/\/ [&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-25026","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25026","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=25026"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/25026\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=25026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=25026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=25026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}