{"id":274,"date":"2024-12-15T11:01:40","date_gmt":"2024-12-15T03:01:40","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=274"},"modified":"2024-12-15T11:01:40","modified_gmt":"2024-12-15T03:01:40","slug":"swoole%e5%85%a8%e6%99%af%e4%bb%8b%e7%bb%8d%ef%bc%9a%e4%bb%8e%e5%9f%ba%e7%a1%80%e5%88%b0%e8%bf%9b%e9%98%b6%e7%9a%84%e5%85%a8%e9%9d%a2%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/15\/swoole%e5%85%a8%e6%99%af%e4%bb%8b%e7%bb%8d%ef%bc%9a%e4%bb%8e%e5%9f%ba%e7%a1%80%e5%88%b0%e8%bf%9b%e9%98%b6%e7%9a%84%e5%85%a8%e9%9d%a2%e8%a7%a3%e6%9e%90\/","title":{"rendered":"Swoole\u5168\u666f\u4ecb\u7ecd\uff1a\u4ece\u57fa\u7840\u5230\u8fdb\u9636\u7684\u5168\u9762\u89e3\u6790"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u672c\u6587\u5c06\u5168\u9762\u4ecb\u7ecd Swoole \u7684\u57fa\u7840\u6982\u5ff5\u3001\u6838\u5fc3\u529f\u80fd\u3001\u8fdb\u9636\u6280\u5de7\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u4ece\u5165\u95e8\u5230\u8fdb\u9636\u5e2e\u52a9\u4f60\u5168\u9762\u638c\u63e1 Swoole\u3002<\/p>\n<\/blockquote>\n\n\n\n<p><strong>Swoole<\/strong> \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684 PHP \u6269\u5c55\uff0c\u65e8\u5728\u589e\u5f3a PHP \u7684\u7f51\u7edc\u548c\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002\u5b83\u4f7f\u5f97 PHP \u4e0d\u4ec5\u80fd\u591f\u5904\u7406\u4f20\u7edf\u7684\u540c\u6b65\u8bf7\u6c42\uff0c\u8fd8\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5f02\u6b65\u4efb\u52a1\u3001\u9ad8\u5e76\u53d1\u8bf7\u6c42\u548c\u957f\u8fde\u63a5\u670d\u52a1\u3002Swoole \u4e3b\u8981\u7528\u4e8e\u6784\u5efa\u5b9e\u65f6\u3001\u9ad8\u5e76\u53d1\u7684 Web \u5e94\u7528\u3001\u5fae\u670d\u52a1\u3001\u804a\u5929\u5e94\u7528\u3001\u6570\u636e\u63a8\u9001\u670d\u52a1\u7b49\u3002<\/p>\n\n\n\n<p>\u4f5c\u4e3a\u4e00\u4e2a PHP \u6269\u5c55\uff0cSwoole \u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u534f\u7a0b\u3001\u5f02\u6b65 I\/O\u3001WebSocket \u652f\u6301\u3001TCP\/UDP \u670d\u52a1\u3001\u5b9a\u65f6\u4efb\u52a1\u3001\u4efb\u52a1\u534f\u7a0b\u7b49\uff0c\u8fd9\u4f7f\u5f97 PHP \u5728\u4f20\u7edf\u4e0a\u8f83\u4e3a\u5355\u7ebf\u7a0b\u3001\u963b\u585e\u7684\u7f3a\u70b9\u4e0a\u5f97\u5230\u4e86\u663e\u8457\u6539\u5584\u3002\u901a\u8fc7 Swoole\uff0cPHP \u53ef\u4ee5\u5904\u7406\u6210\u5343\u4e0a\u4e07\u7684\u5e76\u53d1\u8fde\u63a5\uff0c\u5e76\u80fd\u591f\u8fdb\u884c\u9ad8\u6548\u7684\u5f02\u6b65 IO \u64cd\u4f5c\uff0c\u6781\u5927\u63d0\u5347 Web \u670d\u52a1\u7684\u6027\u80fd\u548c\u54cd\u5e94\u901f\u5ea6\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. Swoole\u6982\u8ff0<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 \u4ec0\u4e48\u662fSwoole\uff1f<\/h3>\n\n\n\n<p>Swoole \u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684 PHP \u6269\u5c55\uff0c\u4e13\u6ce8\u4e8e\u63d0\u4f9b\u5e76\u53d1\u3001\u5f02\u6b65 I\/O \u652f\u6301\u3002Swoole \u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u589e\u5f3a PHP \u7684\u5de5\u5177\uff0c\u5b83\u901a\u8fc7\u63d0\u4f9b\u652f\u6301\u534f\u7a0b\u3001\u5f02\u6b65 I\/O\u3001WebSocket\u3001TCP\/UDP \u7b49\u534f\u8bae\u7684\u670d\u52a1\uff0c\u4f7f\u5f97 PHP \u7684\u6027\u80fd\u7a81\u7834\u4e86\u4f20\u7edf\u7684\u5355\u7ebf\u7a0b\u3001\u963b\u585e\u6a21\u5f0f\uff0c\u9002\u7528\u4e8e\u6784\u5efa\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u5e94\u7528\u3002<\/p>\n\n\n\n<p>\u4e0e\u4f20\u7edf\u7684 PHP-FPM \u6216 Apache\/Nginx \u914d\u5408\u4f7f\u7528\u7684 PHP \u73af\u5883\u4e0d\u540c\uff0cSwoole \u901a\u8fc7\u4f5c\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684 HTTP Server\u3001WebSocket Server \u6216\u8005\u5fae\u670d\u52a1\u6846\u67b6\u8fd0\u884c\uff0c\u80fd\u591f\u652f\u6301\u6210\u5343\u4e0a\u4e07\u7684\u5e76\u53d1\u8bf7\u6c42\uff0c\u540c\u65f6\u5904\u7406\u5f02\u6b65\u4efb\u52a1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.2 Swoole\u7684\u6838\u5fc3\u7279\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9ad8\u5e76\u53d1\u6027\u80fd<\/strong>\uff1a\u652f\u6301\u4e07\u7ea7\u5e76\u53d1\u8fde\u63a5\uff0c\u91c7\u7528\u591a\u8fdb\u7a0b\u3001\u534f\u7a0b\u3001\u5f02\u6b65 I\/O \u548c\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\uff0c\u63d0\u9ad8\u7cfb\u7edf\u541e\u5410\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u652f\u6301\u591a\u79cd\u534f\u8bae<\/strong>\uff1a\u9664\u4e86 HTTP \u534f\u8bae\u5916\uff0cSwoole \u8fd8\u652f\u6301 WebSocket\u3001TCP\u3001UDP\u3001Process \u7ba1\u7406\u7b49\u591a\u79cd\u534f\u8bae\uff0c\u9002\u7528\u4e8e\u591a\u79cd\u7f51\u7edc\u573a\u666f\u3002<\/li>\n\n\n\n<li><strong>\u5185\u7f6e\u534f\u7a0b\u652f\u6301<\/strong>\uff1aSwoole \u63d0\u4f9b\u4e86\u534f\u7a0b\u652f\u6301\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u534f\u7a0b\u5b9e\u73b0\u975e\u963b\u585e I\/O \u64cd\u4f5c\uff0c\u7b80\u5316\u5e76\u53d1\u7f16\u7a0b\u3002<\/li>\n\n\n\n<li><strong>\u5f02\u6b65 I\/O<\/strong>\uff1aSwoole \u652f\u6301\u5f02\u6b65 I\/O \u64cd\u4f5c\uff0c\u53ef\u4ee5\u5728\u4e0d\u963b\u585e\u4e3b\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3001\u6570\u636e\u5e93\u67e5\u8be2\u7b49 I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u3002<\/li>\n\n\n\n<li><strong>\u5b9a\u65f6\u4efb\u52a1\u4e0e\u4efb\u52a1\u8c03\u5ea6<\/strong>\uff1a\u63d0\u4f9b\u5b9a\u65f6\u5668\u529f\u80fd\uff0c\u80fd\u591f\u8c03\u5ea6\u5b9a\u65f6\u4efb\u52a1\uff0c\u9002\u5408\u7528\u4f5c\u540e\u53f0\u4efb\u52a1\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong>\u5185\u7f6e\u670d\u52a1\u5668<\/strong>\uff1aSwoole \u63d0\u4f9b\u5185\u7f6e HTTP\u3001WebSocket\u3001TCP\u3001UDP \u670d\u52a1\uff0c\u7b80\u5316\u5f00\u53d1\uff0c\u51cf\u5c11\u90e8\u7f72\u548c\u914d\u7f6e\u7684\u590d\u6742\u5ea6\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. Swoole\u57fa\u7840\u529f\u80fd<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 \u5b89\u88c5Swoole<\/h3>\n\n\n\n<p>Swoole \u662f\u4e00\u4e2a PHP \u6269\u5c55\uff0c\u53ef\u4ee5\u901a\u8fc7 <strong>pecl<\/strong> \u6216 <strong>\u7f16\u8bd1\u5b89\u88c5<\/strong> \u5b89\u88c5\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>pecl<\/code> \u5b89\u88c5<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>pecl install swoole<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>composer<\/code> \u5b89\u88c5 Swoole HTTP Server\uff08Swoole 4.x \u53ca\u4ee5\u4e0a\uff09<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>composer require swoole\/ide-helper<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528 <code>\u6e90\u7801<\/code> \u5b89\u88c5<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u514b\u9686 Swoole \u6e90\u7801\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   git clone https:\/\/github.com\/swoole\/swoole-src.git\n   cd swoole-src<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u6267\u884c\u5b89\u88c5\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   phpize\n   .\/configure\n   make &amp;&amp; sudo make install<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 \u57fa\u672c\u7684 HTTP Server<\/h3>\n\n\n\n<p>Swoole \u63d0\u4f9b\u4e86\u5185\u7f6e\u7684 HTTP Server\uff0c\u53ef\u4ee5\u7528\u6765\u66ff\u4ee3\u4f20\u7edf\u7684 Apache\/Nginx \u670d\u52a1\u5668\u3002Swoole \u7684 HTTP Server \u652f\u6301\u9ad8\u5e76\u53d1\uff0c\u80fd\u591f\u76f4\u63a5\u5904\u7406 HTTP \u8bf7\u6c42\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684 HTTP \u670d\u52a1<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ swoole_http_server.php\n$server = new swoole_http_server(\"127.0.0.1\", 9501);\n\n$server-&gt;on(\"request\", function ($request, $response) {\n    $response-&gt;header(\"Content-Type\", \"text\/html\");\n    $response-&gt;end(\"&lt;h1&gt;Hello, Swoole!&lt;\/h1&gt;\");\n});\n\n$server-&gt;start();<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u8be5\u4ee3\u7801\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee <code>http:\/\/127.0.0.1:9501<\/code>\uff0c\u770b\u5230\u8fd4\u56de\u7684\u5185\u5bb9\uff1a<code>&lt;h1&gt;Hello, Swoole!&lt;\/h1&gt;<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.3 \u534f\u7a0b\uff08Coroutine\uff09<\/h3>\n\n\n\n<p>\u534f\u7a0b\u662f Swoole \u63d0\u4f9b\u7684\u7528\u4e8e\u5b9e\u73b0\u5f02\u6b65\u975e\u963b\u585e\u64cd\u4f5c\u7684\u6838\u5fc3\u529f\u80fd\u3002Swoole \u7684\u534f\u7a0b\u7c7b\u4f3c\u4e8e\u7ebf\u7a0b\uff0c\u4f46\u76f8\u6bd4\u7ebf\u7a0b\u5360\u7528\u7684\u8d44\u6e90\u66f4\u5c11\uff0c\u4e14\u53ef\u4ee5\u4f7f\u7528\u540c\u6b65\u4ee3\u7801\u7684\u65b9\u5f0f\u5904\u7406\u5f02\u6b65\u4efb\u52a1\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u534f\u7a0b\u793a\u4f8b<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nCo\\run(function () {\n    \/\/ \u4f7f\u7528\u534f\u7a0b\u8fdb\u884c\u5f02\u6b65 HTTP \u8bf7\u6c42\n    $result = Co\\Http\\Client::get('http:\/\/www.example.com');\n    echo $result-&gt;body;\n});<\/code><\/pre>\n\n\n\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>Co\\run()<\/code> \u4f1a\u542f\u52a8\u4e00\u4e2a\u534f\u7a0b\uff0c\u8fd9\u4e2a\u534f\u7a0b\u4f1a\u53d1\u9001\u5f02\u6b65 HTTP \u8bf7\u6c42\uff0c\u83b7\u53d6\u8fd4\u56de\u5185\u5bb9\u540e\u6253\u5370\u51fa\u6765\u3002\u901a\u8fc7\u534f\u7a0b\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5199\u51fa\u975e\u5e38\u7b80\u6d01\u7684\u5f02\u6b65\u4ee3\u7801\uff0c\u800c\u4e0d\u9700\u8981\u624b\u52a8\u7ba1\u7406\u56de\u8c03\u51fd\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.4 WebSocket \u652f\u6301<\/h3>\n\n\n\n<p>Swoole \u652f\u6301 WebSocket \u534f\u8bae\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u8f7b\u677e\u5730\u6784\u5efa\u5b9e\u65f6\u901a\u4fe1\u7cfb\u7edf\uff0c\u5982\u804a\u5929\u5ba4\u3001\u5373\u65f6\u6d88\u606f\u63a8\u9001\u7b49\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u521b\u5efa WebSocket \u670d\u52a1<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$server = new swoole_websocket_server(\"127.0.0.1\", 9502);\n\n$server-&gt;on(\"open\", function ($server, $request) {\n    echo \"Connection open: {$request-&gt;fd}\\n\";\n});\n\n$server-&gt;on(\"message\", function ($server, $frame) {\n    echo \"Received message: {$frame-&gt;data}\\n\";\n    $server-&gt;push($frame-&gt;fd, \"Hello, you sent: {$frame-&gt;data}\");\n});\n\n$server-&gt;on(\"close\", function ($server, $fd) {\n    echo \"Connection closed: {$fd}\\n\";\n});\n\n$server-&gt;start();<\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u542f\u52a8\u4e00\u4e2a WebSocket \u670d\u52a1\uff0c\u5141\u8bb8\u5ba2\u6237\u7aef\u8fde\u63a5\u5e76\u8fdb\u884c\u6d88\u606f\u901a\u4fe1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.5 \u5f02\u6b65 I\/O \u548c\u5b9a\u65f6\u4efb\u52a1<\/h3>\n\n\n\n<p>Swoole \u63d0\u4f9b\u5f02\u6b65 I\/O \u652f\u6301\uff0c\u5141\u8bb8 PHP \u975e\u963b\u585e\u5730\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3001\u6570\u636e\u5e93\u67e5\u8be2\u7b49\u8017\u65f6\u4efb\u52a1\u3002\u540c\u65f6\uff0cSwoole \u4e5f\u63d0\u4f9b\u5b9a\u65f6\u4efb\u52a1\u7684\u529f\u80fd\uff0c\u652f\u6301\u5b9a\u65f6\u6267\u884c\u4efb\u52a1\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5f02\u6b65\u6587\u4ef6\u64cd\u4f5c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Swoole\\Async::readFile('largefile.txt', function ($filename, $content) {\n    echo \"File content: \" . $content;\n});<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b9a\u65f6\u4efb\u52a1<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$timer_id = swoole_timer_tick(1000, function() {\n    echo \"This task runs every second\\n\";\n});<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Swoole\u8fdb\u9636\u529f\u80fd<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">3.1 Swoole \u534f\u7a0b\u5ba2\u6237\u7aef<\/h3>\n\n\n\n<p>Swoole \u534f\u7a0b\u5ba2\u6237\u7aef\u5141\u8bb8\u4f7f\u7528\u534f\u7a0b\u7684\u65b9\u5f0f\u6267\u884c\u5f02\u6b65\u7684\u7f51\u7edc\u8bf7\u6c42\u3002\u5b83\u652f\u6301\u591a\u79cd\u534f\u8bae\uff0c\u5982 HTTP\u3001MySQL\u3001Redis \u7b49\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u534f\u7a0b MySQL \u5ba2\u6237\u7aef<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Co\\run(function () {\n    $db = new Co\\MySQL();\n    $db-&gt;connect(&#91;\n        'host' =&gt; '127.0.0.1',\n        'user' =&gt; 'root',\n        'password' =&gt; 'password',\n        'database' =&gt; 'test',\n    ]);\n    $result = $db-&gt;query(\"SELECT * FROM users\");\n    var_dump($result);\n});<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u534f\u7a0b Redis \u5ba2\u6237\u7aef<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Co\\run(function () {\n    $redis = new Co\\Redis();\n    $redis-&gt;connect('127.0.0.1', 6379);\n    $redis-&gt;set('key', 'value');\n    echo $redis-&gt;get('key');\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.2 \u9ad8\u5e76\u53d1\u5904\u7406\u548c\u8d1f\u8f7d\u5747\u8861<\/h3>\n\n\n\n<p>Swoole \u652f\u6301\u591a\u8fdb\u7a0b\u548c\u8d1f\u8f7d\u5747\u8861\u7684\u673a\u5236\uff0c\u80fd\u591f\u6839\u636e\u8bf7\u6c42\u7684\u6570\u91cf\u548c\u8d1f\u8f7d\u52a8\u6001\u5206\u914d\u8d44\u6e90\uff0c\u9002\u5408\u9ad8\u5e76\u53d1\u3001\u9ad8\u8d1f\u8f7d\u7684\u573a\u666f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8fdb\u7a0b\u7ba1\u7406<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$server = new swoole_http_server(\"127.0.0.1\", 9503);\n$server-&gt;set(&#91;\n    'worker_num' =&gt; 4,  \/\/ \u8bbe\u7f6e\u5de5\u4f5c\u8fdb\u7a0b\u6570\n]);\n\n$server-&gt;on(\"request\", function ($request, $response) {\n    $response-&gt;end(\"Hello Swoole\");\n});\n\n$server-&gt;start();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.3 \u5fae\u670d\u52a1\u67b6\u6784<\/h3>\n\n\n\n<p>Swoole \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u8fdb\u7a0b\u7ba1\u7406\u529f\u80fd\uff0c\u53ef\u4ee5\u6784\u5efa\u9ad8\u5e76\u53d1\u3001\u9ad8\u6027\u80fd\u7684\u5fae\u670d\u52a1\u67b6\u6784\u3002\u7ed3\u5408 Swoole \u7684 HTTP \u670d\u52a1\u5668\u3001WebSocket\u3001\u534f\u7a0b\u7b49\u7279\u6027\uff0c\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u5206\u5e03\u5f0f\u7cfb\u7edf\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5fae\u670d\u52a1\u793a\u4f8b<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ serviceA\n$server = new swoole_http_server('127.0.0.1', 9501);\n$server-&gt;on('request', function ($request, $response) {\n    $response-&gt;end\n\n('Service A Response');\n});\n$server-&gt;start();\n\n\/\/ serviceB\n$client = new swoole_http_client('127.0.0.1', 9501);\n$client-&gt;get('\/', function ($response) {\n    echo $response-&gt;body;\n});<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.4 \u4f7f\u7528 Swoole \u4e0e\u5176\u4ed6\u6846\u67b6\u96c6\u6210<\/h3>\n\n\n\n<p>Swoole \u53ef\u4ee5\u4e0e\u5e38\u89c1\u7684 PHP \u6846\u67b6\uff08\u5982 Laravel\u3001ThinkPHP \u7b49\uff09\u8fdb\u884c\u96c6\u6210\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u5904\u7406\u9ad8\u5e76\u53d1\u8bf7\u6c42\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Swoole\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 \u4f7f\u7528\u534f\u7a0b\u7b80\u5316\u5f02\u6b65\u7f16\u7a0b<\/h3>\n\n\n\n<p>\u5c3d\u91cf\u5229\u7528 Swoole \u7684\u534f\u7a0b\u529f\u80fd\u6765\u7b80\u5316\u5f02\u6b65\u7f16\u7a0b\uff0c\u51cf\u5c11\u56de\u8c03\u5730\u72f1\uff0c\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 \u6309\u9700\u8c03\u6574\u8fdb\u7a0b\u6570\u548c\u534f\u7a0b\u6570<\/h3>\n\n\n\n<p>\u6839\u636e\u5e94\u7528\u7684\u5e76\u53d1\u91cf\u548c\u6027\u80fd\u9700\u6c42\uff0c\u7075\u6d3b\u8c03\u6574\u5de5\u4f5c\u8fdb\u7a0b\u6570\u548c\u534f\u7a0b\u6570\uff0c\u907f\u514d\u8d44\u6e90\u6d6a\u8d39\u6216\u74f6\u9888\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 \u5b89\u5168\u6027\u548c\u7a33\u5b9a\u6027<\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u751f\u4ea7\u73af\u5883\u4e2d\u7684\u670d\u52a1\uff0c\u786e\u4fdd\u505a\u597d\u5bb9\u9519\u5904\u7406\u3001\u8fde\u63a5\u6c60\u7ba1\u7406\u3001\u65e5\u5fd7\u8bb0\u5f55\u548c\u76d1\u63a7\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.4 \u6027\u80fd\u4f18\u5316<\/h3>\n\n\n\n<p>\u901a\u8fc7\u5f02\u6b65 I\/O \u548c\u534f\u7a0b\u7ba1\u7406\uff0c\u5c3d\u53ef\u80fd\u51cf\u5c11\u963b\u585e\u64cd\u4f5c\uff0c\u5229\u7528\u5b9a\u65f6\u4efb\u52a1\u4f18\u5316\u540e\u53f0\u4efb\u52a1\u5904\u7406\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3<\/h2>\n\n\n\n<p>Swoole \u662f\u4e00\u4e2a\u5f3a\u5927\u7684 PHP \u6269\u5c55\uff0c\u80fd\u591f\u663e\u8457\u63d0\u5347 PHP \u5e94\u7528\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\uff0c\u9002\u5408\u6784\u5efa\u9ad8\u6027\u80fd\u3001\u5b9e\u65f6\u548c\u9ad8\u5e76\u53d1\u7684\u7f51\u7edc\u670d\u52a1\u3002\u901a\u8fc7 Swoole \u63d0\u4f9b\u7684\u9ad8\u6027\u80fd HTTP \u670d\u52a1\u3001WebSocket\u3001\u5f02\u6b65 I\/O\u3001\u534f\u7a0b\u7b49\u529f\u80fd\uff0cPHP \u5f00\u53d1\u8005\u80fd\u591f\u8f7b\u677e\u6784\u5efa\u9ad8\u6548\u3001\u53ef\u4f38\u7f29\u7684 Web \u5e94\u7528\u548c\u5fae\u670d\u52a1\u3002<\/p>\n\n\n\n<p>\u4ece\u57fa\u7840\u7684 HTTP \u670d\u52a1\u5668\u5230\u8fdb\u9636\u7684\u5fae\u670d\u52a1\u67b6\u6784\uff0cSwoole \u4e3a\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u4f7f\u5f97 PHP \u4e0d\u518d\u662f\u4e00\u4e2a\u201c\u5355\u7ebf\u7a0b\u963b\u585e\u201d\u7684\u8bed\u8a00\uff0c\u800c\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u5f02\u6b65\u5e76\u53d1\u5e73\u53f0\u3002<\/p>\n\n\n\n<p>\u66f4\u591a\u4fe1\u606f\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c06\u5168\u9762\u4ecb\u7ecd Swoole \u7684\u57fa\u7840\u6982\u5ff5\u3001\u6838\u5fc3\u529f\u80fd\u3001\u8fdb\u9636\u6280\u5de7\u548c\u6700\u4f73\u5b9e\u8df5\uff0c\u4ece\u5165\u95e8\u5230\u8fdb\u9636\u5e2e\u52a9\u4f60\u5168\u9762\u638c\u63e1 Swool [&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-274","post","type-post","status-publish","format-standard","hentry","category-swoole"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/274","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=274"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/274\/revisions"}],"predecessor-version":[{"id":275,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/274\/revisions\/275"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}